我正在尝试实现 CFDictionaryGetValue(),如 this appledoc list 3 所示:

server = CFDictionaryGetValue(credentialDict, kSecAttrServer);
userName = CFDictionaryGetValue(credentialDict, kSecAttrAccount);
password = CFDictionaryGetValue(credentialDict, kSecSharedPassword);

我是这样实现的
let userName = CFDictionaryGetValue(credentialDict, kSecAttrAccount)
let password = CFDictionaryGetValue(credentialDict, kSecSharedPassword)

但我收到错误“无法使用类型为“(CFDictionaryRef,CFStringRef)”的参数列表调用“CFDictionaryGetValue”

我不明白这与苹果文档的实现方式有何不同。

谢谢

最佳答案

是的,雷达 here 中提到的代码存在问题

我为您找到了解决方法:

  let unsafeCred = CFArrayGetValueAtIndex(credentials, 0)
  let credential: CFDictionaryRef = unsafeBitCast(unsafeCred, CFDictionaryRef.self)
  let dict: Dictionary<String, String> = credential as! Dictionary<String, String>
  let username = dict[kSecAttrAccount as String]
  let password = dict[kSecSharedPassword.takeRetainedValue() as! String]

更新
SecRequestSharedWebCredential("www.reddit.com", .None)  {
  credentials, error in
    if CFArrayGetCount(credentials) > 0 {
      let dict = unsafeBitCast(
               CFArrayGetValueAtIndex(credentials, 0),
               CFDictionaryRef.self) as Dictionary
    let username = dict[kSecAttrAccount as String]
    let password = dict[kSecSharedPassword as String]
    login(username, password)
  }
}

请注意这里的区别:

iOS8
var kSecSharedPassword: Unmanaged<AnyObject>!
func SecRequestSharedWebCredential(_ fqdn: CFString!,
                                   _ account: CFString!,
                                   _ completionHandler: ((CFArray!,
                                            CFError!) -> Void)!)

iOS9
let kSecSharedPassword: CFString
func SecRequestSharedWebCredential(_ fqdn: CFString?,
                                   _ account: CFString?,
                                   _ completionHandler: (CFArray?, CFError?) -> Void)

这里是 swift2/iOS9/xcode7 的所有 changes

关于ios - CFDictionaryGetValue 不能用 CFStringRef 调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32316037/

10-11 00:32