【发布时间】:2016-03-25 05:17:26
【问题描述】:
我正在关注here 找到的健康工具包教程
它使用的是旧版本的 Swift (1.2),我正在尝试将其转换为最新版本(撰写本文时为 Swift 2.2)
该错误表明我需要进行do, catch, and try 处理,但它仍然抛出相同的错误Call can throw, but it is not marked with 'try' and the error not handled?
我在下面添加了我的尝试。使用 Swift 2.2,我还需要使用最新的 if error != nil 检查错误吗?还是现在在do, catch, try 处理中处理?
var error:NSError?
var age:Int?
// 1. Request birthday and calculate age
if let birthDay = healthKitStore.dateOfBirth()
{
do {
let today = NSDate()
let calendar = NSCalendar.currentCalendar()
let differenceComponents = NSCalendar.currentCalendar().components(.CalendarUnitYear, fromDate: birthDay, toDate: today, options: NSCalendarOptions(0) )
try! age = differenceComponents.year
} catch {
print(error)
}
}
if error != nil {
print("Error reading Birthday: \(error)")
}
【问题讨论】:
标签: ios swift healthkit swift2.2