Re: for case and if case (Swift 2)
Re: for case and if case (Swift 2)
- Subject: Re: for case and if case (Swift 2)
- From: Roland King <email@hidden>
- Date: Fri, 12 Jun 2015 09:01:40 +0800
> On 12 Jun 2015, at 08:49, Roland King <email@hidden> wrote:
>
> Trying out some of the new Swift 2 features for pattern matching (What’s New In Swift around the 19m 35s + mark with Chris Lattner) but not having much success. I have this in a playground
>
> enum test
> {
> case one
> case two( Int )
> case three( String, Int )
> }
>
> let x = [ test.one, test.two( 123 ), test.three( "xx", 1 ), test.one, test.three( "rrr", 7 ), test.two( 9 ) ]
>
> for case test.two( let a ) in x
> {
> print( "a is \(a) " )
> }
>
> Which follows the pattern shown on-screen, as I understand it at least, and in the updated Swift beta 2 book
>
> I get numerous errors on the ‘for case .’ line, the compiler clearly thinks the entire line is garbage. I’ve tried taking the ‘case’ word out, that doesn’t work, I’ve tried taking the ‘test.’ off the start of ‘test.two’, still no good, possibly worse.
>
> Tried a simple version in an if as well
>
> let y = test.two( 123 )
>
>
> if case test.two( let a ) == y { } // variable binding in a condition requires an initializer
>
>
> I must have tried 10 other combinations as well without any success. I must have entirely misunderstood the new uniform case pattern matching, or it doesn’t work in seed 1. The ‘if case’ construct is going to be quite useful, anyone else have more success?
I can answer the second half of my own question
let y = test.two( 123 )
if case test.two( let a ) = y {}
yes ‘=‘ instead of ‘==‘. I hit on that trying random things and don’t really understand it, an ‘=‘ in an if() test is something I’m having trouble getting my brain around.
_______________________________________________
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