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

No comments:

Post a Comment