How to convert NSNull to nil in Swift?

前端 未结 2 1159
后悔当初
后悔当初 2020-12-17 15:13

I want to fetch JSON data from my server and manipulate it upon launch. In Objective-C, I have used this #define code to convert NSNull to ni

2条回答
  •  鱼传尺愫
    2020-12-17 15:50

    I would recommend, instead of using a custom conversion function, just to cast the value using as?:

    people.age = peopleDict["age"] as? Int
    

    If the value is NSNull, the as? cast will fail and return nil.

提交回复
热议问题