在解析swift中的Json时,无法将'NSNull'类型的值转换为'NSString'

在解析swift中的Json时,无法将'NSNull'类型的值转换为'NSString'

问题描述:

我有以下课程

class BannerResponse : NSObject{

    let  URL                = "Url";
    let  CONTACT_NO         = "ContactNo";
    let  IMAGE              = "Image";
    let  BIG_IMAGE          = "BigImage";
    let  ID                 = "Id";
    let  TITLE              = "Title";
    let  NO_VIEW            = "NoView";
    let  START_DATE         = "StartDate";
    let  END_DATE           = "EndDate";


    var url:String;
    var contactNo:String;
    var image:String;
    var bigImage:String;
    var title:String;
    var id:Int;
    var noView:Int;
    var startDate:String;
    var endDate:String;

    init(data : NSDictionary){

        url         = data[URL] as! String;
        contactNo   = data[CONTACT_NO] as! String;
        image       = data[IMAGE] as! String;
        bigImage    = data[BIG_IMAGE] as! String;
        title       = data[TITLE] as! String;
        id          = data[ID] as! Int;
        noView      = data[NO_VIEW] as! Int;
        startDate   = data[START_DATE] as! String;
        endDate     = data[END_DATE] as! String;
    }
}

当我运行代码时,出现以下错误

when I run the code, I got the following error

Could not cast value of type 'NSNull' (0x10a85f378) to 'NSString' (0x109eccb20).

编辑

do {

                            if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary{

                                onSuccess(BannerResponse(data: json))
                            }
                        } catch {

                            onFail()
                        }


您的一个数据[SOME_KEY] 类型为 NSNull 而不是 String ,因为你强制转换为 String 使用应用程序崩溃。

One of your data[SOME_KEY] is of type NSNull instead of String and because you're forcing the cast to String by using ! the app crashes.

你可以做以下两件事之一:

You can do one of two things:


  1. 改变你的变量BannerResponse类是可选的。在 init $ c $中设置值时,使用而不是 c>方法。像这样:

  1. Change your variables on the BannerResponse class to be optional. And use ? instead of ! when setting the values in your init method. Like this:

`

var title: String?

init(data: NSDictionary) 
{
    self.title = data[TITLE] as? String
}


  1. 在设置值时使用而不是 init 方法但每当 dict [SOME_KEY] 为nil或不是预期类型时设置默认值。这看起来像这样:

  1. Use ? instead of ! when setting the values in your init method but set a default value whenever dict[SOME_KEY] is nil or is not the expected type. This would look something like this:

`

if let title = data[TITLE] as? String 
{
    self.title = title
}
else
{
    self.title = "default title"
}

// Shorthand would be: 
// self.title = data[TITLE] as? String ?? "default title"

我想第三件事是确保服务器永远不会发送空值。但这是不切实际的,因为从来没有这样的事情。您应该编写客户端代码,以假设JSON中的每个值都可以为null或意外类型。

And I guess a third thing is ensure that the server never sends down null values. But this is impractical because there's no such thing as never. You should write your client-side code to assume every value in the JSON can be null or an unexpected type.