This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func divide(a: Int, b: Int?) -> Int? { | |
| guard let divisor = b, divisor != 0 else { | |
| return nil | |
| } | |
| return a / divisor | |
| } | |
| if let result = divide(a: 10, b: 2) { | |
| print(result) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let session = URLSession.shared | |
| let url = "https://www.example.com" | |
| let task = session.dataTask(with:URL(string: url)!){ data, response, error in | |
| if let httpResponse = response as? HTTPURLResponse { | |
| print(httpResponse.statusCode) | |
| } | |
| } | |
| task.resume() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let number = 5 | |
| switch number { | |
| case 1: | |
| print("The number is 1") | |
| case 2: | |
| print("The number is 2") | |
| default: | |
| print("The number is not 1 or 2") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let foo = 10 | |
| var bar = 20 | |
| bar = 30 | |
| foo = 40 | |
| // Error: Cannot assign to 'let' value 'foo' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension View { | |
| func myCustomModifier() -> some View { | |
| self | |
| .font(.headline) | |
| .foregroundColor(.red) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let a = true | |
| let b = false | |
| let result = a && b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let url = URL(string: "https://www.example.com") | |
| let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in | |
| guard let data = data else { return } | |
| print(String(data: data, encoding: .utf8)!) | |
| } | |
| task.resume() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| lazy var name : String = "John" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CustomObject { | |
| var id: Int | |
| var value: String | |
| init(id: Int, value: String) { | |
| self.id = id | |
| self.value = value | |
| } | |
| } | |
| let objects = [CustomObject(id: 1, value: "c"), CustomObject(id: 3, value: "a"), CustomObject(id: 2, value: "b")] | |
| let sortedObjects = objects.sorted { $0.id < $1.id } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyClass { | |
| private var myProperty: Int | |
| init(myProperty: Int) { | |
| self.myProperty = myProperty | |
| } | |
| } |