Swift Tips: Switch

Please find Swift playground at github.com

This post shares from simple to advance the use of switch cases in swift. These can be enhanced and some time shorthand of more lines of code. I hope you would enjoy them and try to use in your coding style. Please let me know your feedback in the comments if you have any. Happy coding.

Continue reading
Blog Post Rating :

How to enumerate items in an array

There several ways to loop through an array in Swift, but using the enumerated() method is one of my favorites because it iterates over each of the items while also telling you the items’ position in the array.

Here’s an example:
[swift]
//How to enumarate a array
let array = [“Hulk”, “Spidy”, “Thor”]

for (index, item) in array.enumerated() {
print(“Found \(item) at position \(index)”)
}
[/swift]

That will print “Found Hulk at position 0”, “Found Spidy at position 1”, and “Found Thor at position 2”.

Blog Post Rating :

Always Try, Try?, And Try!, Never give up

How-to-Get-Started-With-Unit-Testing-Through-Xcode-in-Swift1

Beside Swift being powerful, expressive language with a flexible, elegant syntax. There is one of the features I appreciate most is native support for error handling with try keyword.

It is easy to ignore errors in Objective-C. You have to do a bit of extra work to enable error handling.

[swift]

NSError *error = nil;// Initialize Data With Contents of URL

NSData *data = [[NSData alloc] initWithContentsOfURL:URL options:0 error:&error];

if (error) {

// Error Handling …

}

[/swift]

Continue reading

Blog Post Rating :

Understanding more about MVVM

We all know about MVC and it’s shortcomings. I would love to explain MVVM that make developer’s life as well as testing quite easy

Creating a View Model

Before we take a look at View Model. let’s tale a look at something familiar. You would feel home if you’ve been using the Model-View-Controller pattern to build Swift applications. The moment the view controller has finished loading its view, it performs a network request. It delegates this task to the APIManager class, Continue reading

Blog Post Rating :