On Thu, 25 Feb 2016 13:31:45 -0800, Jens Alfke said:
>As an experiment I tried turning on the new “misuse of ’nonnull'”
>warning in Xcode 7.3, and got a ton of warnings. They all make sense,
>but assuming I were going to correct my code, I don’t know the best way
>go about it. For example, here’s a real warning reported in my project:
> [hostArray addObject: url.host]; // Warning!
>Here the problem is that NSURL.host returns a nullable NSString, but -
>[NSArray addObject:]’s parameter is not nullable.
>
>If I know that the URLs I’m dealing with all have hostnames in them, I
>could just add an “!” after `url.host`. Except I can’t because this is
>Obj-C, not Swift. What’s the equivalent? Do I have to add a cast?
> [hostArray addObject: (NSString* _Nonnull)url.host];
>
>If I don’t want to trust that the URL has a host, I can use `if let` to
>test it. But again, what’s the Obj-C equivalent? It seems like I’d need
> NSString* _Nonnull host = url.host;
> if (host)
> [hostArray addObject: host];
Actually, seems:
NSString* host = url.host;
[hostArray addObject: host];
is enough to "fix" the warning. ie add a temporary variable. You could throw
in an assert too:
NSString* host = url.host;
assert(host);
[hostArray addObject: host];
Cheers,
--
____________________________________________________________
Sean McBride, B. Eng [email protected]
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com
This email sent to [email protected]