Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: from nullable pointer 'NSURL * _Nullable' to non-nullable pointer type 'NSURL * _Nonnull'

I'm trying to convert NSURL * _Nullable to non-nullable. By any chance any of you knows how can I do this?.

Here is some context:

I'm trying to use ios-oauthconsumer, and I'm trying to initialize some objects:

self.requestTokenRequest = [[OAMutableURLRequest alloc] initWithURL:_requestTokenUrl
                                                               consumer:_consumer
                                                                  token:nil
                                                                  realm:nil
                                                      signatureProvider:nil
                                                                  nonce:nil
                                                              timestamp:nil];

They are been initialize by this init:

- (id)initWithURL:(NSURL *)aUrl
         consumer:(OAConsumer *)aConsumer
            token:(OAToken *)aToken
            realm:(NSString *)aRealm
signatureProvider:(id<OASignatureProviding>)aProvider
            nonce:(NSString *)aNonce
        timestamp:(NSString *)aTimestamp {
    if ((self = [self initWithURL:aUrl consumer:aConsumer token:aToken realm:aRealm signatureProvider:aProvider])) {
      nonce = [aNonce copy];
      timestamp = [aTimestamp copy];
    }

    return self;
}

if I po I get this:

enter image description here

and I try to use that object. I get this warning:

enter image description here I'll really appreciate if any of you knows how to convert _Nullable to non-Nullable object.

like image 748
user2924482 Avatar asked May 02 '17 22:05

user2924482


1 Answers

This is quite straightforward = all you need to do is assign the _requestTokenUrl to a variable before using it in a method call. This should be enough :

NSURL *url = _requestTokenUrl;
self.requestTokenRequest = [[OAMutableURLRequest alloc] initWithURL:url
                                                           consumer:_consumer
                                                              token:nil
                                                              realm:nil
                                                  signatureProvider:nil
                                                              nonce:nil
                                                          timestamp:nil];

Now, bear in mind that this simply silences the warning. Since your _requestTokenUrl is nullable and the method expects a nonnull object, you should check if _requestTokenUrl is actually non-nil. So the more appropriate version would look like this :

NSURL *url = _requestTokenUrl;
if (url != nil) {
    self.requestTokenRequest = [[OAMutableURLRequest alloc] initWithURL:url
                                                               consumer:_consumer
                                                                  token:nil
                                                                  realm:nil
                                                      signatureProvider:nil
                                                                  nonce:nil
                                                              timestamp:nil];
} else {
    // `_requestTokenUrl` was nil, act accordingly
}

The assignement to a variable is still required though.

like image 101
Losiowaty Avatar answered Oct 21 '22 20:10

Losiowaty