Re: When can String.enumerateSubstringsInRange ever pass in NIL?
Re: When can String.enumerateSubstringsInRange ever pass in NIL?
- Subject: Re: When can String.enumerateSubstringsInRange ever pass in NIL?
- From: Quincey Morris <email@hidden>
- Date: Sun, 29 May 2016 19:54:34 -0700
- Feedback-id: 167118m:167118agrif8a:167118shxMuJh5vW:SMTPCORP
On May 29, 2016, at 19:16 , Daryle Walker <email@hidden> wrote:
>
> Since I am using the substring and not including “.SubstringNotRequired”, I can just remove the “guard” block, tack on a “!” to substring’s identifier, and be done with it, right?
The problem is there’s no API contract that says exactly when (and when not) the string will be nil. I would suggest, therefore, that you don’t place any reliance on it not being nil in your use cases, but rather deal with it like this (at the top of the closure):
let substring = substring ?? ""
That is, shadow the parameter with a non-optional String variable, and assume an empty string when the parameter is nil. Note that the empty string will be either a tagged pointer or a shared global constant, so there’s no real performance issue in doing this. After that, you don’t care whether it was nil or not.
This is consistent with what would happen in Obj-C if you didn’t check for nil, since in Obj-C a nil pointer will appear to be an empty string in many contexts.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden