1.0----6存储项目,并从集合检索他们

存储项目,并从集合检索他们                                                                     ios7cook目录
集合是对象的实例,可以容纳其他对象。其中一个主要的收藏是一个数组,它实例化或者NSArray的或NSMutableArray的。可以存储任何对象在数组中,并且一个阵列可以包含相同的对象的多个实例。这里是我们创建三个字符串数组的例子:

    NSArray *stringsArray = @[
                              @"String 3"
];
    __unused NSString *firstString = stringsArray[0];
    __unused NSString *secondString = stringsArray[1];
    __unused NSString *thirdString = stringsArray[2];

该__unused宏告诉编译器不要抱怨一个变量,如本例中的firstString变量被声明但从未使用时。编译器的默认行为是,它抛出一个警告,控制台说一个变量没有使用。我们简单的例子已经声明的变量,但没有使用过,所以添加上述宏变量声明的开头保持编译器和自己幸福。

一个可变数组是可以突变,它已被创建后改变数组。一个不可变的数组,就像我们看到的,在创建后用不能被篡改。这里是一个不变的阵列的示例:

    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];

NSLog(@"Immutable array = %@", immutableArray);
    NSLog(@"Mutable Array = %@", mutableArray);
//The output of this program is as follows:
    Immutable array = (
        "String 1",
        "String 2",
"String 3"
    )
    Mutable Array = (
"String 1",
"String 3"
)

另外在整个iOS的程序中很常见的集合是一本字典。字典像数组,但是字典中的每个对象分配一个键,这样以后就可以使用键获取相同的对象。下面是一个例子:

NSDictionary *personInformation =
    @{
      @"firstName" : @"Mark",
      @"lastName" : @"Tremonti",
      @"age" : @30,
      @"sex" : @"Male"
};
    NSString *firstName = personInformation[@"firstName"];
    NSString *lastName = personInformation[@"lastName"];
    NSNumber *age = personInformation[@"age"];
    NSString *sex = personInformation[@"sex"];
    NSLog(@"Full name = %@ %@", firstName, lastName);
    NSLog(@"Age = %@, Sex = %@", age, sex);
The output of this program is:
    Full name = Mark Tremonti
    Age = 30, Sex = Male

你也可以有可变的字典,就像你可以有可变数组。可变词典“的内容,才能被实例化后更改。下面是一个例子:

 NSDictionary *personInformation =
    @{
      @"firstName" : @"Mark",
      @"lastName" : @"Tremonti",
      @"age" : @30,
      @"sex" : @"Male"
};
    NSMutableDictionary *mutablePersonInformation =
        [[NSMutableDictionary alloc] initWithDictionary:personInformation];
mutablePersonInformation[@"age"] = @32;

NSLog(@"Information = %@", mutablePersonInformation); The output of this program is:

    Information = {
        age = 32;
        firstName = Mark;
        lastName = Tremonti;
        sex = Male;

 

您还可以利用套的优势。台像数组,但必须包含一个唯一的组对象。你不能两次添加对象的同一实例中,以相同的一组。下面是一个例子:

 NSSet *shoppingList = [[NSSet alloc] initWithObjects:
                           @"Milk",
                           @"Bananas",
                           @"Bread",
                           @"Milk", nil];
NSLog(@"Shopping list = %@", shoppingList);

If you run this program, the output will be:
Shopping list = {(
Milk,
Bananas,
Bread )}

注意:在我们的程序中提到milk两次,但添加到集合一次。这就是背后套魔术。您还可以使用可变套,像这样:

NSSet *shoppingList = [[NSSet alloc] initWithObjects:
                           @"Milk",
                           @"Bananas",
                           @"Bread",
                           @"Milk", nil];
    NSMutableSet *mutableList = [NSMutableSet setWithSet:shoppingList];
    [mutableList addObject:@"Yogurt"];
    [mutableList removeObject:@"Bread"];
    NSLog(@"Original list = %@", shoppingList);
NSLog(@"Mutable list = %@", mutableList);

And the output is:

Original list = {(
Milk,
Bananas,

Bread )}

Mutable list = {(
Milk,
Bananas,

Yogurt )}

有迹象表明,你需要知道,现在我们正在谈论集和集合其他两个重要的类:
NSOrderedSet
一个不可变的集合,保持在该对象被加入到它的顺序
NSMutableOrderedSet
有序集的可变版本
默认情况下,NSsets不保持在对象被加入到它们的顺序。以下面的例子:

NSSet *setOfNumbers = [NSSet setWithArray:@[@3, @4, @1, @5, @10]];
    NSLog(@"Set of numbers = %@", setOfNumbers);
What gets printed to the screen after you run this program is:
    Set of numbers = {(
        5,
10, 3, 4, 1
)}

但是,这不是在我们创建的设置的顺序。如果您想保留的顺序不变,只需使用NSOrderedSet类代替:

NSOrderedSet *setOfNumbers = [NSOrderedSet orderedSetWithArray
                                  :@[@3, @4, @1, @5, @10]];
NSLog(@"Ordered set of numbers = %@", setOfNumbers);
And, of course, you can use the mutable version of an ordered set:
    NSMutableOrderedSet *setOfNumbers =
        [NSMutableOrderedSet orderedSetWithArray:@[@3, @4, @1, @5, @10]];
    [setOfNumbers removeObject:@5];
    [setOfNumbers addObject:@0];
    [setOfNumbers exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"Set of numbers = %@", setOfNumbers);
The results are shown here:
    Set of numbers = {(
        3,

1, 4, 10, 0

)} 

在我们继续之前关闭的set话题,还有另外一个,你可能需要了解得心应手类。该NSCountedSet类可以容纳一个对象多次的唯一实例。然而,这样做的方法是从阵列执行相同任务的方式有所不同。在一个阵列,相同的对象可以出现多次。但在数集,该对象将只出现一次,而是集保持的对象是多少次加入到集数和每次你删除该对象的实例会递减的计数器。下面是一个例子:

NSCountedSet *setOfNumbers = [NSCountedSet setWithObjects:
                                  @10, @20, @10, @10, @30, nil];
    [setOfNumbers addObject:@20];
    [setOfNumbers removeObject:@10];
NSLog(@"Count for object @10 = %lu",
(unsigned long)[setOfNumbers countForObject:@10]);
NSLog(@"Count for object @20 = %lu",
(unsigned long)[setOfNumbers countForObject:@20]);
The output is:
    Count for object @10 = 2
    Count for object @20 = 2

该NSCountedSet类是可变的,尽管它的名字可能会导致你的想法。

 

 1.0----7添加对象的下标来支持你的类

 

 

posted @ 2014-10-19 14:40  尐苹果  阅读(129)  评论(0编辑  收藏  举报