我被卡通转换钥匙串查询结果使用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])构建工作正常。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。