Skip to content

Instantly share code, notes, and snippets.

View salmaanahmed's full-sized avatar
💭
Software Engineer with tons of experience in Android | iOS | Flutter | Xamarin

Salmaan Ahmed salmaanahmed

💭
Software Engineer with tons of experience in Android | iOS | Flutter | Xamarin
View GitHub Profile
func processImageData() async -> Image {
let dataResource = await loadWebResource("dataprofile.txt")
let imageResource = await loadWebResource("imagedata.dat")
let imageTmp = await decodeImage(dataResource, imageResource)
let imageResult = await dewarpAndCleanupImage(imageTmp)
return imageResult
}
func processImageData(completionBlock: (result: Image) -> Void) {
loadWebResource("dataprofile.txt") { dataResource in
loadWebResource("imagedata.dat") { imageResource in
decodeImage(dataResource, imageResource) { imageTmp in
dewarpAndCleanupImage(imageTmp) { imageResult in
completionBlock(imageResult)
}
}
}
}
// Swift 4: 'Int??'
// Swift 5: 'Int?'
let result = try? database?.countOfRows(matching: predicate)
// Swift 4: 'String??'
// Swift 5: 'String?'
let myString = try? String(data: someData, encoding: .utf8)
// Swift 4: '[String: Any]??'
let d: [String: String?] = ["a": "1", "b": nil, "c": "3"]
let r4 = d.compactMapValues({$0})
// r4 == ["a": "1", "c": "3"]
let d: [String: String?] = ["a": "1", "b": nil, "c": "3"]
let r1 = d.filter { $0.value != nil }.mapValues { $0! }
let r2 = d.reduce(into: [String: String]()) { (result, item) in result[item.key] = item.value }
// r1 == r2 == ["a": "1", "c": "3"]
[1, 2, 3, -1, -2].count(where: { $0 > 0 }) // => 3
[1, 2, 3, -1, -2].reduce(0) { $1 > 0 ? $0 + 1 : $0 }
[1, 2, 3, -1, -2].lazy.filter({ $0 > 0 }).count // => 3
[1, 2, 3, -1, -2].filter({ $0 > 0 }).count // => 3
@salmaanahmed
salmaanahmed / NewCompilationConditions.swift
Created January 28, 2019 12:06
Compilation conditions in swift 5
#if swift(<4.2)
// This will only be executed if the Swift version is less than 4.2.
#endif
#if compiler(<4.2)
// This will only be executed if the Swift compiler version is less than 4.2.
#endif