0

一般来说,我对代码很陌生,而且我正在学习 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]);
    }
4

2 回答 2

3

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

在这里要做的适当的事情是开始以面向对象的方式考虑您的设计。

你的程序中有哪些不同类型的对象?人多,Person上课合适。给Person类一个name属性(an NSString)。

有城市,所以一个City班级是合适的。给City类一个name属性(an NSString),一个inhabitants属性(anNSArrayNSMutableArray要填充对Person对象的引用)和一个返回居民数量的population方法(returning 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. 对于学究:关联的对象不计算在内。它们不适合这个,而且对于这个级别的学习者来说太先进了。

于 2013-08-25T06:18:34.823 回答
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

请阅读此处以了解如何使用NSDictionary

于 2013-08-25T06:19:51.267 回答