Why can’t NSArray and NSDictionary store basic types like int, float, bool?

Because NSArray and NSDictionary can only hold objects. Base types are not objects. So why can only store objects in NSArray and NSDictionary?

Since Objc’s object is really a pointer, from the point of view of Pointers, everything in arrays and dictionaries is of equal length. This makes it easy to handle operations like length, get, add, etc.

After using c arrays, Objc’s NSArray is pretty handy. Because of this point up here.

// Array

NSArray: Used to store ordered arrays. It is immutable and cannot store basic C data types (int, float, double, enum, struct). It cannot store nil. If you want to use basic data types, you need to convert them to Objective-C objects. Such as NSNumber. Also, the last element of the array must be nil, indicating closure.

// Create an array

NSArray *arr = [NSArray array]; ArrayWithArray: NSArray *arr1 = [NSArray arrayWithArray:arr]; NSArray *arr2 = @[@"Apple", @"DELL", @"Lenovo"]; //3, arrayWithObject: NSArray *arr3 = [NSArray arrayWithObject:@"Apple"]; //4, arrayWithObjects: NSArray *arr4 = [NSArray arrayWithObjects:@"Apple", @"Android", nil]; // 3, arrayWithObjects:count: NSString *str5[3] = {@"aaa", @"BBB", @"CCC"}; NSArray *arr5 = [NSArray arrayWithObjects:str5 count:2]; NSArray *arr6 = [[NSArray alloc] init]; arr6 = @[@"LLL", @"IIII", @"BBBB"]; //7, initWithArray: NSArray *arr7 = [[NSArray alloc] initWithArray:arr6]; InitWithObjects: NSArray *arr8 = [[NSArray alloc] initWithObjects:arr6, arr5, nil]; InitWithObjects :count: NSArray *arr9 = [[NSArray alloc] initWithObjects:str5 count:1]; NSMutableArray *array10 = [NSMutableArray arrayWithCapacity:10]; NSMutableArray * array11 = [[NSMutableArray alloc] initWithCapacity:10];Copy the code

// Array delete (NSMutableArray)

NSMutableArray *list = [[NSMutableArray alloc] init]; NSObject *car = [[NSObject alloc] init]; NSObject *car1 = [[NSObject alloc] init]; [list addObject:car]; [list addObject:car1]; NSLog(@"%@", list); [list removeAllObjects]; // Crash here NSLog(@"%@", list); // remove the last object [list removeLastObject]; NSLog(@"%@", list); // remove the specified Object [list removeObject:car]; NSLog(@"%@", list); // removeObject:inRange: // Remove car1 NSRange = {1, 1}; [list removeObject:car1 inRange:range]; NSLog(@"%@", list); RemoveObjectAtIndex: [list removeObjectAtIndex:3]; NSArray *arr00 = @[car]; NSArray *arr00 = @[car]; [list removeObjectsInArray:arr00]; NSLog(@"%@", list); / / 7, removeObjectsInRange: / / delete specified range Object [list removeObjectsInRange: range]; NSLog(@"%@", list);Copy the code

// NSMutableArray

ArrayByAddingObject Returns a new array NSArray *array = @[@"AAA", @"BBB", @"CCC"]; NSString * str11 = @"bb989"; NSLog(@"%@", [array arrayByAddingObject:str11]); / / 2. ArrayByAddingObjectsFromArray combination two arrays NSLog (@ "% @", [array arrayByAddingObjectsFromArray: array]); / / 3. com ponentsJoinedByString array into a string NSLog (@ "% @", [array componentsJoinedByString: @ ", "]); NSMutableArray *array4 = [NSMutableArray arrayWithObjects:@"aaa", @" BBBB ", nil]; [array4 addObject:@"CCC"]; NSLog(@"%@", array4); / / 5. AddObjectsFromArray add formatting to an array array [array4 addObjectsFromArray: array]; NSLog(@"%@", array4); [array4 insertObject:@"OOO" atIndex:1]; NSLog(@"%@", array4); / / 7. ReplaceObjectAtIndex: withObject replace the specified subscript elements [array4 replaceObjectAtIndex: 1 withObject: @ "PPP"); NSLog(@"%@", array4); / / 8. SetArray to replace the original array NSArray * array8 = @ [@ "A" @ "B", @ "C"); [array4 setArray:array8]; NSLog(@"%@", array4); / / 9. ExchangeObjectAtIndex: withObjectAtIndex exchange specified subscript elements [array4 exchangeObjectAtIndex: 0 withObjectAtIndex: 1); NSLog(@"%@", array4);Copy the code

