无法将“NSNull”类型的值转换为“NSNumber”

时间:2017-05-29 17:53:22

标签: ios mysql json swift

我有一个iOS应用程序,它与API接口以获取数据。 到目前为止一切正常。但是,似乎该应用程序在登录后崩溃。这是我的代码:

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行:

enter image description here

我已经尝试了答案here。 我也检查了phpmyadmin / MySQL数据库,但我没有看到任何可能导致问题的原因。

enter image description here

1 个答案:

答案 0 :(得分:1)

使用let或guard语句进行检查。举个例子:

if let link = dict["somethingToCheck"] as? String 
{ 
    //if code execute
} 
else {
    //else code execute
}