简体   繁体   中英

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:

在此输入图像描述

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

在此输入图像描述 I'll really appreciate if any of you knows how to convert _Nullable to non-Nullable object.

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.

Based on the warning, it looks like your "request.URL" is already an NSURL object. The compiler is complaining because you're trying to use a method that wants a string for the argument. Why can't you just use request.URL instead of trying to create another NSURL object from it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM