Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
NSArray sample       Share
2010-07-15 |  RatheeshTR  | Viewed: 14073  |    0

Objective-C Tutorial: NSArray

Example:

NSArray * foo = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil];

Factory methods are static methods that build new instances of NSArrays from given parameters and return them. 

The table below details on all of the factory methods for the NSArray class.

+ (id)array Creates and returns an empty array
+ (id)arrayWithArray:(NSArray *)anArray Creates and returns an array containing the objects in another given array.
+ (id)arrayWithContentsOfFile:(NSString *)aPath Creates and returns an array containing the contents of the file specified by a given path. * The file must be of type .plist for this method to work
+ (id)arrayWithContentsOfURL:(NSURL *)aURL Similar to arrayWithContentsOfFile except it will load the .plist remotely from a given website. This would be a very simple way to get data from a web service.
+ (id)arrayWithObject:(id)anObject Creates and returns an array containing a given object. This will just be a 1 element array
+ (id)arrayWithObjects:(id)firstObj, … This method is used when you have multiple objects on hand and want easily insert them into an array. Make sure the last element you add is nil or this method won’t work.
+ (id)arrayWithObjects:(const id *)objects count:(NSUInteger)count Creates and returns an array that includes a given number of objects from a given C array.


// I am using strings, but you can add just about any object to an NSArray
 
// Creates an NSArray with one object
NSArray * myArray = [NSArray arrayWithObject:@"foo"];
 
// Creates an NSArray with multiple objects. Don't forget to add nil as the last object
NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];
 
// Creates an NSArray from another NSArray
NSArray * myArray3 = [NSArray arrayWithArray:myArray2];
 
// This will create an NSArray from data on iCodeBlog. Go ahead and try it out, this file exists on our servers and contains valid data.
NSArray * myArray4 = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://icodeblog.com/wp-content/uploads/2009/08/foo.plist"]];


You can also choose not to use factory methods and just use the normal NSArray initializers.
They are pretty much the same as the factory methods only you do the allocation yourself. An example of this might be:

NSArray * foo = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil];

Accessing Objects in a NSArray

To access an object in an NSArray, you use the -objectAtIndex: method, as in the following example:

NSArray *numbers;
NSString *string;

numbers = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
string = [numbers objectAtIndex: 2]; // @"Three"

Of course, you have to be careful not to ask for an object at an index which is negative or
bigger than the size of the array; if you do, an NSRangeException is raised
(we'll learn more about exceptions in another tutorial).

To get the length of an array, you use the method -count, as in:

NSArray *numbers;
int i;

numbers = [NSArray arrayWithObjects: @"One", @"Two", @"Three",  nil];
i = [numbers count]; // 3

 

Searching The Array:

NSString * f = @"foo";
NSString * b = @"bar";
NSString * z = @"baz";
NSArray * myArray2 = [NSArray arrayWithObjects:f,b,z,nil];
NSInteger idx = [myArray2 indexOfObject:b];
// This would return 1 (since NSArrays are 0 - indexed)

Sorting Arrays:

NSArray *sortedArray =

    [myArray2 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

    // This will return a sorted array that looks like [@"bar",@"baz",@"foo"]

Looping Through Arrays:

for(NSString * myStr in myArray2) {

    NSLog(myStr);

}

Saving Arrays For Later:

NSArray  * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];

[myArray2 writeToFile:filePath atomically:YES];

Reference url:

http://icodeblog.com/2009/08/26/objective-c-tutorial-nsarray/

http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/

http://memo.tv/nsarray_vs_c_array_performance_comparison

 


Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
Iphone Cocoa Programming tutorials
Iphone BubbleLevel Tutorials
Iphone UIImage Class Reference ( objective C )
Objective -C Date Formatter Examples -Iphone stringFromDate
iPhone Coding: Slider tutorial
Frequently used Iphone SDK API list

Related References
objective-c tutorial: nsarray -- arraywithcontentsofurl
iphone sdk: get contacts with the iphone sdk sample program
iphone sdk - local notification sample code (os 4.0 only)
resize uiimage sample tutorial

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us