Optionals in SWIFT
In SWIFT, an interesting and a powerful feature that i liked mostly is "Optionals" . Can you imagine, how many times you have checked a variable in Objective-C for its Validness, (If it is nil or not) ? How many times do you think, you might have written the below code in Objective-C: NSString *string = someFun() if (string != nil){ //do some stuff with the returned string here } I can’t even count on this, since i do this often throughout my classes. Apple have done a great job to solve this issue with Optional variables. Let’s look into this feature more deeper with an example to understand its use and purpose. Consider, you have an array of strings of Phone Numbers as below: let numbers = [“9876543219”, “9898878890”, “9876556789”] We’ll write a simple function to see if we can find a phone number within the array. func findNumber (number: String , numbers: [ String ]) -> String { ...