Course notesTypes & Variables
- variable definition:
```swift
var variableName
var variableName = "value"
```
- constant definition:
```swift
let constantName
let constantName = "value"
```
- comments:
```swift
// This is a line comment
/* This is a block comment */
```
- types: Int, Float, Double, String, Bool
- type definition:
```swift
var variableName: Int
var variableName: Int = 0
var variableName: Float
var variableName: Float = 0.0
var variableName: Double
var variableName: Double = 0.0
var variableName: String
var variableName: String = "This is a string"
var variableName: Bool
var variableName: Bool = true
```
- interpolation:
```swift
var variable = "variable"
var variableName = "this is a \(variable) in a string"
```
- variable operations:
```swift
var variableName = 0
variableName += 1
variableName -= 1
variableName *= 1
variableName /= 1
```
Control structures
- for loop:
```swift
for i in 1...5 {
print(i)
}
for i in 0..<5 {
print(i)
}
```
- condition operators: &&, ||
- while loop:
```swift
while condition {
print("")
}
```
- if condition:
```swift
if condition {
print("")
} else if otherCondition {
print("")
} else {
print("")
}
```
- switch:
```swift
switch nomVariable {
case valueToTest:
print("")
case otherValueToTest:
print("")
default:
print("")
}
```
Collections
- empty array:
```swift
var arrayName = [Type]()
var arrayName: [Type] = []
```
- empty dictionnary:
```swift
var dictionnaryName = [KeyType: ValueType]()
var arrayName: [Type] = []
```
- array element iteration:
```swift
for elementName in arrayName {
print(elementName)
}
```
- collection element count:
```swift
arrayName.count
```
Optionals
- definition:
```swift
var optionalName: Type?
var optionalName: Type? = optionalValue
```
- safe access:
> :warning: An optional variable as the type **Optional** not an other
```swift
if let variableName = optionalName {
print("")
}
guard let variableName = optionalName else {
return nil
}
```
Methods and Actions
- action definition:
```swift
func actionName (parameterName: ParameterType) {
print("")
}
```
- method definition:
```swift
func methodName (parameterName: ParameterType) -> ReturnType {
print("")
return returnValue
}
```
User Input
- read input:
```swift
if let userInput = readLine() {
print(userInput)
}
```
Course notesClasses and objects
- class declaration:
> :warning: A property without a default value need to be in the constructor
```swift
class ClassName {
var propertyWithValueName: Type = value
var propertyWithoutValueName: Type
init(propertyWithoutValueName: Type) {
self.propertyWithoutValueName = propertyWithoutValueName
}
func methodName() {
print("")
}
}
```
- class property and method access:
```swift
var instance = ClassName()
instance.propertyName
instance.methodName()
```
- class method: `static`
- class heritage:
```swift
class ClassName {}
class SubClassName: ClassName {}
```
- override parent method: `override`
- call parent method or property: `super`
- instance type check:
```swift
var instance = ClassName()
if instance is ClassName { }
```
- instance cast:
> :warning: A child class can bu upcasted to become of the type of the parent class
> :warning: A parent can be downcasted to become of the type of the child class
```swift
class ParentClassName {}
class ChildClassName: ParentClassName {}
// We want to cast to the child class
var parentClassInstance = ParentClassName()
// We know that it will works
if let childClassInstance = parentClassInstance as! ChildClassName {}
// We don't know if it will works
if let childClassInstance = parentClassInstance as? ChildClassName {}
// We want to cast to the parent class
var childClassInstance = ChildClassName()
// We don't know if it will works
if let parentClassInstance = childClassInstance as? ParentClassName {}
```
Any Type
- universal type: `Any`
- universal class: `AnyObject`
Enum
- enum definition:
```swift
enum TypeName: Type {
case caseName, caseName, ...
}
```
Calculated Property
- calculated property definition:
```swift
var calculatedPropertyName: Type {
get {
return otherPropertyNameWithOperation
}
set {
otherPropertyName = calculatedPropertyNameWithOperation
}
}
```
- calculated property definition in read only:
```swift
var calculatedPropertyName: Type {
get {
return otherPropertyNameWithOperation
}
}
var calculatedPropertyName: Type {
return otherPropertyNameWithOperation
}
```
- observe property:
```swift
var observableProperty = defaultValue {
willSet {
print("This happen before an edit of the property")
print("Here we can use \(newValue) and the property itself(before editing)")
}
didSet {
print("This happen after an edit of the property")
print("Here we can use \(oldValue) and the property itself(after editing)")
}
}
```
Access Control
> :warning: A property can't have an access more important than its class
- access usage:
```swift
access class ClassName {
access var propertyName = propertyValue
}
```
- access possibility and order: `public` > `internal` > `fileprivate` > `private`
️Notes
Courses:
[x] SwiftUI Tutorial by Apple Very good course to learn Swift UI, it's very complete
[x] Swift Base by OpenClassroom
Course notes
Types & Variables
- variable definition: ```swift var variableName var variableName = "value" ``` - constant definition: ```swift let constantName let constantName = "value" ``` - comments: ```swift // This is a line comment /* This is a block comment */ ``` - types: Int, Float, Double, String, Bool - type definition: ```swift var variableName: Int var variableName: Int = 0 var variableName: Float var variableName: Float = 0.0 var variableName: Double var variableName: Double = 0.0 var variableName: String var variableName: String = "This is a string" var variableName: Bool var variableName: Bool = true ``` - interpolation: ```swift var variable = "variable" var variableName = "this is a \(variable) in a string" ``` - variable operations: ```swift var variableName = 0 variableName += 1 variableName -= 1 variableName *= 1 variableName /= 1 ```Control structures
- for loop: ```swift for i in 1...5 { print(i) } for i in 0..<5 { print(i) } ``` - condition operators: &&, || - while loop: ```swift while condition { print("") } ``` - if condition: ```swift if condition { print("") } else if otherCondition { print("") } else { print("") } ``` - switch: ```swift switch nomVariable { case valueToTest: print("") case otherValueToTest: print("") default: print("") } ```Collections
- empty array: ```swift var arrayName = [Type]() var arrayName: [Type] = [] ``` - empty dictionnary: ```swift var dictionnaryName = [KeyType: ValueType]() var arrayName: [Type] = [] ``` - array element iteration: ```swift for elementName in arrayName { print(elementName) } ``` - collection element count: ```swift arrayName.count ```Optionals
- definition: ```swift var optionalName: Type? var optionalName: Type? = optionalValue ``` - safe access: > :warning: An optional variable as the type **Optional** not an other ```swift if let variableName = optionalName { print("") } guard let variableName = optionalName else { return nil } ```Methods and Actions
- action definition: ```swift func actionName (parameterName: ParameterType) { print("") } ``` - method definition: ```swift func methodName (parameterName: ParameterType) -> ReturnType { print("") return returnValue } ```User Input
- read input: ```swift if let userInput = readLine() { print(userInput) } ```[x] Swift POO by OpenClassroom
Course notes
Classes and objects
- class declaration: > :warning: A property without a default value need to be in the constructor ```swift class ClassName { var propertyWithValueName: Type = value var propertyWithoutValueName: Type init(propertyWithoutValueName: Type) { self.propertyWithoutValueName = propertyWithoutValueName } func methodName() { print("") } } ``` - class property and method access: ```swift var instance = ClassName() instance.propertyName instance.methodName() ``` - class method: `static` - class heritage: ```swift class ClassName {} class SubClassName: ClassName {} ``` - override parent method: `override` - call parent method or property: `super` - instance type check: ```swift var instance = ClassName() if instance is ClassName { } ``` - instance cast: > :warning: A child class can bu upcasted to become of the type of the parent class > :warning: A parent can be downcasted to become of the type of the child class ```swift class ParentClassName {} class ChildClassName: ParentClassName {} // We want to cast to the child class var parentClassInstance = ParentClassName() // We know that it will works if let childClassInstance = parentClassInstance as! ChildClassName {} // We don't know if it will works if let childClassInstance = parentClassInstance as? ChildClassName {} // We want to cast to the parent class var childClassInstance = ChildClassName() // We don't know if it will works if let parentClassInstance = childClassInstance as? ParentClassName {} ```Any Type
- universal type: `Any` - universal class: `AnyObject`Enum
- enum definition: ```swift enum TypeName: Type { case caseName, caseName, ... } ```Calculated Property
- calculated property definition: ```swift var calculatedPropertyName: Type { get { return otherPropertyNameWithOperation } set { otherPropertyName = calculatedPropertyNameWithOperation } } ``` - calculated property definition in read only: ```swift var calculatedPropertyName: Type { get { return otherPropertyNameWithOperation } } var calculatedPropertyName: Type { return otherPropertyNameWithOperation } ``` - observe property: ```swift var observableProperty = defaultValue { willSet { print("This happen before an edit of the property") print("Here we can use \(newValue) and the property itself(before editing)") } didSet { print("This happen after an edit of the property") print("Here we can use \(oldValue) and the property itself(after editing)") } } ```Access Control
> :warning: A property can't have an access more important than its class - access usage: ```swift access class ClassName { access var propertyName = propertyValue } ``` - access possibility and order: `public` > `internal` > `fileprivate` > `private`[x] iOS Base by OpenClassroom
[ ] iOS et MVC by OpenClassroom
[ ] 100 days of SwiftUI
Context (User Story)
As a dev
I want to learn Swift
In order to help in the iOS part of XpeApp
Steps
Acceptance Criteria
Blockers