NSArray and NSMutableArray usage, nsarray

Source: Internet
Author: User

NSArray and NSMutableArray usage, nsarray

NSArray is a static array, that is, the content it points to cannot be changed. It points to a memory area. Once initialized, it cannot be used to modify data in the memory area, but it can read data.

NSMutableArray is a dynamic NSArray subclass. It can be used to modify the content of the memory area to which it points and add array content.

The subscript of the first data of NSArray and NSmutableArray is 0.


**************************************** **************************************** NSArray *************************************** **************************************** * NSArray * array = [NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three", @ "Four", nil]; self. dataArray = array; [array release]; //-(unsigned) Count; number of objects contained in the array; NSLog (@ "self. dataArray cound: % d ", [self. dataArray count]); //-(id) objectAtIndex: (unsigned int) index; get the object at the specified index; NSLog (@ "self. dataArray cound 2: % @ ", [self. dataArray objectAtIndex: 2]); // arrayWithArray: NSArray * array1 = [NSArray alloc] init]; NSMutableArray * MutableArray = [NSMutableArray alloc] init]; NSArray * array = [NSArray arrayWithObjects: @ "a", @ "B", @ "c", nil]; NSLog (@ "array: % @", array ); mutableArray = [NSMutableArray arrayWithArray: array]; NSLog (@ "MutableArray: % @", MutableArray); array1 = [NSArray arrayWithArray: array]; NSLog (@ "array1: array: % @ ", array1); // Copy // id obj; NSMutableArray * newArray = [NSMutableArray alloc] init]; NSArray * oldArray = [NSArray arrayWithObjects: @" ", @ "B", @ "c", @ "d", @ "e", @ "f", @ "g", @ "h", nil]; NSLog (@ "oldArray: % @", oldArray); for (int I = 0; I <[oldArray count]; I ++) {obj = [oldArray objectAtIndex: i] copy]; [newArray addObject: obj];} // NSLog (@ "newArray: % @", newArray); [newArray release]; // quickly enumerate NSMutableArray * newArray = [NSMutableArray alloc] init]; NSArray * oldArray = [NSArray arrayWithObjects: @ "a", @ "B", @ "c ", @ "d", @ "e", @ "f", @ "g", @ "h", nil]; NSLog (@ "oldArray: % @", oldArray); for (id obj in oldArray) {[newArray addObject: obj];} // NSLog (@ "newArray: % @", newArray); [newArray release]; // Deep copyNSMutableArray * newArray = [NSMutableArray alloc] init]; NSArray * oldArray = [NSArray arrayWithObjects: @ "a", @ "B", @ "c ", @ "d", @ "e", @ "f", @ "g", @ "h", nil]; NSLog (@ "oldArray: % @", oldArray); newArray = (NSMutableArray *) Assign (kCFAllocatorDefault, (CFPropertyListRef) oldArray, assign); NSLog (@ "newArray: % @", newArray); [newArray release]; // Copy and sortNSMutableArray * newArray = [NSMutableArray alloc] init]; NSArray * oldArray = [NSArray arrayWithObjects: @ "B", @ "a", @ "e ", @ "d", @ "c", @ "f", @ "h", @ "g", nil]; NSLog (@ "oldArray: % @", oldArray); NSEnumerator * enumerator; enumerator = [oldArray objectEnumerator]; id obj; while (obj = [enumerator nextObject]) {[newArray addObject: obj: @ selector (compare :)]; NSLog (@ "newArray: % @", newArray); [newArray release]; // splits the string into an array-componentsSeparatedByString: NSString * string = [NSString alloc] initWithString: @ "One, Two, Three, Four"]; NSLog (@ "string: % @", string ); NSArray * array = [string componentsSeparatedByString: @ ","]; NSLog (@ "array: % @", array); [string release]; // merge elements from the array to the string-componentsJoinedByString: NSArray * array = [NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three ", @ "Four", nil]; NSString * string = [array componentsJoinedByString: @ ","]; NSLog (@ "string: % @", string ); // NSArray * array; array = [NSMutableArray arrayWithCapacity: 20]; //-(void) addObject: (id) anObject; NSMutableArray * array = [NSMutableArray arrayWithObjects: @ "One", @ "Two", @ "Three", nil]; [array addObject: @ "Four"]; NSLog (@ "array: % @", array); //-(void) removeObjectAtIndex: (unsigned) index; NSMutableArray * array = [NSMutableArray arrayWithObjects: @ "One", @ "Two", @ "Three ", nil]; [array removeObjectAtIndex: 1]; NSLog (@ "array: % @", array); //-(NSEnumerator *) objectEnumerator; NSMutableArray * array = [NSMutableArray inline: @ "One", @ "Two", @ "Three", nil]; NSEnumerator * enumerator; enumerator = [array objectEnumerator]; id thingie; while (thingie = [enumerator nextObject]) {NSLog (@ "thingie: % @", thingie);} //-(NSEnumerator *) reverseObjectEnumerator; from the forward NSMutableArray * array = [NSMutableArray arrayWithObjects: @ "One", @ "Two", @ "Three", nil]; NSEnumerator * enumerator; enumerator = [array handler]; id object; while (object = [enumerator nextObject]) {NSLog (@ "object: % @", object) ;}// fast enumeration NSMutableArray * array = [NSMutableArray arrayWithObjects: @ "One", @ "Two", @ "Three", nil]; for (NSString * string in array) {NSLog (@ "string: % @", string);} //-(id) initWithObjectsAndKeys; NSDictionary * dictionary = [NSDictionary alloc] initWithObjectsAndKeys: @ "One", @ "1", @ "Two ", @ "2", @ "Three", @ "3", nil]; NSString * string = [dictionary objectForKey: @ "One"]; NSLog (@ "string: % @ ", string); NSLog (@" dictionary: % @ ", dictionary); [dictionary release]; // create NSMutableDictionary * dictionary = [NSMutableDictionary dictionary]; // Add the dictionary [dictionary setObject: @ "One" forKey: @ "1"]; [dictionary setObject: @ "Two" forKey: @ "2"]; [dictionary setObject: @ "Three" forKey: @ "3"]; [dictionary setObject: @ "Four" forKey: @ "4"]; NSLog (@ "dictionary: % @", dictionary); // Delete the specified dictionary [dictionary removeObjectForKey: @ "3"]; NSLog (@ "dictionary: % @", dictionary ); // put NSRect into NSArray NSMutableArray * array = [NSMutableArray alloc] init]; NSValue * value; CGRect rect = CGRectMake (0, 0,320,480); value = [NSValue valueWithBytes: & rect objCType: @ encode (CGRect)]; [array addObject: value]; NSLog (@ "array: % @", array ); // extract value = [Array objectAtIndex: 0] From array; [value getValue: & rect]; NSLog (@ "value: % @", value ); NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * home; home = @".. /Users/"; NSDirectoryEnumerator * direnum; direnum = [fileManager enumeratorAtPath: home]; NSMutableArray * files = [NSMutableArray alloc] init]; // enumerate NSString * filename; while (filename = [direnum nextObject]) {if ([filename pathExtension] hasSuffix: @ "jpg"]) {[files addObject: filename];} // fast enumeration for (NSString * filename in direnum) {if ([filename pathExtension] isw.tostring: @ "jpg"]) {[files addObject: filename];} NSLog (@ "files: % @", files); // enumerate NSEnumerator * filenum; filenum = [files objectEnumerator]; while (filename = [filenum nextObject]) {NSLog (@ "filename: % @", filename);} // quick enumeration for (id object in files) {NSLog (@ "object: % @", object);} for (NSString * arrayString1 in readArray1) {// NSLog (@ "arrayString1 = % @", arrayString1); NSArray * array1 = [arrayString1 componentsSeparatedByString: @ "# :)"]; NSString * name1 = [array1 objectAtIndex: 0]; NSString * page1 = [array1 objectAtIndex: 1]; [self. array addObject: [[NSMutableDictionary alloc] initWithObjectsAndKeys: name1, @ "name", page1, @ "page", nil]; NSString * fileString3 = [NSString stringWithFormat: @ "% @ #:) % @", name1, page1]; [self. markName addObject: fileString3];}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.