为在 Swift 中开发 iOS 应用程序而提供的常用工具集合。如果觉得有帮助,请帮忙。
处理 Grand Central Dispatch 队列的工具
// Perform a block on the given queue
performOn(.Main) { self.tableView.reloadData() }
performOn(.LowPriority) { self.clearCache() }
// Delay a block on the given queue - if no queue is provided .Main is assumed
delay(0.5) { self.hideSpinner() }
delay(30, .Background) { self.logActivity() }
// Queue types available:
enum QueueType {
case Main // The app's main queue
case Background // Background queue (DISPATCH_QUEUE_PRIORITY_BACKGROUND)
case LowPriority // Background queue (DISPATCH_QUEUE_PRIORITY_LOW)
case HighPriority // Background queue (DISPATCH_QUEUE_PRIORITY_HIGH)
}
扩展 NSCoding,使其更适合 Swift 使用
// Archive an NSCoding to NSData
let note = Note(title: "secret", content: "I like turtles")
let archivedData = note.archive()
// Unarchive NSData into an NSCoding object
let unarchivedNote = Note.unarchive(archivedData)
unarchivedNote.content // "I like turtles"
// Run the given block with CAAnimations disabled
withCAAnimationsDisabled { self.view.resize() }
// Fetch region info from NSLocale:
NSLocale.currentLocale().countryCode // String e.g.: "US"
NSLocale.currentLocale().countryName // String e.g.: "United States"
// Get info about the currently running app (taken from the
// main bundle's) Info.plist file
App.name
App.version
App.formattedNameAndVersion
// Returns true/false whether the app running within the simulator or not
App.inSimulator
// Returns true/false whether the app is running in DEBUG mode
App.isDebug
// Print a formatted log message for a CustomDebugStringConvertible
struct Animal: CustomDebugStringConvertible {
let name: String
var debugDescription: String { return name }
}
let dog = Animal(name: "Woofy")
debugLog(dog, "is tired") // prints "(Woofy) is tired" to the console