Objective-C:将NSStrings相互关联?

时间:2013-08-25 06:07:12

标签: ios objective-c

我对代码一般都很新,而且我正在学习Objective-C,所以如果我错误地说出这个问题,我会提前道歉。

我已经创建了几个以这个目标为目标的数组:

  • 显示有多少人并告诉他们的名字

  • 显示有多少个城市并告诉他们的名字

  • 将人员分配到城市并计算人口

我已完成该任务,但我已将每个人分别分配给每个城市的阵列。有没有办法让我简单地联系NSStrings?例如“blockCity = bill,bob,jim”而不是为人群创建一个新数组?

// My people

    NSString *bill = (@"Bill");
    NSString *bob = (@"Bob");
    NSString *jim = (@"Jim");
    NSString *kevin = (@"kevin");
    NSString *stacy = (@"stacy");
    NSString *cooper = (@"Cooper");

    NSMutableArray *people = [NSMutableArray arrayWithObjects: bill, bob, jim, kevin, stacy, cooper, nil];


        NSLog(@"Here are my people: %@", people);
        NSLog(@"I have %lu people", [people count]);

// My places

    NSString *blockCity = (@"BlockCity");
    NSString *hyperCity = (@"HyperCity");
    NSString *pixelTown = (@"PixelTown");
    NSString *nowhere = (@"Nowhere");

    NSMutableArray *cities = [NSMutableArray arrayWithObjects: blockCity, hyperCity, pixelTown, nowhere, nil];

        NSLog(@"Cities: %@", cities);
        NSLog(@"There are %lu cities", [cities count]);

//populations

   // BlockCity population
    NSMutableArray *bcpop = [NSMutableArray arrayWithObjects: bill, bob, jim, nil];

        if ([bcpop count] == 0) {
            NSLog(@"%@ is abandoned.", blockCity);

        } else {

            NSLog(@"%@ has a population of %lu", blockCity, [bcpop count]);
        }

    //HyperCity population
    NSMutableArray *hcpop = [NSMutableArray arrayWithObjects: nil];

        if ([hcpop count] == 0) {
            NSLog(@"%@ is abandoned.", hyperCity);

        } else {

            NSLog(@"%@ has a population of %lu", hyperCity, [hcpop count]);
        }

    //PixelTown population
    NSMutableArray *ptpop = [NSMutableArray arrayWithObjects: kevin, stacy, cooper, nil];

        if ([ptpop count] == 0) {
            NSLog(@"%@ is abandoned.", pixelTown);

        } else {

            NSLog(@"%@ has a population of %lu", pixelTown, [ptpop count]);
        }

    //Nowhere population
    NSMutableArray *npop = [NSMutableArray arrayWithObjects: nil];

    if ([npop count] == 0) {
        NSLog(@"%@ is abandoned.", nowhere);

    } else {

        NSLog(@"%@ has a population of %lu", nowhere, [npop count]);
    }

2 个答案:

答案 0 :(得分:3)

不,没有内置的方法可以“关联NSStrings”。 1

在这里做的恰当的事情是开始以面向对象的方式思考你的设计。

程序中有哪些不同类型的对象?有人,所以Person类是合适的。为Person类提供name属性(NSString)。

有城市,所以City类是合适的。为City类提供name属性(NSString),inhabitants属性(NSArrayNSMutableArray以填充引用Person个对象)以及返回居民数量的population方法(返回unsigned long)。

一旦掌握了所有这些,您甚至可以给City一个populationDescription方法,该方法返回其总体的字符串描述,如下所示:

- (NSString *)populationDescription {
    if (self.population == 0) {
        return [NSString stringWithFormat:@"%@ is abandoned.", self.name];
    } else {
        return [NSString stringWithFormat:@"%@ has a population of %lu", self.name, self.population);
    }
}

<小时/> 脚注1.对于学生:相关对象不计算在内。它们并不适合这种情况,而且对于这个级别的学习者来说它们太先进了。

答案 1 :(得分:0)

我建议在这种情况下使用NSMutableDictionary将人与城市联系起来。类似的东西:

NSMutableArray *people = [NSMutableArray arrayWithObjects: bill, bob, jim, kevin, stacy, cooper, nil];
NSMutableDictionary *cityWithPeople = [[NSMutableDictionary alloc] init];
[cityWithPeople setObject:people forKey:@"CityName"]; // to add people related to that city

NSArray peopleInCity=[cityWithPeople objectForKey:key]; // return array of people in city

此处read也知道如何使用NSDictionary