Knowledge points of IOS array NSArray and NSMutableArray, two-dimensional nsarray Array

Source: Internet
Author: User

Knowledge points of IOS array NSArray and NSMutableArray, two-dimensional nsarray Array

This article summarizes the knowledge points of arrays NSArray and NSMutableArray, mainly for some common operations. For other operations, see the relevant documents, the following code also uses a third-party plug-in BlocksKit;

A: In Foundation, arrays (NSArray) are ordered object sets. B: NSArray can only store Objective-C objects, rather than basic data types such as int and float, however, Objective-C is compatible with C. Therefore, in Objective-C Programs, the array of C can still be used to store the basic data type c: NSArray cannot be changed once it is created. If you want to add, delete, or modify an array, you need to use NSMutableArray, a subclass of NSArray, to create an object.

1: Some Common NSArray operations

NSArray * array = [NSArray array]; // Add a single // array = [NSArray arrayWithObject: @ "wujy"]; // Add multiple arrays = [NSArray arrayWithObjects: @ "cnblogs ",@". com ", nil]; NSInteger arrayCount = array. count; NSLog (@ "number of current array Arrays: % ld", arrayCount); int I = 0; for (id obj in array) {NSLog (@ "Current % d is % @", I, obj); I ++;} // NSString * lastString = [array lastObject] for common array Operations; NSLog (@ "last object Value: % @", lastString); NSString * firstString = [array FirstObject]; NSLog (@ "the first object value is: % @", firstString); NSString * indexString = [array objectAtIndex: 1]; NSLog (@ "second object Value: % @", indexString); NSInteger indexInt = [array indexOfObject: @ "cnblogs"]; NSLog (@ "Return Index location: % ld ", indexInt); // convert the string to an array NSString * arrayString = @" a, B, c, d "; NSArray * newArray = [arrayString componentsSeparatedByString :@", "]; for (id obj in newArray) {NSLog (@" the current string is converted to % @ ", obj);} // determine whether the array has an element if ([NewArray containsObject: @ "c"]) {NSLog (@ "element with a letter c");} else {NSLog (@ "element without a letter c ");} // create NSArray * twoArray = [NSArray arrayWithObjects: @ 1, @ 2, @ 3, @ 4, @ 5, nil]; // The iterator traverses the reverseObjectEnumerator array element to access NSEnumerator * arrayenumerator = [twoArray reverseObjectEnumerator]; id obj = nil; while (obj = [arrayenumerator nextObject]) {NSLog (@ "Current Value: % d", [obj intValue]);} // use the BlocksKit plug-in to traverse NSArray * frArray = [NSArr Ay arrayWithObjects: @ (0.2), @ (0.5), @ (0.9), nil]; [frArray bk_each: ^ (id obj) {NSLog (@ "% @", obj) ;}]; // compare it with the array item. If it does not exist, it is null and the matched value id found = [frArray bk_match: ^ BOOL (id obj) {BOOL match = ([obj floatValue] = 0.5 )? YES: NO; return match;}]; NSLog (@ "Valid values: % @", found); id notFound = [frArray bk_match: ^ BOOL (id obj) {BOOL match = ([obj floatValue] = 0.7 )? YES: NO; return match;}]; NSLog (@ "NO matching value (nil): % @", notFound ); // Filter Array NSArray * selectFound = [frArray bk_select: ^ BOOL (id obj) {BOOL match = ([obj floatValue]> 0.3 )? YES: NO; return match;}]; NSLog (@ "the number of currently filtered array arrays is: % ld", selectFound. count); for (id obj in selectFound) {NSLog (@ "currently compliant with % @", obj);} // filter the array to obtain the opposite NSArray * rejectFound = [frArray bk_reject: ^ BOOL (id obj) {BOOL match = ([obj floatValue]> 0.3 )? YES: NO; return match;}]; NSLog (@ "the current number of anti-array arrays is: % ld", rejectFound. count); for (id obj in rejectFound) {NSLog (@ "currently met (0.2 met) % @", obj );} // traverse the array and modify each value to form a new array NSArray * mapFound = [array bk_map: ^ id (id obj) {return [obj stringByAppendingString :@". png "] ;}]; for (id obj in mapFound) {NSLog (@" current map % @ ", obj );} // merge the values of the array // you can add a grid character in the middle of the array merging (here is the preceding one |; the merged string is: | cnblogs.com) NSString * concentrated = [array bk_reduce: @ "|" withBlock: ^ id (id sum, id obj) {return [sum stringByAppendingString: obj];}]; NSLog (@ "merged string: % @", concentrated); // Add an integer bk_performanceinteger float value = [frArray bk_performancefloat: 0 withBlock: ^ CGFloat (CGFloat result, id obj) {return result + [obj floatValue] ;}]; NSLog (@ "merged frArray value: % f", value );

 

2: Some Common NSMutableArray operations

// Convert from array to nsmutablearray NSArray * array = [[NSArray alloc] handler: @ "wujy", @ "cnblogs", nil]; NSMutableArray * mutableArray = [javasarraywitharray: array]; // create NSMutableArray * newMutableArray = [NSMutableArray array]; [newMutableArray addObject: @ "a"]; [newMutableArray usage: [NSArray alloc] callback: @ "B", @ "c", @ "d", @ "e", nil]; // iterator traverses reverseObjectEnumerato R array elements access NSEnumerator * arrayenumerator = [newMutableArray reverseObjectEnumerator]; id obj = nil; while (obj = [arrayenumerator nextObject]) {NSLog (@ "current value: % @ ", obj);} // create NSMutableArray * capacityArray = [[NSMutableArray alloc] failed: 5]; [capacityArray failed: [[NSArray alloc] failed: @ 1, @ 2, @ 3, nil]; NSLog (@ "before deletion (1, 2, 3): % @", capacityArray); // Delete the specified Element [capacit YArray removeObject: @ 3]; NSLog (@ "Delete (1, 2): % @", capacityArray); // insert [capacityArray insertObject: @ 4 atIndex: 2]; NSLog (@ "inserted (, 4) % @", capacityArray); // insert multiple nsindexsets * set = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange (0, 2)]; [capacityArray insertObjects: [[NSArray alloc] initWithObjects: @ 5, @ 6, nil] atIndexes: set]; NSLog (@ "insert multiple values (, 4): % @ ", capacityArray); // Blocks operation // filter [capacityArray bk _ Effecmselect: ^ BOOL (id obj) {BOOL match = ([obj intValue]> 2 )? YES: NO; return match;}]; NSLog (@ "the filtered array value is (5, 6, 4): % @", capacityArray ); // obtain the anti-filter [capacityArray bk_effecmreject: ^ BOOL (id obj) {BOOL match = ([obj intValue]> 5 )? YES: NO; return match;}]; NSLog (@ "the array value after filtering is (5, 4): % @", capacityArray ); // perform operations on the value of the array [capacityArray bk_effecmmap: ^ id (id obj) {return [NSNumber numberWithInt: [obj intValue] + 1];}]; NSLog (@ "the array value after map is (6, 5): % @", capacityArray );

 

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.