Re: Simple Swift question
Also the nifty new defer statement works well along with multiple guards if you need to do any cleanup when exiting the scope. The combination is great. Kevin On 29 Jun 2015, at 23:43, Rick Mann rm...@latencyzero.com wrote: On Jun 29, 2015, at 15:35 , Jens Alfke j...@mooseyard.com wrote: The unsightly nesting of the if-lets can be avoided with the nifty new ‘guard’ statement in Swift 2. Ah, yes, that's what I should be using. Thanks! -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/ktam%40yvs.eu.com This email sent to k...@yvs.eu.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 5:53 PM, Rick Mann rm...@latencyzero.com wrote: If you are using Swift 2.0, you can use the new `guard let` to get optional checking without the cascade of indentation. guard let url = NSURL(string: some url) else { /* handle failure and either halt or return */ } // URL is now in scope and non-optional. let req = NSURLRequest(URL: url) I am, but this may not work as well if there are additional steps after this one that should run anyway. Can I enclose the bit in braces to limit the scope of the break? Probably not, huh? e.g.: { guard let url = ... else { break } use url } { ...other init... } Look into the “defer” keyword. Charles ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 15:23 , Rick Mann rm...@latencyzero.com wrote: Oh, I think I figured it out. NSURL(string:) is optional, but NSURLRequest(URL:) can't. Very unexpected, and the error message I was getting did not clue me in. - How are you supposed to do simple things like this, succinctly? let url = NSURL(string: some url) let req = NSURLRequest(URL: url) self.webView.loadRequest(req) It’s not clear what the solution (or problem) was, since you seem to have left some words out. In general, though, the pattern for this sort of thing is: if let a = somethingReturningOptionalA (), let b = somethingReturningOptionalB (a), let c = somethingReturningOptionalC (b) { somethingUsingC (c) } Note that each let clause can have an optional ‘where’ condition: if let a = somethingReturningOptionalA (), let b = somethingReturningOptionalB (a) where b.size 100, … and you can start with a purely boolean clause: if onePlusOne == Two, let a = somethingReturningOptionalA (), … Alternatively, you can invert everything with ‘guard’ statements, picking off the nil values one by one, without any nesting, but that’s only appropriate when you can actually exit the scope if the guard fails: guard let a = somethingReturningOptionalA () else { break } guard let b = somethingReturningOptionalB (a) else {break } guard let c = somethingReturningOptionalC (b) else {break } somethingUsingC (c) ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 15:43 , Greg Parker gpar...@apple.com wrote: On Jun 29, 2015, at 3:18 PM, Rick Mann rm...@latencyzero.com wrote: How are you supposed to do simple things like this, succinctly? let url = NSURL(string: some url) let req = NSURLRequest(URL: url) self.webView.loadRequest(req) This, obviously, doesn't work, since the results are optionals, and so need to be unwrapped. But it gets rapidly ridiculous with a bunch of nested if-lets (imagine you had more than just two operations to get from here to there). What's the right way to do this? If you know that the call cannot actually fail (for example, NSURL(string:) with a valid hardcoded URL) then you can simply force-unwrap. If you are wrong then the program will halt at runtime. let url = NSURL(string: some url)! Yep, part of the problem was I was expecting the two type to have the same construction semantics, which isn't the case. If you are using Swift 2.0, you can use the new `guard let` to get optional checking without the cascade of indentation. guard let url = NSURL(string: some url) else { /* handle failure and either halt or return */ } // URL is now in scope and non-optional. let req = NSURLRequest(URL: url) I am, but this may not work as well if there are additional steps after this one that should run anyway. Can I enclose the bit in braces to limit the scope of the break? Probably not, huh? e.g.: { guard let url = ... else { break } use url } { ...other init... } Quincey Morris pointed out that I can add multiple let clauses with commas. I tried using , and even a where clause, but was being tripped up by NSURL being optional and NSURLRequest not. Here's a contrived example, though. Would this be gross? let url = NSURL(string: http://my.matterport.com/show?m=YJrquK9RCiE;) if let url2 = NSURL(string: url!.absoluteString) where url != nil { ... } Oh, I think I figured it out. NSURL(string:) is optional, but NSURLRequest(URL:) can't. Very unexpected, and the error message I was getting did not clue me in. You can often improve diagnostics by adding the explicit types that you expect each variable to have. That can help pinpoint the place where your understanding of the code and the compiler's understanding of the code don't match. For example, in your code above, writing `let url: NSURL = …` moves the error message from the NSURLRequest line to the NSURL line. That sure seems to defeat the purpose of a highly publicized feature of the language. Or are you suggesting just doing it temporarily to figure out what the compiler really wants? The compiler's message was, Could not find an overload for 'init' that accepts the supplied arguments. Could it instead say NSURLRequst.init(URL:) does not return Optional type? The confusion stems from it talking about what the init *accepts*, rather than what it returns. -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 3:23 PM, Rick Mann rm...@latencyzero.com wrote: Oh, I think I figured it out. NSURL(string:) is optional, but NSURLRequest(URL:) can't. Very unexpected Creating an NSURL from a string can fail, since not all strings are valid URLs. But creating an NSURLRequest isn’t failable; you always get an instance. But it gets rapidly ridiculous with a bunch of nested if-lets To be fair, this would have occurred in Objective-C too, assuming you were properly testing for nil everywhere. Swift just doesn’t let you get away with closing your eyes and pretending that [NSURL URLWithString:] can’t return nil. (If “some url” represents a constant string that you happen to know is a valid URL, then you can just put an “!” after it to avoid the optional. It’s perfectly appropriate in this place; it’s the moral equivalent of adding an NSAssert that the NSURL isn’t nil.) The unsightly nesting of the if-lets can be avoided with the nifty new ‘guard’ statement in Swift 2. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 3:18 PM, Rick Mann rm...@latencyzero.com wrote: How are you supposed to do simple things like this, succinctly? let url = NSURL(string: some url) let req = NSURLRequest(URL: url) self.webView.loadRequest(req) This, obviously, doesn't work, since the results are optionals, and so need to be unwrapped. But it gets rapidly ridiculous with a bunch of nested if-lets (imagine you had more than just two operations to get from here to there). What's the right way to do this? If you know that the call cannot actually fail (for example, NSURL(string:) with a valid hardcoded URL) then you can simply force-unwrap. If you are wrong then the program will halt at runtime. let url = NSURL(string: some url)! If you are using Swift 2.0, you can use the new `guard let` to get optional checking without the cascade of indentation. guard let url = NSURL(string: some url) else { /* handle failure and either halt or return */ } // URL is now in scope and non-optional. let req = NSURLRequest(URL: url) Oh, I think I figured it out. NSURL(string:) is optional, but NSURLRequest(URL:) can't. Very unexpected, and the error message I was getting did not clue me in. You can often improve diagnostics by adding the explicit types that you expect each variable to have. That can help pinpoint the place where your understanding of the code and the compiler's understanding of the code don't match. For example, in your code above, writing `let url: NSURL = …` moves the error message from the NSURLRequest line to the NSURL line. -- Greg Parker gpar...@apple.com Runtime Wrangler ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Simple Swift question
Oh, I think I figured it out. NSURL(string:) is optional, but NSURLRequest(URL:) can't. Very unexpected, and the error message I was getting did not clue me in. - How are you supposed to do simple things like this, succinctly? let url = NSURL(string: some url) let req = NSURLRequest(URL: url) self.webView.loadRequest(req) This, obviously, doesn't work, since the results are optionals, and so need to be unwrapped. But it gets rapidly ridiculous with a bunch of nested if-lets (imagine you had more than just two operations to get from here to there). What's the right way to do this? -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Simple Swift question
How are you supposed to do simple things like this, succinctly? let url = NSURL(string: some url) let req = NSURLRequest(URL: url) self.webView.loadRequest(req) This, obviously, doesn't work, since the results are optionals, and so need to be unwrapped. But it gets rapidly ridiculous with a bunch of nested if-lets (imagine you had more than just two operations to get from here to there). What's the right way to do this? -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 5:18 PM, Rick Mann rm...@latencyzero.com wrote: But it gets rapidly ridiculous with a bunch of nested if-lets (imagine you had more than just two operations to get from here to there). Have you looked at Swift 2.0 yet? It addresses that forced Arrow Anti-Pattern issue quite nicely. Charles ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 15:36 , Charles Srstka cocoa...@charlessoft.com wrote: On Jun 29, 2015, at 5:18 PM, Rick Mann rm...@latencyzero.com wrote: But it gets rapidly ridiculous with a bunch of nested if-lets (imagine you had more than just two operations to get from here to there). Have you looked at Swift 2.0 yet? It addresses that forced Arrow Anti-Pattern issue quite nicely. This is in Swift 2.0. Can you tell me how it does this? -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 15:35 , Jens Alfke j...@mooseyard.com wrote: The unsightly nesting of the if-lets can be avoided with the nifty new ‘guard’ statement in Swift 2. Ah, yes, that's what I should be using. Thanks! -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 16:30 , Jens Alfke j...@mooseyard.com wrote: On Jun 29, 2015, at 4:23 PM, Rick Mann rm...@latencyzero.com wrote: http://pastebin.com/Q7sBWBnN url is unwrapped already, and I'm (wrongly) trying to unwrap the NSURLRequest. But it's not complaining about the arguments to NSURLRequest.init(URL:). The error I get with Xcode 7 beta 2 is error: operand of postfix '?' should have optional type; type is 'NSURLRequest' if let req = NSURLRequest(URL: url)? ~~^ So it looks like this has been fixed already. Ah! Let me go download that. Thanks! -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 16:30 , Jens Alfke j...@mooseyard.com wrote: The error I get with Xcode 7 beta 2 is error: operand of postfix '?' should have optional type; type is 'NSURLRequest' if let req = NSURLRequest(URL: url)? ~~^ So it looks like this has been fixed already. Strange, I'm still getting the same error after downloading beta 2. Xcode Version 7.0 beta (7A121l): could not find an overload for 'init' that accepts the supplied arguments. -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On 30 Jun 2015, at 09:35, Rick Mann rm...@latencyzero.com wrote: On Jun 29, 2015, at 16:30 , Jens Alfke j...@mooseyard.com wrote: The error I get with Xcode 7 beta 2 is error: operand of postfix '?' should have optional type; type is 'NSURLRequest' if let req = NSURLRequest(URL: url)? ~~^ So it looks like this has been fixed already. Strange, I'm still getting the same error after downloading beta 2. Xcode Version 7.0 beta (7A121l): could not find an overload for 'init' that accepts the supplied arguments. What’s the actual problem here, this thread baffles me What’s wrong with let rr = WKWebView() if let url = NSURL(string: some url) { rr.loadRequest( NSURLRequest( URL: url ) ) } The NSURLRequest() initializer isn’t fallible so both error messages are basically correct when you stuff a ‘?’ on the end of the NSURLRequest() init. There is no overload for an init() for an optional NSURLRequest which takes the supplied arguments because there isn’t a fallible initializer for an NSURLRequest. it’s the same as the difference between this let x = Int( 123 ) and let x = Int( 123 )? The first works, the second one goes ‘huh?’ because there are no fallible initializers for Int. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 16:17 , Jens Alfke j...@mooseyard.com wrote: On Jun 29, 2015, at 3:53 PM, Rick Mann rm...@latencyzero.com wrote: The compiler's message was, Could not find an overload for 'init' that accepts the supplied arguments. Could it instead say NSURLRequst.init(URL:) does not return Optional type? The error message looks correct to me — it's complaining about passing the variable ‘url’ (an NSURL?) to the NSURLRequest initializer (which takes a non-optional NSURL.) I don’t see any reason the compiler would complain about the return type of NSURLRequest.init, since all you’re doing is assigning it to a constant without giving a type. I don't think so. The previous email didn't have the right example. I posted it to pastebin to preserve the formatting and show the compiler output: http://pastebin.com/Q7sBWBnN url is unwrapped already, and I'm (wrongly) trying to unwrap the NSURLRequest. But it's not complaining about the arguments to NSURLRequest.init(URL:). -- Rick Mann rm...@latencyzero.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Simple Swift question
On Jun 29, 2015, at 4:23 PM, Rick Mann rm...@latencyzero.com wrote: http://pastebin.com/Q7sBWBnN http://pastebin.com/Q7sBWBnN url is unwrapped already, and I'm (wrongly) trying to unwrap the NSURLRequest. But it's not complaining about the arguments to NSURLRequest.init(URL:). The error I get with Xcode 7 beta 2 is error: operand of postfix '?' should have optional type; type is 'NSURLRequest' if let req = NSURLRequest(URL: url)? ~~^ So it looks like this has been fixed already. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com