使用Swift查询iOS钥匙串
发布时间:2020-12-14 05:50:59 所属栏目:百科 来源:网络整理
导读:我被卡通转换钥匙串查询结果使用Swift。 我的要求似乎在工作: let queryAttributes = NSDictionary(objects: [kSecClassGenericPassword,"MyService","MyAccount",true],forKeys: [kSecClass,kSecAttrService,kSecAttrAccount,kSecReturnData])dispatch_asy
我被卡通转换钥匙串查询结果使用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])构建工作正常。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |