Filter an array based on condition

 Problem

I have an array and want to filter its elements based on a defined condition.



 Recipe


// Initialize the Array
var a = [1,2,3,4,5,6]

// Get only even numbers: X % 2 = 0

a = a.filter { $0 % 2 == 0 }
print(a)
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground