Showing posts with label comparing with objective c. Show all posts
Showing posts with label comparing with objective c. Show all posts

Friday, June 12, 2015

Dictionaries


Swift only provides two collection types. One is arrays and the other is dictionaries. Each value in a dictionary is associated with a unique key. If you’re familiar with NSDictionary in Objective C, the syntax of initializing a dictionary in Swift is similar. Here is an example:

Objective C:

NSDictionary *companies = @{@"AAPL" : @"Apple Inc", @"GOOG" : @"Google Inc", @"AMZN" :@"Amazon.com, Inc", @"FB" : @"Facebook Inc"};

Swift:

var companies = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc", "AMZN": "Amazon.com, Inc",
"FB" : "Facebook Inc"]

The key and value in the key-value pairs are separated by a colon. Like array and other variables, Swift automatically detects the type of the key and value. However, if you like, you can specify the type information by using the following syntax:

var companies: Dictionary<String, String> = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc",
"AMZN" : "Amazon.com, Inc", "FB" : "Facebook Inc"]

To iterate through a dictionary, use the for-in loop.

for (stockCode, name) in companies {
    println("\(stockCode) = \(name)")
}


You can also use the keys and values properties to retrieve the keys and values of the dictionary.

for stockCode in companies.keys {
    println("Stock code = \(stockCode)")
}
for name in companies.values {
    println("Company name = \(name)")
}


To access the value of a particular key, specify the key using the subscript syntax. If you want to add a new key-value pair to the dictionary, simply use the key as the subscript and assign it with a value like below:

companies["TWTR"] = "Twitter Inc"

Now the “companies” dictionary contains a total of 5 items. The “TWTR”:”Twitter Inc” pair is automatically added to the “companies” dictionary.

Thursday, June 11, 2015

Arrays

The syntax of declaring an array in Swift is similar to that in Objective c.
 
            Here is an example:
   

Objective c:

NSArray *recipes = @[@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich"];

Swift: 

var recipes = ["Egg Benedict", "Mushroom Risotto", "Full Breakfast", "Hamburger", "Ham and Egg Sandwich"]

While you can put any objects in NSArray or NSMutableArray in Objective C, arrays in Swift can only store items of the same type. In the above example, you can only store strings in the string array. With type inference, Swift automatically detects the array type. But if you like, you can also specify the type in the following form: 


var recipes : String[] = ["Egg Benedict", "Mushroom Risotto", "Full Breakfast", "Hamburger","Ham and Eg Sandwich"]


Like NSArray, the Swift provides various methods for you to query and manipulate an array.
Simply use the count method to find the number of items in the array: 

var numberOfItems = recipes.count

// recipes.count will return 5

In Objective C you use the addObject: method of NSMutableArray to add a new item to an array. Swift makes the operation even simpler. You can add an item by using the “+=” operator: 

recipes += ["Thai Shrimp Cake"]

This applies when you need to add multiple items:

recipes += ["Creme Brelee", "White Chocolate Donut", "Ham and Cheese Panini"]

To access or change a particular item in an array, pass the index of the item by using subscript syntax just like that in Objective C. 

var recipeItem = recipes[0]

recipes[1] = "Cupcake"

One interesting feature of Swift is that you can use “...” to change a range of values. Here is an example:

recipes[1...3] = ["Cheese Cake", "Greek Salad", "Braised Beef Cheeks"]

This changes the item 2 to 4 of the recipes array to “Cheese Cake”, “Greek Salad” and “Braised Beef Cheeks”. (Remember the first item in an array starts with the index 0. This is why index 1 refers to item 2.)

If you print the array onto console, here is the result: 

Egg Benedict
Cheese Cake
Greek Salad
Braised Beef Cheeks
Ham and Egg Sandwich 

Wednesday, June 10, 2015

Basic String Manipulation

In Swift, strings are represented by the String type, which is fully Unicode-compliant. You can declare strings as variables or constants:

let dontModifyMe = "You cannot modify this string"
var modifyMe = "You can modify this string"

In Objective-C, you have to choose between NSString and NSMutableString classes to indicate whether the string can be modified. You do not need to make such choice in Swift. Whenever you assign a string as variable (i.e. var), the string can be modified in your code.

Swift simplifies string manipulating and allows you to create a new string from a mix of constants, variables, literals, as well as, expressions. Concatenating strings is super easy. Simply add two strings together using the “+” operator:

let firstMessage = "Swift is awesome. "
let secondMessage= "What do you think?"
var message = firstMessage + secondMessage
println(message)

Swift automatically combines both messages and you should see the following message in console. Note that println is a global function in Swift to print the message in console.
Swift is awesome. What do you think?
You can do that in Objective C by using stringWithFormat: method. But isn’t the Swift version more readable?

NSString *firstMessage = @"Swift is awesome. ";
NSString *secondMessage = @"What do you think?";
NSString *message = [NSString stringWithFormat:@"%@%@", firstMessage, secondMessage];
NSLog(@"%@", message);

String comparison is more straightforward. In Objective C, you can’t use the == operator to compare two strings. Instead you use the isEqualToString: method of NSString to check if both strings are equal. Finally, Swift allows you to compare strings directly by using the == operator.

var string1 = "Hello"
var string2 = "Hello"
if string1 == string2 {
        println("Both are the same")
}