我被卡通转换钥匙串查询结果使用Swift。
我的要求似乎在工作:
let queryAttributes = NSDictionary(objects: [kSecClassGenericPassword,"MyService","MyAccount",true],forKeys: [kSecClass,kSecAttrService,kSecAttrAccount,kSecReturnData]) dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT,0),{ var dataTypeRef : Unmanaged<AnyObject>? let status = SecItemCopyMatching(queryAttributes,&dataTypeRef); let retrievedData : NSData = dataTypeRef!.takeRetainedValue() as NSData *** ^^^^can't compile this line^^^^ })
我的问题是,代码不会编译:
Bitcast requires both operands to be pointer or neither %114 = bitcast %objc_object* %113 to %PSs9AnyObject_,!dbg !487 Bitcast requires both operands to be pointer or neither %115 = bitcast %PSs9AnyObject_ %114 to i8*,!dbg !487 LLVM ERROR: Broken function found,compilation aborted!
我不知道如何转换非托管的< AnyObject>到NSData。
有任何想法吗?
使用withUnsafeMutablePointer函数和UnsafeMutablePointer结构来检索数据,如下所示:
var result: AnyObject? var status = withUnsafeMutablePointer(&result) { SecItemCopyMatching(queryAttributes,UnsafeMutablePointer($0)) } if status == errSecSuccess { if let data = result as NSData? { if let string = NSString(data: data,encoding: NSUTF8StringEncoding) { // ... } } }
它发布(Fastest [-O])构建工作正常。