我用 Xcode6 Beta 2 编写了一个 Swift 应用程序,它使用 CFNetwork 类(例如NSURLRequest
和NSHTTPURLResponse
.
该应用程序在 iOS 8 上运行良好,但当我尝试在 iOS 7 设备或运行 iOS 7 的模拟器上运行它时,启动应用程序时出现以下错误:
dyld: Symbol not found: _OBJC_CLASS_$_NSHTTPURLResponse
Referenced from: /Users/patrick/Library/Developer/CoreSimulator/Devices/B0A61F43-A67C-4803-8F5D-77C3972107BE/data/Applications/E0C7C89F-9EEE-4893-BE5B-FCC224F2855D/CheckYourWeather.app/CheckYourWeather
Expected in: /Applications/Xcode6-Beta2.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/Frameworks/CFNetwork.framework/CFNetwork
in /Users/patrick/Library/Developer/CoreSimulator/Devices/B0A61F43-A67C-4803-8F5D-77C3972107BE/data/Applications/E0C7C89F-9EEE-4893-BE5B-FCC224F2855D/CheckYourWeather.app/CheckYourWeather
我做了一些研究,发现这是一个链接问题。不过,我知道我正在使用的类已经在 iOS 7 中可用。
我还尝试CFNetwork.framework
在项目设置中将其添加到框架中并将其设置为可选,这只会导致应用程序在运行时崩溃。
对我来说令人困惑的部分是:我编写了一个测试应用程序,并将我在主应用程序中使用的代码粘贴到其中,它工作得很好。因此,代码可能不是问题。
从模拟器/设备中删除应用程序,清理项目并删除 Xcode 的 DerivedData 并没有解决问题。
更新:
这是导致崩溃的代码:
extension NSURLRequest {
class func plainPostRequest(url: NSURL, httpBody: String) -> NSURLRequest {
let urlRequest = NSMutableURLRequest(URL: url)
urlRequest.HTTPMethod = "POST"
urlRequest.HTTPBody = httpBody.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
urlRequest.setValue("text/plain", forHTTPHeaderField: "Content-Type")
return urlRequest
}
}
但这只是一个例子。任何使用 CFNetwork 类都会导致应用程序在启动期间崩溃。