Re: Instantiate NSString from NSURL in Swift
Re: Instantiate NSString from NSURL in Swift
- Subject: Re: Instantiate NSString from NSURL in Swift
- From: Marco S Hyman <email@hidden>
- Date: Mon, 23 Feb 2015 11:48:47 -0800
> On Feb 23, 2015, at 11:25 AM, Quincey Morris <email@hidden> wrote:
>
> On Feb 23, 2015, at 11:06 , Marco S Hyman <email@hidden> wrote:
>>
>> let foo = theUrl?.host?.lowercaseString
>> let bar = NSString(string: foo!)
>> let baz = NSString(string: theUrl?.host?.lowercaseString!)
>>
>> You'd think baz would be the same as bar.
>
> I’m not so sure. a?.b! is (at least informally) ambiguous, for a?.(b!) or (a?.b)!
>
> If the compiler interprets it as the first alternative, then I think you’d get what the OP observed, assuming ‘lowercase’ doesn’t return an optional result.
Arrgghhhh... the compiler (and I missed the forest by concentrating on the
lowercaseString tree) is correct. It's not an order of evaluation issue.
lowerCaseString is a non optional property. Therefore it makes no sense to
write someString.lowercaseString? or someString.lowercaseString!
NSString(string: String) doesn't take an optional.
some?.optional?.lowercaseString evaluates to an optional type even though the
final component isn't optional.
Put all that together and NSString(string: some?.variable?.lowercaseString)
is an error, you're passing an optional type to an initializer that doesn't
take an optional type. I'd use
if let lcstring = some?.variable?.lowercaseString {
// use lcstring safely here
}
_______________________________________________
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