user1141351 user1141351 - 2 years ago 82
iOS Question

Cannot invoke initializer for type 'NSDictionary' in Swift2.0

It is probably simple but, since I'm not very experienced at Swift, I can't figure how to port this line to Swift 2.0.

func setPasscode(identifier: String, passcode: String) {
var dataFromString: NSData = passcode.dataUsingEncoding(NSUTF8StringEncoding)!;

var keychainQuery = NSDictionary(objects: [kSecClassGenericPassword, identifier, dataFromString], forKeys: [kSecClass, kSecAttrService, kSecValueData])

SecItemDelete(keychainQuery as CFDictionaryRef);
var status: OSStatus = SecItemAdd(keychainQuery as CFDictionaryRef, nil);

the error message:
Cannot invoke initializer for type 'NSDictionary' with an argument list of type '(objects: NSArray, forKeys: [CFString])'

Answer Source
let identifier = "someIdentifier"
let dataFromString = "someData"
let values = [kSecClassGenericPassword, identifier, dataFromString]
let keys: [NSString] = [kSecClass, kSecAttrService, kSecValueData]
let keychainQuery = NSDictionary(objects: values, forKeys: keys)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download