Sunday, February 16, 2014

iOS Tutorial - Part 7 - NSArray, NSMutableArray

Download finished project from here: Google Drive Download Link

NSArray, NSMutableArray


Video Description
NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. Here is the documentation of this class.

Initialize NSArray

We already know how to define NSArray property and also how to synthesize it. But let's log the value of the array and see what would we get.
Define the property:
@property (nonatomic, strong) NSArray *myArray;
Synthesize it:
@synthesize myMutableArray = _myMutableArray;
Log it like bellow:
NSLog(@" %@", self.myArray);
We expect that the result shows an empty array but The result will be null. Why? Because we have not initialized the array. Before initializing the array, there is no memory allocated for this array. By initializing the array we allocate memory for it then we can send message to that array and get a log for it. The best place to initialize the array property is in it's getter.

Equivalent for Synthesize

As I mentioned in tutorial part 4 When we synthesize properties, in fact we are creating setter and getter for that property. If we want to manually create the setter and getter for the property, we have something like bellow:
Setter
- (void)setMyArray:(NSArray *)myArray
{
    _myArray = myArray;
}

Getter
- (NSArray *)myArray
{
    return _myArray;
}

We need to modify the getter to initialize our array whenever we need the array. We allocate memory like bellow:
- (NSArray *)myArray
{
    if (_myArray == nil) _myArray = [[NSArray alloc] init];
    return _myArray;
}
The if statement above means that, if myArray is null, allocate memory for it and then initialize it for me. Now if we log the array, it shows an empty array. Since we don't change anything in setter method we can delete it.

Useful methods of NSArray

arrayWithObjects
You can define array items by using this method like bellow:
    
self.myArray = [NSArray arrayWithObjects:@"object zero",@"object one" , nil];

count
Returns number of items that array holds
NSLog(@" %d", self.myArray.count);
objectAtIndex:
Returns the value of the item that is located in the specified index
NSLog(@" %@", [self.myArray objectAtIndex:1]);
lastObject
Returns the value of the last object of the array
    NSLog(@" %@", [self.myArray lastObject]);

Useful methods of NSMutableArray

NSMutableArray has all of NSArray methods, plus the following methods:
addObject
Adds an object to the end of the array
[self.myMutableArray addObject:@"object 1"];
insertObject: atIndex:
Inserts an specified item to the array at specified index
[self.myMutableArray insertObject:@"Hi" atIndex:1];
removeObjectAtIndex:
Removes the object at specified index
[self.myMutableArray removeObjectAtIndex:0];
removeLastObject
Removes the last object of the array
[self.myMutableArray removeLastObject];

All of this tutorial code

//
//  ViewController.m
//  HelloWorld
//
//  Created by HuxTek on 1/19/14.
//  Copyright (c) 2014 HuxTek. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSArray *myArray;
@property (nonatomic, strong) NSMutableArray *myMutableArray;

@end

@implementation ViewController
@synthesize myArray = _myArray;
@synthesize myMutableArray = _myMutableArray;


//Setter for myArray
- (void)setMyArray:(NSArray *)myArray
{
    _myArray = myArray;
}

//Getter for myArray
- (NSArray *)myArray
{
    //Check if the array is null, allocate memory for it and then initilize it
    if (_myArray == nil) _myArray = [[NSArray alloc] init];
    return _myArray;
}

//Getter for myMutableArray
- (NSArray *)myMutableArray
{
    if (_myMutableArray == nil) _myMutableArray = [[NSMutableArray alloc] init];
    return _myMutableArray;
}

//The following method is View life cycle (Don't worry about it now I will dedicate special session for view life cyle. I used it here because I wanted to send message to console
-(void) viewDidLoad {
    [super viewDidLoad];
    
    //NSArray
    
    //Log static values
    NSLog(@" %@", self.myArray);
    //Result: ()
    self.myArray = [NSArray arrayWithObjects:@"object zero",@"object one" , nil];
    NSLog(@" %@", self.myArray);
    /* Result:
    (
     "object zero",
     "object one"
     )
     */
    
    
    // %d ==> for integer type
    // %g ==> for float type
    NSLog(@" %d", self.myArray.count);
    //Result: 2
    
    NSLog(@" %@", [self.myArray objectAtIndex:1]);
    //Result: object one
    
    NSLog(@" %@", [self.myArray lastObject]);
    //Result: object two

    //NSMUtableArray
    [self.myMutableArray addObject:@"object 0"];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     "object 0"
     )
     */
    
    [self.myMutableArray addObject:@"object 1"];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     "object 0",
     "object 1"
     )
     */
    
    [self.myMutableArray insertObject:@"Hi" atIndex:1];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     "object 0",
     Hi,
     "object 1"
     )
    */
    
    [self.myMutableArray removeObjectAtIndex:0];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     Hi,
     "object 1"
     )
     */
    
    
    [self.myMutableArray removeLastObject];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     Hi
     )
     */

}

@end
Download finished project from here: Google Drive Download Link

Thursday, February 13, 2014

iOS Tutorial - Part 6 - NSLog, NSString, Documentation, Comment

NSString, NSLog


Video Description
One of the important frameworks in Objective-C is NSString, there are a lot of useful methods in this frameworks that could save you multiple lines of codes. In this tutorial I will discuss some of useful ones. If you want to see all of the methods you can read the documentation here : NSString Documentation

