Posts

Showing posts from February, 2019

Swift 'for' loop with enumerated() and zip()

'for' loop is used every where in programming languages as the part of Control flow statements. Consider names of few users, I have initialized them below. let names = [ "John" , "Fred" , "Maria" , "Rose" ] Simple for Loop A simple 'for' loop iterating through the items is listed below. for var i = 0 ; i < names.count; i++ {     print( "Name= \ ( names[i] )" ) } This will result in compile time error in Swift 3 onwards. Below is the compile time error " C-style for statement has been removed in Swift 3 " So, lets correct it now and use a much shorter syntax. for item in names {     print ( "Name = \ ( item )" ) } Output: Name = John Name = Fred Name = Maria Name = Rose for Loop with Counter Now what if I want the counter to be also accessible within the same loop. That counter can be used to perform some operation , let's say a