因此,似乎有一些方法已经改变,这些方法在我当前的代码库中已经破坏了.我目前收到以下错误:
Cannot convert the expression’s type ‘(CFAllocator!,data: @lvalue NSData)’ to type ‘CFData!’
这是相关的代码:
let mainbun = NSBundle.pathForResource("mainkey",ofType: "der",inDirectory: "/myapppath") var key: NSData = NSData(base64EncodedString: mainbun!,options: nil)! var turntocert: SecCertificateRef = SecCertificateCreateWithData(kcfAllocatorDefault,data: key)
我有它使用桥接头,但我仍然希望能够直接在swift中创建证书引用.
更新:
这很有效
var bundle: NSBundle = NSBundle.mainBundle() var mainbun = bundle.pathForResource("keyfile",ofType: "der") var key: NSData = NSData(contentsOfFile: mainbun!)! var turntocert: SecCertificateRef = SecCertificateCreateWithData(kcfAllocatorDefault,key).takeRetainedValue()
在Swift中,SecCertificateCreateWithData返回Unmanaged类型.您需要使用takeRetainedValue()获取非托管引用的值.
let mainbun = NSBundle.pathForResource("mainkey",options: nil)! var turntocert: SecCertificateRef = SecCertificateCreateWithData(kcfAllocatorDefault,key).takeRetainedValue()
您遇到的核心问题是将CFData转换为NSData. See this question