Skip to content

Instantly share code, notes, and snippets.

View nhathm's full-sized avatar
🌴
On vacation

Hoàng Minh Nhật nhathm

🌴
On vacation
View GitHub Profile
struct Car {
var id: Int
init?(id: Int) {
guard id > 0 else {
 return nil
 }
self.id = id
}
func checkSystemWhenStart() throws -> String {
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) {
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"]
}
}
enum ServerError: Error {
case urlFailed
case networkFailed
case serverFailed
case dataFailed
}
func fetchDataFromServer(_ urlString: String, completion: @escaping (Result<Data, ServerError>) -> ()) {
// Check if URL valid
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 {
let someStrangeString = ###"This is a sample of ##" and "## strings "###
// output: This is a sample of ##" and "## strings
let rawStringWithHashSymbol = ##"Sample raw string with # symbol"##
// output: Sample raw string with # symbol
let age = 18
let ageDescription = #"My age = \#(age)"# // Swift 4.2: \(age) => Swift 5: \#(age)
// output: My age = 18
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
let backslashString = "Hello, backslash \\"
// output: Hello, backslash \
let quotemarkString = "Hello, quote mark \""
// output: Hello, quote mark "