/ Published in: Objective C
Examples of different things you can do with arrays
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
[selectedLevels replaceObjectAtIndex:0 withObject:3]; NSLog(@"Count: %i, %@", 0, [selectedLevels objectAtIndex:0]); NSLog(@"Count: %i, %@", i, [xTempArray objectAtIndex:i]); NSLog(@"Count: %i", [xTempArray count]); gameData[0].stars = 2; [ScoreAndLevelData addObject:TempShortArray]; [TempShortArray setArray:[[xTempArray objectAtIndex:i] componentsSeparatedByString:@","] ]; //------------------------NSArray //from http://www.icodeblog.com/2009/08/26/objective-c-tutorial-nsarray/ // I am using strings, but you can add just about any object to an NSArray // Creates an NSArray with one object // Creates an NSArray with multiple objects. Don't forget to add nil as the last object // Creates an NSArray from another NSArray // 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. NSInteger idx = [myArray2 indexOfObject:b]; // This would return 1 (since NSArrays are 0 - indexed) // Lets pretend the bullet object has a method called move // and there is an array of 50 bullets [bullets makeObjectsPerformSelector:@selector(move)]; // This will return a sorted array that looks like [@"bar",@"baz",@"foo"] NSLog(myStr); } //save data to file [myArray2 writeToFile:filePath atomically:YES]; //http://www.techotopia.com/index.php/Working_with_Objective-C_Array_Objects //--NSArray can not be changed! NSArray *myColors; myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; //requires the nil at end NSLog (@"Number of elements in array = %lu", [myColors count]); //NSMutableArray CAN be changed NSMutableArray *myColors; //------- NSArray *myColors; int i; int count; count = [myColors count]; for (i = 0; i < count; i++) NSLog (@"Element %i = %@", i, [myColors objectAtIndex: i]); //------- NSArray *myColors; NSString *color; for (color in myColors) NSLog (@"Element = %@", color); //adding [myColors addObject: @"Indigo"]; [myColors addObject: @"Violet"]; //inserting [myColors insertObject: @"Indigo" atIndex: 1]; [myColors insertObject: @"Violet" atIndex: 3]; //deleting [myColors removeObjectAtIndex: 0]; [myColors removeObject: @"Red"]; [myColors removeObjectIdenticalTo: @"Red"]; [myColors removeAllObjects]; [myColors removeLastObject]; //sorting NSArray *sortedArray; sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; //----------DICTIONARY OBJECTS //Once a relationship between a key and a value has been established that relationship cannot subsequently be modified. //creating: //or changeble //populating [bookListing setObject: @"Wind in the Willows" forKey: @"100-432112"]; [bookListing setObject: @"Tale of Two Cities " forKey: @"200-532874"]; [bookListing setObject: @"Sense and Sensibility" forKey: @"202-546549"]; [bookListing setObject: @"Shutter Island" forKey: @"104-109834"]; //or 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]; //or NSDictionary *bookListing = [[NSDictionary alloc] initWithObjects: objectsArray forKeys: keysArray]; //------get an entry count int count; [bookListing setObject: @"Wind in the Willows" forKey: @"100-432112"]; [bookListing setObject: @"Tale of Two Cities " forKey: @"200-532874"]; [bookListing setObject: @"Sense and Sensibility" forKey: @"202-546549"]; [bookListing setObject: @"Shutter Island" forKey: @"104-109834"]; NSLog (@"Number of books in dictionary = %lu", [bookListing count]); //calling the data: NSLog ( @"100-432112 = %@", [bookListing objectForKey: @"100-432112"]); NSLog ( @"200-532874 = %@", [bookListing objectForKey: @"200-532874"]); NSLog ( @"202-546549 = %@", [bookListing objectForKey: @"202-546549"]); NSLog ( @"104-109834 = %@", [bookListing objectForKey: @"104-109834"]); //deleting [bookListing removeObjectForKey: @"104-109834"]; [bookListing removeAllObjects]; //---------use blocks to go through each item of array [cards enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) { NSLog(@"%@ card at index %d", object, index); }]; //--------array of ints //--------accessing array object as int [[xArray objectAtIndex:x] intValue];