私は一般的にコードに非常に慣れておらず、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]);
}