xCode Objective C Array examples


/ Published in: Objective C
Save to your folder(s)

Examples of different things you can do with arrays


Copy this code and paste it in your HTML
  1. [selectedLevels replaceObjectAtIndex:0 withObject:3];
  2. NSLog(@"Count: %i, %@", 0, [selectedLevels objectAtIndex:0]);
  3.  
  4. NSMutableArray *TempShortArray = [[NSMutableArray alloc] init];
  5. NSLog(@"Count: %i, %@", i, [xTempArray objectAtIndex:i]);
  6. NSLog(@"Count: %i", [xTempArray count]);
  7. gameData[0].stars = 2;
  8.  
  9. ScoreAndLevelData = [[NSMutableArray alloc] initWithCapacity:10];
  10. [ScoreAndLevelData addObject:TempShortArray];
  11.  
  12. NSMutableArray *TempShortArray = [[NSMutableArray alloc] init];
  13. [TempShortArray setArray:[[xTempArray objectAtIndex:i] componentsSeparatedByString:@","] ];
  14.  
  15.  
  16.  
  17.  
  18. //------------------------NSArray
  19. //from http://www.icodeblog.com/2009/08/26/objective-c-tutorial-nsarray/
  20.  
  21. // I am using strings, but you can add just about any object to an NSArray
  22.  
  23. // Creates an NSArray with one object
  24. NSArray * myArray = [NSArray arrayWithObject:@"foo"];
  25.  
  26. // Creates an NSArray with multiple objects. Don't forget to add nil as the last object
  27. NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];
  28.  
  29. // Creates an NSArray from another NSArray
  30. NSArray * myArray3 = [NSArray arrayWithArray:myArray2];
  31.  
  32. // 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.
  33. NSArray * myArray4 = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://icodeblog.com/wp-content/uploads/2009/08/foo.plist"]];
  34.  
  35. NSString * f = @"foo";
  36. NSString * b = @"bar";
  37. NSString * z = @"baz";
  38. NSArray * myArray2 = [NSArray arrayWithObjects:f,b,z,nil];
  39. NSInteger idx = [myArray2 indexOfObject:b];
  40. // This would return 1 (since NSArrays are 0 - indexed)
  41.  
  42. // Lets pretend the bullet object has a method called move
  43. // and there is an array of 50 bullets
  44. [bullets makeObjectsPerformSelector:@selector(move)];
  45.  
  46. NSArray *sortedArray = [myArray2 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  47. // This will return a sorted array that looks like [@"bar",@"baz",@"foo"]
  48.  
  49. for(NSString * myStr in myArray2) {
  50. NSLog(myStr);
  51. }
  52.  
  53. //save data to file
  54. NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];
  55. [myArray2 writeToFile:filePath atomically:YES];
  56.  
  57. //http://www.techotopia.com/index.php/Working_with_Objective-C_Array_Objects
  58. //--NSArray can not be changed!
  59. NSArray *myColors;
  60. myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; //requires the nil at end
  61. NSLog (@"Number of elements in array = %lu", [myColors count]);
  62.  
  63. //NSMutableArray CAN be changed
  64. NSMutableArray *myColors;
  65. myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
  66.  
  67. //-------
  68. NSArray *myColors;
  69. int i;
  70. int count;
  71. myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
  72. count = [myColors count];
  73. for (i = 0; i < count; i++)
  74. NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]);
  75.  
  76. //-------
  77. NSArray *myColors;
  78. NSString *color;
  79. myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
  80. for (color in myColors)
  81. NSLog (@"Element = %@", color);
  82.  
  83. //adding
  84. [myColors addObject: @"Indigo"];
  85. [myColors addObject: @"Violet"];
  86. //inserting
  87. [myColors insertObject: @"Indigo" atIndex: 1];
  88. [myColors insertObject: @"Violet" atIndex: 3];
  89.  
  90. //deleting
  91. [myColors removeObjectAtIndex: 0];
  92. [myColors removeObject: @"Red"];
  93. [myColors removeObjectIdenticalTo: @"Red"];
  94. [myColors removeAllObjects];
  95. [myColors removeLastObject];
  96.  
  97. //sorting
  98. NSArray *sortedArray;
  99. sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
  100.  
  101.  
  102.  
  103. //----------DICTIONARY OBJECTS
  104. //Once a relationship between a key and a value has been established that relationship cannot subsequently be modified.
  105. //creating:
  106. NSDictionary *bookListing = [NSDictionary dictionary];
  107. //or changeble
  108. NSMutableDictionary *bookListing = [NSMutableDictionary dictionary];
  109.  
  110. //populating
  111. [bookListing setObject: @"Wind in the Willows" forKey: @"100-432112"];
  112. [bookListing setObject: @"Tale of Two Cities " forKey: @"200-532874"];
  113. [bookListing setObject: @"Sense and Sensibility" forKey: @"202-546549"];
  114. [bookListing setObject: @"Shutter Island" forKey: @"104-109834"];
  115. //or
  116. NSDictionary *bookListing = [NSDictionary dictionaryWithObjectsAndKeys: @"Wind in the Willows", @"100-432112", @"Tale of Two Cities ", @"200-532874", @"Sense and Sensibility", @"202-546549", @"Shutter Island", @"104-109834", nil];
  117. //or
  118. NSArray *objectsArray = [NSArray arrayWithObjects: @"Wind in the Willows", @"Tale of Two Cities ", @"Sense and Sensibility", @"Shutter Island", nil];
  119. NSArray *keysArray = [NSArray arrayWithObjects: @"100-432112", @"200-532874", @"202-546549", @"104-109834", nil];
  120. NSDictionary *bookListing = [[NSDictionary alloc] initWithObjects: objectsArray forKeys: keysArray];
  121.  
  122.  
  123. //------get an entry count
  124. NSMutableDictionary *bookListing = [NSMutableDictionary dictionary];
  125. int count;
  126. [bookListing setObject: @"Wind in the Willows" forKey: @"100-432112"];
  127. [bookListing setObject: @"Tale of Two Cities " forKey: @"200-532874"];
  128. [bookListing setObject: @"Sense and Sensibility" forKey: @"202-546549"];
  129. [bookListing setObject: @"Shutter Island" forKey: @"104-109834"];
  130. NSLog (@"Number of books in dictionary = %lu", [bookListing count]);
  131.  
  132. //calling the data:
  133. NSLog ( @"100-432112 = %@", [bookListing objectForKey: @"100-432112"]);
  134. NSLog ( @"200-532874 = %@", [bookListing objectForKey: @"200-532874"]);
  135. NSLog ( @"202-546549 = %@", [bookListing objectForKey: @"202-546549"]);
  136. NSLog ( @"104-109834 = %@", [bookListing objectForKey: @"104-109834"]);
  137.  
  138. //deleting
  139. [bookListing removeObjectForKey: @"104-109834"];
  140. [bookListing removeAllObjects];
  141.  
  142. //---------use blocks to go through each item of array
  143. NSArray *cards =
  144. [NSArray arrayWithObjects:@"Jack", @"Queen", @"King", @"Ace", nil];
  145.  
  146. [cards enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
  147. NSLog(@"%@ card at index %d", object, index);
  148. }];
  149.  
  150.  
  151. //--------array of ints
  152. [NSArray arrayWithObjects:[ NSNumber numberWithInteger: 1500 ],[ NSNumber numberWithInteger: 1750 ],nil]
  153.  
  154. //--------accessing array object as int
  155. [[xArray objectAtIndex:x] intValue];

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.