Deprecated, new version is published here: https://github.com/xremix/Personal-Cheatsheet-Collection/
Cheat Sheet for iOS Development using Swift, containing a couple of code samples that are interesting to know and remember when needed. Mostly interesting are longer code samples at the bottom.
(lldb) po [myItem.myProperty]
if let count = getCount(){
return count
}
return 0
for case let item as MyVar in myArray{
// ...
}
func getTime() -> (Int, Int, Int) {
// ...
return ( hour, minute, second)
}
func getTime() -> (hour: Int, minute: Int,second: Int) {
let hour = 1
let minute = 2
let second = 3
return ( hour, minute, second)
}
class family {
var _members:Int = 2
var members:Int {
get {
return _members
}
set (newVal) {
if newVal >= 2 {
_members = newVal
} else {
println('error: cannot have family with less than 2 members')
}
}
}
}
let startTime = CFAbsoluteTimeGetCurrent()
print("Time: \(CFAbsoluteTimeGetCurrent() - startTime)")
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
}
}
func measure(_ title: String, block: (() -> ()) -> ()) {
let startTime = CFAbsoluteTimeGetCurrent()
block {
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime
print("\(title):: Time: \(timeElapsed)")
}
}
func demo(){
// For asynchronous code:
measure("async test") {finish in
myAsyncCall {
finish()
}
// Code to Test here
}
// For synchronous code:
measure("sync test") {finish in
// code to benchmark
finish()
// Code to Teste here
}
}