From 6d6480b5ebdb2997ac86fbc71ce3ef5d4f805f59 Mon Sep 17 00:00:00 2001 From: Marcel Schaeben Date: Tue, 20 Jan 2015 09:03:10 +0100 Subject: [PATCH] replace unmanaged objects with withUnsafeMutablePointer to fix matthewpalmer/Locksmith#13 --- Locksmith/Locksmith.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Locksmith/Locksmith.swift b/Locksmith/Locksmith.swift index 13c60ef..3956c20 100644 --- a/Locksmith/Locksmith.swift +++ b/Locksmith/Locksmith.swift @@ -14,7 +14,8 @@ public class Locksmith: NSObject { // MARK: Perform request class func performRequest(request: LocksmithRequest) -> (NSDictionary?, NSError?) { let type = request.type - var result: Unmanaged? = nil + //var result: Unmanaged? = nil + var result: AnyObject? var status: OSStatus? var parsedRequest: NSMutableDictionary = parseRequest(request) @@ -23,13 +24,13 @@ public class Locksmith: NSObject { switch type { case .Create: - status = SecItemAdd(requestReference, &result) + status = withUnsafeMutablePointer(&result) { SecItemAdd(requestReference, UnsafeMutablePointer($0)) } case .Read: - status = SecItemCopyMatching(requestReference, &result) + status = withUnsafeMutablePointer(&result) { SecItemCopyMatching(requestReference, UnsafeMutablePointer($0)) } case .Delete: status = SecItemDelete(requestReference) case .Update: - status = Locksmith.performUpdate(requestReference, result: &result) + status = Locksmith.performUpdate(requestReference, result: &result) default: status = nil } @@ -41,7 +42,8 @@ public class Locksmith: NSObject { if result != nil { if type == .Read && status == errSecSuccess { - if let data = result?.takeUnretainedValue() as? NSData { + + if let data = result as? NSData { // Convert the retrieved data to a dictionary resultsDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? NSDictionary } @@ -56,12 +58,13 @@ public class Locksmith: NSObject { } } - private class func performUpdate(request: CFDictionaryRef, result: UnsafeMutablePointer?>) -> OSStatus { + private class func performUpdate(request: CFDictionaryRef, inout result: AnyObject?) -> OSStatus { // We perform updates to the keychain by first deleting the matching object, then writing to it with the new value. SecItemDelete(request) - // Even if the delete request failed (e.g. if the item didn't exist before), still try to save the new item. + // Even if the delete request failed (e.g. if the item didn't exist before), still try to save the new item. // If we get an error saving, we'll tell the user about it. - var status: OSStatus = SecItemAdd(request, result) + + var status: OSStatus = withUnsafeMutablePointer(&result) { SecItemAdd(request, UnsafeMutablePointer($0)) } return status }