13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 3: Foundational APIs 53Listing 3-16. Array Literal SyntaxNSArray * array = @[ @"Hello", NSApp, [NSNumber numberWithInt: 3] ];Classically, arrays are only a place to store and access objects. In this regard, NSArray is morethan adequate. The method count returns an unsigned NSInteger (an NSUInteger) that specifiesthe number of objects contained within the array. Similarly, objectAtIndex: accepts as aparameter an NSUInteger representing the index of an object in the array, which is returned. Thismethod returns an object of type id. Naturally, if you try to access an object at an index thatdoes not exist, a runtime exception is generated.The return value of objectAtIndex: has an interesting implication: an array can be aheterogeneous list. Unlike Java, C++, or C#, <strong>Objective</strong>-C does not include a strict templatingsystem because the collections libraries can contain any type of object. An array instancetypically only holds one type of object, but the code in Listing 3-17 is perfectly valid.Listing 3-17. Heterogeneous ArraysNSArray *array = [NSArray arrayWithObjects:@"a string", [NSNumber numberWithBool:YES], nil];New in OS X 10.8 and iOS 6.0 with the latest LLVM compiler is a collection subscriptionsyntax. This provides a much simpler (and arguably more readable) way of accessing an array’scontents, shown in Listing 3-18.Listing 3-18. Array SubscriptingNSString * str = myArray[3];Note We said “OS X 10.8 and iOS 6.0” rather than just “with the latest compiler,” as for numeric objectliterals. The reason for this is that array subscripts evaluate not to a call to -objectAtIndex:, butto a new method called -objectAtSubscriptedIndex:. This is only implemented on NSArray inthe new operating systems, hence the requirement. You can check for the existence of this method atruntime using -respondsToSelector: or [NSArray instancesRespondToSelector:].If NSArray only provided functions to store and access a list of objects, we would stop here.Fortunately, the Foundation collection libraries are rich with functionality.You can create an array by appending an object, or objects, to an existing array, as shown inListing 3-19.Listing 3-19. Appending to Existing ArraysNSArray *myNewArray = [myOldArray arrayByAddingObject:myObject];NSArray *myOtherNewArray = [myOldArray arrayByAppendingObjectsFromArray:myOtherOldArray];It’s important to reiterate that these new ways to create arrays don’t copy the objects theycontain; the new array points to the same objects in memory as the first array does.Displaying the contents of an array to users can be accomplished using the methodcomponentsJoinedByString:.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!