无法将类型'NSNull'的值转换为'NSNumber'
问题描述:
我有一个iOS应用程序与API进行接口以获取数据。 到现在为止一切工作正常。但是,似乎该应用在登录后崩溃。这里是我的代码:无法将类型'NSNull'的值转换为'NSNumber'
func Evaluate(response:NSDictionary)
{
ClientWorkoutID = response["clientworkout_id"] as! Int
ProgramUserID = response["clientworkout_userid"] as! Int
ProgramID = response["clientworkout_programid"] as! Int
WorkoutID = response["clientworkout_workoutid"] as! Int
WorkoutWeek = response["clientworkout_week"] as! Int
WorkoutDay = response["clientworkout_day"] as! Int
DayType = response["clientworkout_daytype"] as! Int
ExType = response["clientworkout_extype"] as! Int
MuscleGroup = response["clientworkout_musclegroup"] as! Int
ExName = response["clientworkout_exname"] as? String ?? ""
ExID = response["clientworkout_exid"] as! Int
Order = 7 * (WorkoutWeek - 1) + WorkoutDay
SetCount = response["clientworkout_sets"] as! Int
Sets = response["clientworkout_setcontent"] as? String ?? ""
SetInfo = response["clientworkout_setinfo"] as? String ?? ""
ExerciseProgress = response["clientworkout_exprogress"] as! Double
DayProgress = response["clientworkout_dayprogress"] as! Double
}
的错误是在该行129:
我已经试过了答案here。 我也检查了phpmyadmin/MySQL数据库,但我没有看到任何可能导致问题的东西。
答
的使用,如果让或保护语句来检查。举个例子:
if let link = dict["somethingToCheck"] as? String
{
//if code execute
}
else {
//else code execute
}
这仅仅意味着“clientworkout_exid”中的JSON值是** **空,而不是一个号码......请注意,如果服务器没有每个强制类型转换可能会崩溃准确地发送你所期望的。 –