数组
NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",nil];
NSArray *array1 = [[NSArray alloc] initWithArray:array];
NSLog(@"array = %@,array1 = %@",array,array1);
NSArray *array2 = [NSArray arrayWithObjects:@"3",@"4", nil];
NSInteger age = 10;
NSArray *array3 = @[@"5",@"6",@(age)];
NSLog(@"array3 = %@",array3);
NSUInteger count = array3.count;
[array3 count];
数组的运算:
NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",nil];
NSArray *array1 = [[NSArray alloc] initWithArray:array];
NSLog(@"array = %@,array1 = %@",array,array1);
NSArray *array2 = [NSArray arrayWithObjects:@"3",@"4", nil];
NSInteger age = 10;
NSArray *array3 = @[@"5",@"6",@(age)];
NSLog(@"array3 = %@",array3);
NSUInteger count = array3.count;
[array3 count];
数组的排序
NSArray *array = @[@"12",@"3",@"6",@"2",@"1"];
NSArray *sortArray = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"sort = %@",sortArray);
NSArray *sortArray1 = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSInteger a = [obj1 integerValue];
NSInteger b = [obj2 integerValue];
if (a > b) {
return -1;
}else if(a == b){
return 0;
}else{
return 1;
}
}];
NSLog(@"sort1 = %@",sortArray1);
数组的遍历
NSArray *array = @[@"12",@"abcde",@6,@"2",@"1"];
for (int i = 0; i < array.count ; i ++) {
NSLog(@"%@",array[i]);
NSLog(@"%@",[array objectAtIndex:i]);
}
for (id object in array) {
NSLog(@"object = %@",object);
}
NSMutableArray
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
[mutableArray addObject:@"3"];
NSLog(@"mutableArray = %@",mutableArray);
[mutableArray removeObjectAtIndex:0];
NSLog(@"mutableArray = %@",mutableArray);
NSInteger index = [mutableArray indexOfObject:@"2"];
NSLog(@"index = %ld",index);
[mutableArray replaceObjectAtIndex:0 withObject:@"000"];
NSLog(@"mutableArray = %@",mutableArray);
[mutableArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"mutableArray = %@",mutableArray);
iOS9 新特性
NSArray<NSString *> *array = @[@"hello",@"world"];
NSMutableArray<NSString *> *mutableArray = [NSMutableArray arrayWithObjects:@"uu", nil];
[mutableArray addObject:@"3"];
字典NSDictionary
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];
NSLog(@"dictionary = %@",dictionary);
NSDictionary *newDictionary = @{
@"1" : @"one",
@"2" : @"two"
};
NSLog(@"dictionary = %@",newDictionary);
NSLog(@"%ld",dictionary.count);
NSString *string = dictionary[@"1"];
NSLog(@"%@",string);
NSString *string1 = [dictionary objectForKey:@"1"];
NSLog(@"%@",string1);
NSArray *keys = [dictionary allKeys];
NSArray *values = [dictionary allValues];
#pragma mark - 字典的遍历
for (int i = 0; i < dictionary.count; i ++) {
NSString *key = [keys objectAtIndex:i];
NSString *value = [dictionary objectForKey:key];
NSLog(@"value = %@",value);
}
for (id object in dictionary) {
NSLog(@"object = %@",dictionary[object]);
}
NSMutableDictionary
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2", nil];
[mutableDictionary setValue:@"three" forKey:@"3"];
NSLog(@"mutable = %@",mutableDictionary);
[mutableDictionary removeObjectForKey:@"3"];
NSLog(@"mutable = %@",mutableDictionary);
NSLog(@"%@",[mutableDictionary objectForKey:@"1"]);
[mutableDictionary setValue:@"three" forKey:@"1"];
NSLog(@"mutable = %@",mutableDictionary);
[mutableDictionary setObject:@"four" forKey:@"2"];
NSLog(@"mutable = %@",mutableDictionary);
NSDictionary *dictionary1 = @{ @"A": @"YES",
@"B":
@{@"math" : @"tom",
@"book" : @[@"english",@"chinese"]
}
};
NSLog(@"%@", dictionary1[@"B"][@"book"][0]);
NSSet
NSSet *set = [NSSet setWithObjects:@"1",@"1",@"2",@"2", nil];
NSLog(@"set = %@",set);
NSLog(@"set1 = %@", [set anyObject]);
NSLog(@"set2 = %@", [set allObjects]);