Re: enumerate arrays in Swift
Re: enumerate arrays in Swift
- Subject: Re: enumerate arrays in Swift
- From: Roland King <email@hidden>
- Date: Wed, 02 Jul 2014 21:08:54 +0800
That's in the book, look at the enumerate() function which returns the tuples required to give you (index,element)
On 2 Jul, 2014, at 9:01 pm, Gerriet M. Denkmann <email@hidden> wrote:
> enumerateKeysAndObjectsUsingBlock: can be written as:
>
> var airports = ["TYO": "Tokyo", "DUB": "Dublin"]
> for (airportCode, airportName) in airports
> {
> println("\(airportCode) = \(airportName)")
> }
>
>
> But how to do NSArray's enumerateObjectsUsingBlock: ?
>
> This works, but is rather clumsy:
>
> let names = ["Anna", "Alex", "Brian", "Jack"]
> var i : Int = 0
> for name in names
> {
> let indexFormated = NSString(format: "-", i++)
> println( "name[" + indexFormated + "] = " + name)
> }
>
> What I want is:
> for (idx, name) in names {...}
> Can this be done in Swift?
>
> Gerriet.
>
> _______________________________________________
> 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
_______________________________________________
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