arrays in objective-c
DESCRIPTION
Some examples of programs using arrays in Objective-C.TRANSCRIPT
![Page 1: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/1.jpg)
Computer Science Large Practical:
Arrays in Objective-C
Stephen Gilmore
School of Informatics
Friday 2nd November, 2012
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 1 / 16
![Page 2: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/2.jpg)
Experience report
The Desktop Team, in the University’s Information Servicesdepartment have asked for feedback from you regarding yourexperiences (good or bad!) with Xcode in the Main Library IS OpenAccess Lab.
This will help inform their plans for future academic years.
What has been your experience?
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 2 / 16
![Page 3: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/3.jpg)
Programming with arrays in Objective-C
shot 2012-11-01 at 15.49.03.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 3 / 16
![Page 4: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/4.jpg)
Inspecting an array
shot 2012-11-01 at 15.48.54.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 4 / 16
![Page 5: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/5.jpg)
The XML representation
shot 2012-11-01 at 15.49.50.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 5 / 16
![Page 6: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/6.jpg)
Sorting an array
shot 2012-11-02 at 09.12.09.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 6 / 16
![Page 7: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/7.jpg)
The sorted array
shot 2012-11-01 at 15.51.38.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 7 / 16
![Page 8: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/8.jpg)
The output in the console
shot 2012-11-01 at 15.57.19.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 8 / 16
![Page 9: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/9.jpg)
Everything OK? What about “Build and Analyze”?
shot 2012-11-02 at 09.02.59.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 9 / 16
![Page 10: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/10.jpg)
Handling arrays (1/2)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]
init];
NSLog(@"Hello, arrays!");
NSArray * myArray = [[NSArray alloc]
initWithObjects:@"foo",@"bar",@"baz",nil];
for(NSString * myStr in myArray) {
NSLog(@"-- %@", myStr);
}
[myArray writeToFile:@"myArray.plist" atomically:YES];
NSArray *sortedArray =
[myArray sortedArrayUsingSelector:@selector(
caseInsensitiveCompare:)];
[sortedArray writeToFile:@"sortedArray.plist"
atomically:YES];
for(NSString * myStr in sortedArray) {
NSLog(@"---- %@", myStr);
}
[pool drain];
return 0;
}
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 10 / 16
![Page 11: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/11.jpg)
Handling arrays (2/2)
NSArray *sortedArray = [myArray
sortedArrayUsingSelector:@selector(
caseInsensitiveCompare:)];
[sortedArray writeToFile:@"sortedArray.plist"
atomically:YES];
for(NSString * myStr in sortedArray) {
NSLog(@"---- %@", myStr);
}
[pool drain];
return 0;
}
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 11 / 16
![Page 12: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/12.jpg)
Object allocation and arrays
Arrays in Objective-C are not generic arrays: they can containdifferent kinds of objects.
The nil value at the end is a terminator, signalling the end of thearray.
You can release an object as soon as you add it to an array becausethe array allocates its own memory for the object by calling retain onall objects which are added to it.
arrayWithObjects auto releases from memory so you do not haveto release the array yourself.
If we replace [NSArray alloc] initWithObjects witharrayWithObjects then this should solve our memory leak.
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 12 / 16
![Page 13: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/13.jpg)
“Build and Analyze” finds no potential leaks now
shot 2012-11-02 at 09.18.34.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 13 / 16
![Page 14: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/14.jpg)
Mutable arrays
NSArray objects are not mutable so we cannot update their contents.
To have an array which we can update we should useNSMutableArray.
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 14 / 16
![Page 15: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/15.jpg)
Declaring and using mutable arrays
shot 2012-11-02 at 09.59.55.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 15 / 16
![Page 16: Arrays in Objective-C](https://reader033.vdocuments.net/reader033/viewer/2022052900/555e372dd8b42a63048b4a53/html5/thumbnails/16.jpg)
Output from the program
shot 2012-11-02 at 10.00.07.png
Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 16 / 16