ios - 不同的有效代码示例抛出相同的错误

标签 ios swift xcode ssl

我正在尝试使用 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/

相关文章:

ios - -[AFHTTPRequestSerializer requestWithMethod :URLString:parameters:error:] 中的断言失败

ios - 使用核心数据,我得到一个错误 'nib but didn’ t get a UICollectionView。

ios - Xcode 内存图调试图标未显示

ios - 如何指定 iOS 应用运行的区域

javascript - typeahead.js iOS Safari 与独立 Web 应用程序的差异

ios - 使用 Swift 在 iOS 11 中向 NSAttributedString 添加删除线

ios - 多个推送 Segues 到 Storyboard 中的一个 View Controller

ios - 如何将 UILabel 转换为字符串?

ios - 如何让 UIButton 在按下时缩小为 UIActivityIndi​​cator?

ios - Crashlytics 不适用于 xcode 10、iOS 12