Xcode 7.0 Swift 更新问题
Xcode 7.0 Swift Update Problems
我正在尝试更新我的项目以使用 Xcode 7.0,在更新我的 Swift 项目后,我在这一行遇到了一个我不明白的错误。
let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
错误是
"Call can throw, but it is not marked with 'try' and the error is not handled"
我的项目文件中也出现了这两个错误...
"linker command failed with exit code 1 (use -v to see invocation)"
和
"error: cannot parse the debug map for "/Users/MattFiler/Library/Developer/Xcode/DerivedData/ePlanner-cqwzlxqgpwaloubjgnzdlomjkfea/Build/Intermediates/SwiftMigration/ePlanner/Products/Debug-iphonesimulator/ePlannerTests.xctest/ePlannerTests": 没有那个文件或目录"
如果它抛出错误,您需要尝试捕获。
do {
let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
//...
}
catch {
}
试试这个代码:
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options: .MutableContainers ) as! NSDictionary
// Use jsonData here
} catch {
print("Well something happened: \(error)")
}
你需要 try
关键字,因为 NSJSONSerialization.JSONObjectWithData
现在如果自 Swift 以来出现错误,则会抛出错误 2. 抛出函数需要用 try
或try!
.
此外,您还需要 do { ... } catch
来捕获可能发生的任何错误。这将捕获错误并处理它。
您可能想阅读 changes in Swift 2 以了解发生这种情况的原因。 WWDC 视频也会很有帮助。
我正在尝试更新我的项目以使用 Xcode 7.0,在更新我的 Swift 项目后,我在这一行遇到了一个我不明白的错误。
let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
错误是
"Call can throw, but it is not marked with 'try' and the error is not handled"
我的项目文件中也出现了这两个错误...
"linker command failed with exit code 1 (use -v to see invocation)"
和
"error: cannot parse the debug map for "/Users/MattFiler/Library/Developer/Xcode/DerivedData/ePlanner-cqwzlxqgpwaloubjgnzdlomjkfea/Build/Intermediates/SwiftMigration/ePlanner/Products/Debug-iphonesimulator/ePlannerTests.xctest/ePlannerTests": 没有那个文件或目录"
如果它抛出错误,您需要尝试捕获。
do {
let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
//...
}
catch {
}
试试这个代码:
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(urlData!, options: .MutableContainers ) as! NSDictionary
// Use jsonData here
} catch {
print("Well something happened: \(error)")
}
你需要 try
关键字,因为 NSJSONSerialization.JSONObjectWithData
现在如果自 Swift 以来出现错误,则会抛出错误 2. 抛出函数需要用 try
或try!
.
此外,您还需要 do { ... } catch
来捕获可能发生的任何错误。这将捕获错误并处理它。
您可能想阅读 changes in Swift 2 以了解发生这种情况的原因。 WWDC 视频也会很有帮助。