我正在尝试使用 TCP SSL 连接 openssl s_server
和 iOS 客户端(另外,我得到 another issue with this,可能与此相关)。现在我正在尝试使用这个 sample code from github为此:
let mainbun = NSBundle.mainBundle().pathForResource("pd-test", ofType: "cer")
var key: NSData = NSData.dataWithContentsOfMappedFile(mainbun!)! as NSData
var cert:SecCertificateRef =
SecCertificateCreateWithData(kCFAllocatorDefault, key).takeRetainedValue()
var err:OSStatus = noErr
let secDict = NSDictionary(
objects: [kSecClassCertificate,cert],
forKeys: [kSecClass, kSecValueRef]
)
SecItemAdd(secDict as CFDictionaryRef, nil);
但得到错误:
ViewController.swift:67:74: 'AnyObject' is not convertible to 'NSData'; did you mean to use 'as!' to force downcast?
ViewController.swift:69:9: Value of type 'SecCertificate?' has no member 'takeRetainedValue'
ViewController.swift:75:23: Value of type 'CFString' does not conform to expected element type 'NSCopying'
为什么我无法构建有效代码(我看到很多这样的示例,它们都抛出几乎相同的错误,例如 this )?我该如何解决?
最佳答案
Why I can't build valid code
这是因为 Swift 语言在不断发展;您使用的 Swift 版本比编写这些示例时使用的版本更新。它当时是有效的 Swift,但它对 您的 版本的 Swift 无效。
(如果您要下载 Xcode 8 seed 6,第一行甚至无法编译;现在没有 NSBundle
这样的东西,没有这样的东西mainBundle()
,并且没有 pathForResource
这样的东西。这表明 Swift 语言可以发生多么彻底的改变。)
关于ios - 不同的有效代码示例抛出相同的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39235716/