In Objective-C, how does NSArray and NSMutableArray work ?, Objective-cnsarray

Source: Internet
Author: User

In Objective-C, how does NSArray and NSMutableArray work ?, Objective-cnsarray

Objective-C arrays are more powerful than C ++ and Java arrays are that NSArray can store different objects. However, you can only save objects, int, char, double, and other basic data types. You must convert them into objects to add arrays.

1. NSArray immutable Array

[Array count]: the length of the array. [Array objectAtIndex 0]: input an array script id to obtain the data object. [ArrayWithObjects;...]: assigns a value to the array object initialization. Here, we can write a pointer to any object, and nil must be used at the end.

 

# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {@ autoreleasepool {NSObject * obj = [[NSObject alloc] init]; NSArray * array = [NSArray arrayWithObjects: @ "a", obj, @ "c", nil]; NSLog (@ "array Count: % lu", [array count]); // traverse the array for (NSObject * object in array) {NSLog (@ "array object: % @", object) ;}[ obj release] ;}return 0 ;}

Print result:

?
2012-07-09 10:52:17.050 objectiveC[944:403] array Count:310:52:17. 052 objectiveC [944: 403] array object:10:52:17. 052 objectiveC [944: 403] array object: <NSObject: 0x7fe479c14110>10:52:17. 053 objectiveC [944: 403] array object: c

Search for objects corresponding to an array index

NSLog (@ "Index 2: % @", [array objectAtIndex: 2]);

Print results: 10:55:16. 382 objectiveC [993: 403] Index 2: c

2. NSMutableArray variable object Array

[NSMutableArray arrayWithCapacity: 6]: initializes the length of a variable array object. If the code below continues to add an array that exceeds the length of 6, the length of NSMutableArray will be automatically expanded. 6 is the granularity you can set. [Array addObject:...]: Add a data object to the end of the variable array. [Array addObjectsFromArray: ..]: adds an array object to the end of a variable array.

2.1 common use:

# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {@ autoreleasepool {NSObject * obj = [[NSObject alloc] init]; NSMutableArray * muArray = [NSMutableArray records: 6]; [muArray addObject: @ "Object 1"]; [muArray addObject: @ "Object 2"]; [muArray addObject: @ "Object 3"]; [muArray addObject: @ "object 4"]; [muArray insertObject: @ "disturbing" atIndex: 2]; [muArray addObject: obj]; for (NSObject * object in muArray) {NSLog (@ "array object: % @", object);} [obj release];} return 0 ;}

Print:

14:01:08. 994 objectiveC [2090: 403] array object: Object 12012-07-09 14:01:08. 996 objectiveC [2090: 403] array object: Object 22012-07-09 14:01:08. 997 objectiveC [2090: 403] array object: spoiler 14:01:08. 997 objectiveC [2090: 403] array object: Object 32012-07-09 14:01:08. 998 objectiveC [2090: 403] array object: Object 42012-07-09 14:01:08. 998 objectiveC [2090: 403] array object: <NSObject: 0x109714110>

Nsange range = NSMakeRange (); set a range between 0 and 3. [Array removeObject: obj inRange: range]: sets to delete data within a range. If this range does not delete this object, nothing will be deleted. In this example, the obj object is in the range 0 to 3 of the array, so the obj is deleted.

2.2 removeObject and removeObjectIdenticalTo

[Array removeObject :( id)]: deletes the specified Element in the array and determines Based on the isEqual message of the object.

[Array removeObjectIdenticalTo :( id)]: deletes the specified Element in the array and determines Based on the object address.

[Array removeObjectIdenticalTo :( id) inRange :( nsange)]: deletes the specified element within the specified range.

[Array removeObjectAtIndex :( NSUInteger)]: deletes the data of the specified index.

[Array removeObjectsInArray :( NSArray *)]: deletes an array element.

Next we will mainly verify the difference between removeObject removeObjectIdenticalTo,

First experiment removeObject

