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
struct Car { | |
var id: Int | |
init?(id: Int) { | |
guard id > 0 else { | |
return nil | |
} | |
self.id = id | |
} | |
func checkSystemWhenStart() throws -> String { |
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
import Foundation | |
@dynamicCallable | |
class DynamicCalculator { | |
func dynamicallyCall(withArguments args: [Int]) -> Int { | |
return args.reduce(0, +) | |
} | |
func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Int>) -> Int { | |
let sum = args.reduce(0) { |
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
import Foundation | |
// Dynamic Member Lookup | |
@dynamicMemberLookup | |
struct Computer { | |
var values: [String: String] | |
subscript(dynamicMember member: String) -> String { | |
return values[member, default: "Value for \(member) not exist"] | |
} | |
} |
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
enum ServerError: Error { | |
case urlFailed | |
case networkFailed | |
case serverFailed | |
case dataFailed | |
} | |
func fetchDataFromServer(_ urlString: String, completion: @escaping (Result<Data, ServerError>) -> ()) { | |
// Check if URL valid |
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 fetchDataFromServer(_ urlString: String, completion: @escaping (Data?, String?) -> ()) { | |
// Check if URL valid | |
guard let url = URL(string: urlString) else { | |
completion(nil, "Invalid URL") | |
return | |
} | |
let defaultSession = URLSession(configuration: URLSessionConfiguration.default) | |
let dataTask : URLSessionTask = defaultSession.dataTask(with: url) { data, response, error in | |
guard error == nil else { |
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 someStrangeString = ###"This is a sample of ##" and "## strings "### | |
// output: This is a sample of ##" and "## strings |
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 rawStringWithHashSymbol = ##"Sample raw string with # symbol"## | |
// output: Sample raw string with # symbol |
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 age = 18 | |
let ageDescription = #"My age = \#(age)"# // Swift 4.2: \(age) => Swift 5: \#(age) | |
// output: My age = 18 |
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 rawStringInSwift5 = #"This is a sample about backslash \ and quote mark " in Swift 5"# | |
// output: This is a sample about backslash \ and quote mark " in Swift 5 |
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 backslashString = "Hello, backslash \\" | |
// output: Hello, backslash \ | |
let quotemarkString = "Hello, quote mark \"" | |
// output: Hello, quote mark " |
NewerOlder