Your SlideShare is downloading. ×
0
Computer Science Large Practical:                                     Arrays in Objective-C                               ...
Experience report         The Desktop Team, in the University’s Information Services         department have asked for fee...
Programming with arrays in Objective-CStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday ...
Inspecting an arrayStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012 ...
The XML representationStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 20...
Sorting an arrayStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   6...
The sorted arrayStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2012   7...
The output in the consoleStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November,...
Everything OK? What about “Build and Analyze”?Stephen Gilmore (School of Informatics)   Computer Science Large Practical  ...
Handling arrays (1/2) #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) {    NSAutoreleasePool * ...
Handling arrays (2/2)       NSArray *sortedArray = [myArray          sortedArrayUsingSelector:@selector(           caseIns...
Object allocation and arrays         Arrays in Objective-C are not generic arrays: they can contain         different kinds...
“Build and Analyze” finds no potential leaks nowStephen Gilmore (School of Informatics)   Computer Science Large Practical ...
Mutable arrays         NSArray objects are not mutable so we cannot update their contents.         To have an array which ...
Declaring and using mutable arraysStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd ...
Output from the programStephen Gilmore (School of Informatics)   Computer Science Large Practical   Friday 2nd November, 2...
Upcoming SlideShare
Loading in...5
×

Arrays in Objective-C

1,025

Published on

Some examples of programs using arrays in Objective-C.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,025
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Arrays in Objective-C"

  1. 1. Computer Science Large Practical: Arrays in Objective-C Stephen Gilmore School of Informatics Friday 2nd November, 2012Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 1 / 16
  2. 2. Experience report The Desktop Team, in the University’s Information Services department have asked for feedback from you regarding your experiences (good or bad!) with Xcode in the Main Library IS Open Access 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
  3. 3. Programming with arrays in Objective-CStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 3 / 16
  4. 4. Inspecting an arrayStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 4 / 16
  5. 5. The XML representationStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 5 / 16
  6. 6. Sorting an arrayStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 6 / 16
  7. 7. The sorted arrayStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 7 / 16
  8. 8. The output in the consoleStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 8 / 16
  9. 9. Everything OK? What about “Build and Analyze”?Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 9 / 16
  10. 10. 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];Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 10 / 16
  11. 11. 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
  12. 12. Object allocation and arrays Arrays in Objective-C are not generic arrays: they can contain different kinds of objects. The nil value at the end is a terminator, signalling the end of the array. You can release an object as soon as you add it to an array because the array allocates its own memory for the object by calling retain on all objects which are added to it. arrayWithObjects auto releases from memory so you do not have to release the array yourself. If we replace [NSArray alloc] initWithObjects with arrayWithObjects then this should solve our memory leak.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 12 / 16
  13. 13. “Build and Analyze” finds no potential leaks nowStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 13 / 16
  14. 14. Mutable arrays NSArray objects are not mutable so we cannot update their contents. To have an array which we can update we should use NSMutableArray.Stephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 14 / 16
  15. 15. Declaring and using mutable arraysStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 15 / 16
  16. 16. Output from the programStephen Gilmore (School of Informatics) Computer Science Large Practical Friday 2nd November, 2012 16 / 16
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×