NSInteger myNumber = -20;
NSString *myString = @"Objective-C is great!";
数组初始化
NSArray *stringsArray = @[ @"String 1"];
__unused NSString *firstString = stringsArray[0];
NSString *string1 = @"String 1"; NSString *string2 = @"String 2"; NSString *string3 = @"String 3";NSArray *immutableArray = @[string1, string2, string3]; NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithArray:immutableArray];[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:1]; [mutableArray removeObjectAtIndex:1]; [mutableArray setObject:string1 atIndexedSubscript:0];
字典初始化
NSDictionary *personInformation = @{@"firstName" : @"Mark", @"lastName" : @"Tremonti", @"age" : @30, @"sex" : @"Male"};
NSString *firstName = personInformation[@"firstName"];集合初始化1
NSSet *shoppingList = [[NSSet alloc] initWithObjects: @"Milk",@"Bananas", @"Bread", @"Milk", nil];NSMutableSet *mutableList = [NSMutableSet setWithSet:shoppingList]; [mutableList addObject:@"Yogurt"];[mutableList removeObject:@"Bread"];//集合初始化2NSSet *setOfNumbers = [NSSet setWithArray:@[@3, @4, @1, @5, @10]]; NSLog(@"Set of numbers = %@", setOfNumbers);NSMutableOrderedSet *setOfNumbers = [NSMutableOrderedSet orderedSetWithArray:@[@3, @4, @1, @5, @10]];[setOfNumbers removeObject:@5]; [setOfNumbers addObject:@0]; [setOfNumbers exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"Count for object @10 = %lu",
(unsigned long)[setOfNumbers countForObject:@10]);
NSString *const kFirstNameKey = @"firstName";NSString *const kLastNameKey = @"lastName";
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];或[dictionary setValue:@"Tim" forKey:kFirstNameKey]; [dictionary setValue:@"Cook" forKey:kLastNameKey];__unused NSString *firstName = [dictionary valueForKey:kFirstNameKey]; __unused NSString *lastName = [dictionary valueForKey:kLastNameKey];
SString *const kFirstNameKey = @"firstName";NSString *const kLastNameKey = @"lastName";
NSDictionary *dictionary = @{ kFirstNameKey : @"Tim",kLastNameKey : @"Cook", };__unused NSString *firstName = dictionary[kFirstNameKey]; __unused NSString *lastName = dictionary[kLastNameKey];
NSArrary>>>>
NSArray *array = [[NSArray alloc] initWithObjects:@"Tim", @"Cook", nil]; __unused NSString *firstItem = [array objectAtIndex:0]; __unused NSString *secondObject = [array objectAtIndex:1];或
NSArray *array = @[@"Tim", @"Cook"]; __unused NSString *firstItem = array[0]; __unused NSString *secondObject = array[0];