Get a Swift Variable’s Actual Name as String

This is officially supported in Swift 3 using #keyPath() https://github.com/apple/swift-evolution/blob/master/proposals/0062-objc-keypaths.md Example usage would look like: NSPredicate(format: “%K == %@”, #keyPath(Person.firstName), “Wendy”) In Swift 4 we have something even better: \KeyPath notation https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md NSPredicate(format: “%K == %@”, \Person.mother.firstName, “Wendy”) // or let keyPath = \Person.mother.firstName NSPredicate(format: “%K == %@”, keyPath, “Andrew”) The shorthand is a welcome … Read more

How to remove diacritics from a String in Swift?

You can operate directly on a Swift String (if “Foundation” is imported): let foo = “één” let bar = foo.stringByFoldingWithOptions(.DiacriticInsensitiveSearch, locale: NSLocale.currentLocale()) print(bar) // een Swift 3: let foo = “één” let bar = foo.folding(options: .diacriticInsensitive, locale: .current) print(bar) // een

Can you execute an Applescript script from a Swift Application

As Kamaros suggests, you can call NSApplescript directly without having to launch a separate process via NSTask (as CRGreen suggests.) Swift Code let myAppleScript = “…” var error: NSDictionary? if let scriptObject = NSAppleScript(source: myAppleScript) { if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError( &error) { print(output.stringValue) } else if (error != nil) { print(“error: \(error)”) } … Read more

How to create generic protocols in Swift?

It’s a little different for protocols. Look at “Associated Types” in Apple’s documentation. This is how you use it in your example protocol ApiMapperProtocol { associatedtype T associatedtype U func MapFromSource(_:T) -> U } class UserMapper: NSObject, ApiMapperProtocol { typealias T = NSDictionary typealias U = UserModel func MapFromSource(_ data:NSDictionary) -> UserModel { var user … Read more