Int main (int argc, const char * argv []) {@ autoreleasepool {NSString * str1 = [[NSString alloc] init]; NSString * str2 = [[NSString alloc] init]; NSString * str3 = [str1 stringByAppendingFormat: @ "string"]; NSString * str4 = [str2 stringByAppendingFormat: @ "string"]; NSMutableArray * muArray = [NSMutableArray arrayWithCapacity: 6]; [muArray addObject: @ "object"]; [muArray addObject: str3]; [muArray addObject: str4]; for (NSObject * object in muArray) {NSLog (@ "array object: % @", object);} if ([str3 isEqual: str4]) {NSLog (@ "str1 isEqual str2");} if (str3 = str4) {NSLog (@ "str1 = str2");} [muArray removeObject: str3]; for (NSObject * object in muArray) {NSLog (@ "array object: % @", object);} [str1 release]; [str2 release];} return 0 ;}

Run print:

14:57:52. 059 objectiveC [2399: 403] array object: Object 14:57:52. 061 objectiveC [2399: 403] array object: String 14:57:52. 062 objectiveC [2399: 403] array object: String 14:57:52. 062 objectiveC [2399: 403] str1 isEqual str22012-07-09 14:57:52. 063 objectiveC [2399: 403] array object: Object

In this case, all strings are removed.

Try removeObjectIdenticalTo

Code changed to: [muArray removeObjectIdenticalTo: str3];

Print:

?
14:59:53. 520 objectiveC [2432: 403] array object: Object14:59:53. 521 objectiveC [2432: 403] array object: String14:59:53. 522 objectiveC [2432: 403] array object: String2012-07-09 14:59:53.523 objectiveC[2432:403] str1 isEqual str214:59:53. 523 objectiveC [2432: 403] array object: Object14:59:53. 524 objectiveC [2432: 403] array object: String

There is another "string" left behind.

We compared two objects, isEqual, and compared the object address with =.Str1 isEqual str2

        NSRange rang = NSMakeRange(0, 3);        [muArray removeObject:obj inRange:rang];

You can specify a range to delete objects. Other methods can also specify a range to delete objects. objects that are not in this range are not deleted.

2.3 Replace the object corresponding to an index value

Int main (int argc, const char * argv []) {@ autoreleasepool {NSObject * obj = [[NSObject alloc] init]; NSMutableArray * muArray = [NSMutableArray limit: 6]; [muArray addObject: @ "Object 1"]; [muArray addObject: @ "Object 2"]; [muArray addObject: @ "Object 3"]; [muArray addObject: @ "object 4"]; [muArray addObject: obj]; for (NSObject * object in muArray) {NSLog (@ "array object: % @", object );} [muArray replaceObjectAtIndex: 4 withObject: @ "string replaced"]; for (NSObject * object in muArray) {NSLog (@ "array object: % @", object );} [obj release];} return 0 ;}

Printed result after replacement:

15:06:01. 919 objectiveC [2497: 403] array object: Object 12012-07-09 15:06:01. 920 objectiveC [2497: 403] array object: Object 22012-07-09 15:06:01. 920 objectiveC [2497: 403] array object: Object 32012-07-09 15:06:01. 921 objectiveC [2497: 403] array object: Object 42012-07-09 15:06:01. 921 objectiveC [2497: 403] array object: string replaced

3. array iteration Traversal method

Int main (int argc, const char * argv []) {@ autoreleasepool {NSObject * obj = [[NSObject alloc] init]; NSMutableArray * muArray = [NSMutableArray limit: 6]; [muArray addObject: @ "Object 1"]; [muArray addObject: @ "Object 2"]; [muArray addObject: @ "Object 3"]; [muArray addObject: @ "object 4"]; [muArray addObject: obj]; NSEnumerator * enmuerator = [muArray objectEnumerator]; id object; while (object = [enmuerator nextObject]) {NSLog (@ "object in the array: % @", object);} [obj release];} return 0 ;}
I hope to share with more IOS developers. I can add txs8882909 to study and communicate with each other.
 

Related Article

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.