Swift: unrecognized selector sent to instance, how to detect null value?
<p>I am trying to use <a href="https://github.com/microsoftgraph/msgraph-sdk-ios" rel="nofollow">Microsoft Graph iOS SDK</a> and followed the official <a href="https://github.com/microsoftgraph/iOS-swift-connect-sample" rel="nofollow">code sample</a>. </p>

<p>The following code snippet is to get basic profile of all users in the organization: </p>

<pre><code>private func getContactInfo(){
    self.graphClient.users().request().getWithCompletion{
        (collection: MSCollection?, request:MSGraphUsersCollectionRequest?, error: NSError?) in
        if let users = collection {
            for user: MSGraphUser in users.value as! [MSGraphUser] {
                print(user)
                print(String(user.surname.dynamicType))
                print(user.mobilePhone)
            }
        }
    }
}
</code></pre>

<p>Output Result:</p>

<pre><code>{
    businessPhones =     (
    );
    displayName = "Boss";
    givenName = "Jack";
    id = "30fb78ff-522f-45e7-a9cd-75ba8ee2eca6";
    jobTitle = "&lt;null&gt;";
    mail = "boss@abc.net";
    mobilePhone = "&lt;null&gt;";
    officeLocation = "&lt;null&gt;";
    preferredLanguage = "&lt;null&gt;";
    surname = "\U8a79";
    userPrincipalName = "boss@abc.net";
}

ImplicitlyUnwrappedOptional&lt;String&gt;
</code></pre>

<p>And runtime exception happened </p>

<pre><code>2016-06-05 17:02:55.302 ABC[76976:915052] -[NSNull _fastCStringContents:]: unrecognized selector sent to instance 0xb59398
2016-06-05 17:02:55.305 ABC[76976:915052] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull _fastCStringContents:]: unrecognized selector sent to instance 0xb59398'
*** First throw call stack:
(
    0   CoreFoundation                      0x0092c494 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x02640e02 objc_exception_throw + 50
...
</code></pre>

<p>When I try to print the null value like mobilePhone, runtime exception occurred. How to detect null value to avoid crashing? Thank you.</p>

<p><strong>Add: <a href="https://github.com/microsoftgraph/msgraph-sdk-ios/blob/df744cfd6f78b4e977ed3b7acb9b5338ee56341b/MSGraphSDK/MSGraphCoreSDK/model/MSGraphUser.m" rel="nofollow">MSGraphUser.m</a> snippet</strong> used in this case,</p>

<pre><code>@interface MSGraphUser()
{
...
NSString* _mobilePhone;
...
- (NSString*) mobilePhone
{
    return self.dictionary[@"mobilePhone"];
}
...
</code></pre>