IOS Study Notes 2015-03-20 OC-array, 2015-03-20oc-
[NSArray] the element of an array in Definition 1 immutable array 2 oc can be an element of any object 3. The address of the element contained in the number 2. initialize NSArray * variable = [[NSArry alloc] initWithObjects: object ..., nil]; 3 Use Case 1 traverse array A enumeration NSEnumerator * enums = [array1 objectEnumerator]; while (obj = [enums nextObject]) B subscript for (I = 0; I <count; I ++) {C rapid enumeration method for (id obj in array1) 2 get length int count = [array object count]; [NSMutableArray] 1 Definition 1 variable array 2 can insert any object 2 initialize NSMutableArray * variable name = [[NSMutableArray alloc] init]; 3 Use scenario 1 add object [mutableArray addObject: @ "JACK"]; 2. Replace the subscript with the object [mutableArray exchangeObjectAtIndex: 0 withObjectAtIndex: 2]; 3. Remove the object [mutableArray removeObject: person]; [mutableArray removeObjectAtIndex: 0]; 4. Sequential enumeration traversing NSEnumerator * _ enums = [mutableArray objectEnumerator]; 5. Inverted enumeration traversing NSEnumerator * _ reverseEnums = [mutableArray reverseObjectEnumerator]; 6 conversion of strings and arrays NSArray * _ strArray = [str componentsSeparatedByString: @ ""]; NSString * str = [_ strMutableArray componentsJoinedByString: @ ""];
//// Main. m // OC-array /// Created by wangtouwang on 15/3/20. // Copyright (c) 2015 wangtouwang. all rights reserved. // # import <Foundation/Foundation. h> # import "Person. h "int main (int argc, const char * argv []) {@ autoreleasepool {// immutable array NSArry NSLog (@" NSArry "); person * person = [Person new]; person. _ name = @ "JACK"; // initialize NSArray * array1 = [[NSArray alloc] initWithObjects: @ "jack", @ "lucy", person, nil]; NSLog (@ "% @", array1); // enumerative NSLog (@ "the enumerated method prints the array Result:"); NSEnumerator * enums = [array1 objectEnumerator]; id obj; while (obj = [enums nextObject]) {NSLog (@ "% @", obj);} // you can quickly enumerate NSLog (@ "the quick enumeration method prints the array Result :"); for (id obj in array1) {NSLog (@ "% @", obj);} // subscripts facilitate NSInteger I = 2; id obj2 = [array1 objectAtIndex: I]; NSLog (@ "% @", obj2); // obtain the length NSInteger count = [array1 count]; NSLog (@ "array length % lu", count); I = 0; for (I = 0; I <count; I ++) {NSLog (@ "% @", [array1 objectAtIndex: I]);} // variable array NSMutableArray * mutableArray = [[Alibaba alloc] init]; // Add [mutableArray addObject: @ "JACK"]; [mutableArray addObject: @ "MOBILE"]; [mutableArray addObject: @ "TEL"]; [mutableArray addObject: @ "BOOK"]; [mutableArray addObject: @ "STUDENT"]; [mutableArray addObject: person]; NSLog (@ "% @", mutableArray); // Replace the NSLog object (@ "replace object"); [mutableArray exchangeObjectAtIndex: 0 withObjectAtIndex: 2]; NSLog (@ "% @", mutableArray); // remove NSLog (@ "Remove object parameter id type"); [mutableArray removeObject: person]; NSLog (@ "% @", mutableArray); // remove the NSInteger NSLog (@ "Remove object parameter NSInteger"); [mutableArray removeObjectAtIndex: 0]; NSLog (@ "% @", mutableArray); // NSLog (@ "Forward enumeration traversal"); NSEnumerator * _ enums = [mutableArray objectEnumerator]; id obj3; while (obj3 = [_ enums nextObject]) {NSLog (@ "% @", obj3 );} // NSLog can be retrieved in reverse order and deleted over time (@ "reverse enumeration traversal and can be deleted over time"); NSEnumerator * _ reverseEnums = [mutableArray reverseObjectEnumerator]; while (obj3 = [_ reverseEnums nextObject]) {NSLog (@ "% @", obj3); [mutableArray removeObject: obj3];} NSLog (@ "view array length % lu after inverted enumeration traversal", [mutableArray count]); // NSString * str = @ "hello world hao are you"; NSArray * _ strArray = [str componentsSeparatedByString: @ ""]; NSLog (@ "lenght = % lu", [_ strArray count]); NSMutableArray * _ strMutableArray = [[NSMutableArray alloc] init]; NSEnumerator * _ strEnums = [_ strArray reverseObjectEnumerator]; NSString * objstring; while (objstring = [_ strEnums nextObject]) {[_ strMutableArray addObject: objstring];} NSLog (@ "% @", _ strMutableArray); str = [_ strMutableArray componentsJoinedByString: @ ""]; NSLog (@ "% @", str);} return 0 ;}