NSLog

In order to send message to console we use NSLog API.
NSLog(@" %@", @"Hello Console");

%@: reruns the description of the object that is passed after comma (,)
@"": returns whatever string value that is between quotation ""

How to access the value of property

We access the property inside of the instance methods by putting word "self" and the "." and then name of the property. For example for the property like this :
@property (nonatomic, strong) NSString *st1;

If we want to get the value, we put it on the right side of the equation:
  = self.st1

If we want to set the value, we put it on the left side of the equation:
 self.st1 = //Whatever value

How to put comments in Objective-C

Comments are not compiled in Objective-C. They are just for developers to put brief explanation of their code. If your comment is just one line, you can add // at the beginning of the line and start typing. But if your comment is more than one line it's better to use multiple comment line format which starts with /* and you can end it with */

Useful NSString methods

stringByAppendingString
By using this method you can concatenate two strings together. See example at the end of this tutorial to learn how to use it.
stringByAppendingFormat
By using this method you can give your string(s) a format that you want. See example at the end of this tutorial to learn how to use it.
//
//  ViewController.m
//  HelloWorld
//
//  Created by HuxTek on 1/19/14.
//  Copyright (c) 2014 HuxTek. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSString *st1;
@property (nonatomic, strong) NSString *st2;
@property (nonatomic, strong) NSString *st3;
@end

@implementation ViewController
@synthesize st1 = _st1;
@synthesize st2 = _st2;
@synthesize st3 = _st3;


//The following method is View life cycle (Don't worry about it now I will dedicate special session for view life cyle. I used it here because I wanted to send message to console
-(void) viewDidLoad {
    [super viewDidLoad];
    //Log static values
    NSLog(@" %@", @"Hello Console");
    //Result: Hello Console
    
    
    //Set a vale for property (We set the value of st1 to be "string one"
    self.st1 = @"string one";
    
    //If we want to log the value of the st1, we should write
    NSLog(@" %@", self.st1);
    //Result: string one
    
    
    //Get a vlue of propert
    //We want to get a value of st1 and set it for st2
    //In simple word we want to copy st1 into st2
    self.st2 = self.st1;
    
    //If we want to log the value of the st2, we should write
    NSLog(@" %@", self.st2);
    //Result: string one
    
    
    //How to comments
    //One line comment by adding // at the begging of line
    //Compiler will not compile the comments
    
    /*
        Multiple comment line
        line 2
        line 3 ...
     */
    
    
    //Append (concatinate st1 to st2 by the follwing method)
    //Open bracket [ and close bracket ] is for sending message to objects
    //We send message to st1 to append st2 and at the same time we set st3 to be the result value of this operation
    
    //Lets change the value of st2 so we can differentiate st1 from st2
    self.st2 = @"string two";
    self.st3 = [self.st1 stringByAppendingString:self.st2];
    NSLog(@" %@", self.st3);
    //Result: string onestring two
    
    
    
    //String format
    //If we want to have format like bellow
    //(string one) -> **string two**
    //We should use a method called: stringByAppendingFormat
    self.st3 = [self.st3 stringByAppendingFormat:@"(%@) -> **%@**", self.st1, self.st2];
    NSLog(@" %@", self.st3);
    //Result: (null)
    
    // Result is null because we have not initialized st3
    // We initialize it with empty string before setting it to the desired format
    self.st3 = @"";
    self.st3 = [self.st3 stringByAppendingFormat:@"(%@) -> **%@**", self.st1, self.st2];
    NSLog(@" %@", self.st3);
    //Result: (string one) -> **string two**
}


@end

Saturday, February 8, 2014

iOS Tutorial - Part 5 - Foundation Frameworks

Foundation Frameworks (Property and variable types)


Video Description

NSObject

NSObject is the root class of most Objective-C class hierarchies. Through NSObject, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects (Don't worry if you don't understand the definition, we will talk about it later).

NSString & NSMutableString


In order to define a String type variable or property in Objective-C language we use NSString like bellow:
@property (nonatomic, strong) NSString *myString;

If you want to modify the existing string, instead of NSString type you should use NSMutableString, which is mutable version of the NSString
@property (nonatomic, strong) NSMutableString *myMutableString;

NSArray & NSMutableArray


Ordered collection of objects. In order to define an Array type variable or property in Objective-C language we use NSArray like bellow:
@property (nonatomic, strong) NSArray *myArray;
If we want to add or remove object to the array or any other modification we have to use NSMutableArray type like bellow:
@property (nonatomic, strong) NSMutableArray *myMutableArray;

NSDictionary & NSMutableDictionary


Look up objects using a key to get a value. In order to define a dictionary with keys and values in Objective-C language we use NSDictionary like this:
@property (nonatomic, strong) NSDictionary *myDictionary;
If we want to have a modifiable version of NSDictionary we should use NSMutableDictionary type like bellow:
@property (nonatomic, strong) NSMutableDictionary *myMutableDictionary;

NSNumber

Object wrapper around primitive types like int, float, double, BOOL, etc. It is useful when you want to put these primitive types in an NSArray or NSDictionary. We can define it like bellow:
@property (nonatomic, strong) NSNumber *myNumber;