IOS NSArray,NSDictionary

小结:

NSArray有序的集合;

NSDictionary无序的集合,可排序; 增删改查

------NSArray-----------

 create :

1)NSArray *array = [NSArray arrayWithObjects:@"Henry",@"Jones", @"Susan", @"Smith", @"Patty", @"Johnson", nil]; 

2)NSArray *myArray = [NSArray arrayWithArray:array];

NSLog(@"%@", myArray); 

3) NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"Foo", @"Bar", @"FooBar", nil]; 

4) NSMutableArray *array2 = [NSMutableArray arrayWithCapacity: 3];

//Add an object

[array2 addObject: @"Foo"];

//Add another object

[array2 addObject: @"Bar"];

//Insert an object at a particular index

[array2 insertObject: @"FooBar" atIndex: 1]; 

5) int n = 15;

NSMutableArray *numberArray = [[NSMutableArray alloc] initWithCapacity:n];

//srand(time(0));

srandom(time(NULL));

for(int i = 0; i < n; i++)

[numberArray addObject:[NSNumber numberWithInt:arc4random()%n]];

NSLog( @"%@", numberArray);

sort:

NSArray *sortedArray = 

  [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; 

---------dictionary--------------

NSArray  *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil];

    NSArray *objects = [NSArray arrayWithObjects:@"How", @"are", @"you", nil];

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

//Case 1, loop through 

for (id key in dictionary) {

        NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);

    }

//Case 2, loop through 

NSEnumerator *enumerator;

    id key;

enumerator = [dictionary keyEnumerator];    

    while ((key = [enumerator nextObject])){  

        NSLog(@"%@====>%@", key, [dictionary objectForKey:key]);

    }

原文地址:https://www.cnblogs.com/csj007523/p/2600889.html