// array query

NSString *str = @"CCC"; NSArray *array = @[str]; If ([array containsObject: STR]) {NSLog(@"array contains STR "); NSArray *array2 = [NSArray arrayWithObjects:@"AAA", @"BBBB", nil]; NSLog(@"count = %zi", array2.count); NSMutableArray *array5 = [NSMutableArray arrayWithObjects:@"aaa", @" BBBB ", @" CCC ",nil]; NSLog(@"%@", [array5 lastObject]); NSLog(@"%@", [array5 objectAtIndex:1]); [array5 indexOfObject:@" %li", [array5 indexOfObject:@" BBBB "]); / / 6. IndexOfObject: inRange returned within the specified object subscript NSRange range8 = NSMakeRange (0, 3); NSLog(@"%li", [array5 indexOfObject:@"ccc" inRange:range8]); / / 7. FirstObjectCommonWithArray return to the first two sets to the same object element NSMutableArray * array6 = [NSMutableArray arrayWithObjects: @ "aaa1." @"bbbb1", @"ccc",nil]; NSLog(@"%@", [array5 firstObjectCommonWithArray:array6]); NSLog(@"%@", [array5 subarrayWithRange:range8]);Copy the code

// array sort

/ / 1. Reverse reverseObjectEnumerator array output NSArray * array8 = [NSArray arrayWithObjects: @ "one," @ "two," @ "three," nil]; for (NSString *str in [array8 reverseObjectEnumerator]) { NSLog(@"%@", str); }Copy the code

//

/ / define an array NSArray * arr = @ [@ "one," @ "two," @ "three," @ "four"); For (int I = 0; int I = 0; i < arr.count; i++) { NSLog(@"-> %@",arr[i]); } //2) for (NSString * STR in arr) {NSLog(@"-- > %@", STR); } / / 3) the use of block, visit / / stop: YES will stop, stop, NO will not stop [arr enumerateObjectsUsingBlock: ^ (id obj, NSUInteger independence idx. BOOL * stop) {if (independence idx = = 2) {* stop = YES; / / stop / / break;} else {NSLog (@ "independence idx = % ld, obj = % @", independence idx, obj);}}];Copy the code

// 8

NSArray *array = [NSArray array];
NSArray *array1 = [NSArray array];
if ([array isEqualToArray:array1]) {
    NSLog(@"YES");
}else {
    NSLog(@"NO");
}
Copy the code

// NSArray reads and writes files:

NSArray *array = [NSArray arrayWithObjects:@"one",@"zbz",@"cgx",@"sb",@"cjk",@"senni", nil]; // Write NSArray contents to a file --arr. Plist A special file format BOOL isWrite = [array writeToFile:@"/Users/apple/Desktop/testArray/arr.plist" atomically:YES]; If (isWrite){NSLog(@" write successfully "); } / / from a file, read an array of information NSArray * readArr = [NSArray arrayWithContentsOfFile: @ "/ Users/apple/Desktop/testArray/arr. Plist"]. NSLog(@"readArr = %@",readArr);Copy the code

// NSArray splits and merges

NSString *string = @"One,Two,Three,Four"; NSArray *strArray = [string componentsSeparatedByString:@","]; NSLog(@"array:%@",strArray); NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil]; NSString *str = [array componentsJoinedByString:@","]; NSLog(@"string:%@",str);Copy the code