Re: How to fix "misuse of 'nonnull'" warnings in Xcode 7.3?
Re: How to fix "misuse of 'nonnull'" warnings in Xcode 7.3?
- Subject: Re: How to fix "misuse of 'nonnull'" warnings in Xcode 7.3?
- From: Quincey Morris <email@hidden>
- Date: Thu, 25 Feb 2016 14:08:12 -0800
- Feedback-id: 167118m:167118agrif8a:167118sKFmJPJ0u8:SMTPCORP
On Feb 25, 2016, at 13:31 , Jens Alfke <email@hidden> wrote:
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];
I think the answer is that you do have to add a cast in this situation (though it’s unsafe — you’re not checking that it’s non-nil, you just *believe* it isn’t nil). However, it looks like you can use the simpler form:
hostArray addObject: (NSString*)url.host]; // apparently converts it back to “nullability unspecified"
This strikes me as similar as converting between NSUInteger and NSInteger, where you’re forced to use a cast, even when you know that the value is >= 0. 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];
Again, if you write:
NSString* host = url.host; if (host) [hostArray addObject: host];
it’s basically no clunkier than what you have to do with ‘if let’ in Swift.
I can imagine writing clever macros to simplify this, something like #define NOTNULL(X) ((__typeof(X) _Nonnull)(X))
But if you’re just going to cast away the issue, without an actual check for nil, then you may as well turn the build setting back off. What about something like:
#define NOTNULL(X) ((X) == nil ? (__typeof(X) (void*) 0xdeafbeef) : (__typeof(X) _Nonnull)(X))
|
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden