0

我是Objective C的新手,并且已经在这个问题上停留了5天))我要做的是为关于城市和大都市的简单任务编写实现。我有具有属性的 City 类和具有全局数组的类 Metropolis,该数组通过 createCity 方法添加城市对象。我已经实现了这个任务,但是这个数组什么也没返回。有谁能够帮助我?

这是任务的一部分:

1. Write a “City” class, which inherits from NSObject. Your class should contain the following:
Variables:
name, age, population.
Instance methods:
setName:age:population (single method) which set city’s name, age and population. getName, getAge, getPopulation which return city’s name, age and population, respectfully.
nextDay which adds a random number to city’s population, then subtracts a random number from city’s population. Figure out a way to generate random numbers yourself.
2. Create an instance of City class, set its name, age and population as you want.
3. Write a for-­‐loop (if in doubt how to do it – google or use Xcode’s help system) for 10 steps. Each step send ‘nextDay’ message to your object and print out the population.

    4. Write a “Metropolis” class. It should contain the following:
Variable:
array of 10 cities.
Instance method:
createCity:atIndex:withPopulation: (single method) which creates a city with first parameter being a name at index (from the second parameter) and sets its population to that of third parameter. So, you should be able to do this:
[myMetropolis createCity: @”Almaty” atIndex: 2 withPopulation: 1500000]
5. Create an instance of Metropolis class and create all 10 cities.

这是我的实现:

城市.h

#import <Foundation/Foundation.h>

@interface City : NSObject
{
    NSString* name;
    int age;
    int population;
}

-(void)setName: (NSString*)n age: (int)a population: (int)p;
-(NSString*)getName;
-(int)getAge;
-(int)getPopulation;
-(void)nextDay;


@end

城市.m

#import "City.h"

@implementation City

-(void)setName:(NSString*)n age:(int)a population:(int)p
{
    name = n;
    age = a;
    population = p;
}

-(NSString*)getName
{
    return name;
}

-(int)getAge
{
    return age;
}

-(int)getPopulation
{
    return population;
}

-(void)nextDay
{
    int r = arc4random() % 100;
    int r2 = arc4random() % 100;
    population = population + r;
    population = population - r2;

}


@end

大都会.h

#import <Foundation/Foundation.h>
    #import "City.h"

@interface Metropolis : NSObject{
        NSMutableArray* myArray;
    }



-(void)createCity: (NSString*)n atIndex: (int)a withPopulation: (int)p;

-(NSMutableArray*) getArray;

@end

大都会.m

#import "Metropolis.h"
#import "City.h"
@implementation Metropolis

NSMutableArray* myArray = nil;
- (void)initialize {
        myArray = [[NSMutableArray alloc]initWithCapacity:10];
}

-(void)createCity:(NSString*)n atIndex:(int)a withPopulation:(int)p
{
    City* newCity = [[City alloc]init];
    [newCity setName:n age:0 population:p];

    [myArray insertObject:newCity atIndex:a];

}
-(NSMutableArray*)getArray
{
    return myArray;
}




@end

主文件

#import <Foundation/Foundation.h>
#import "City.h"
#import "Metropolis.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Metropolis* myMetropolis = [[Metropolis alloc]init];
        [myMetropolis createCity:@"Aktobe" atIndex:0 withPopulation:15];
        [Metropolis initialize];
        NSMutableArray* c = [[NSMutableArray alloc]init];
        c = [myMetropolis getArray];


        NSLog(@"%@", [[c objectAtIndex:0] getName]);

    }
    return 0;
}
4

3 回答 3

1

我重写了我的答案以使其更加完整,并结合了其他答案中产生的一些其他想法,尤其是@Hannes Sverrisson

解决问题的简单方法是在 createCity 之前调用 initialize (否则您尝试将对象添加到 nil 数组),并确保您没有从静态上下文调用 initialize 。即改变【大都会初始化】;到 [myMetropolis 初始化];

更好的方法,更好的意思是与典型的 Objective-C 设计更一致,你应该重写实例方法 init。这是在 Metropolis 实现中完成的,并替换了您的初始化方法。

-(id) init { 
    self = [super init]; 
    if (self) { 
        myArray = [[NSMutableArray alloc]initWithCapacity:10]; 
    } 
    return self; 
} 

或者为了更有趣,创建一个以城市数量为参数的新 init 方法。

    -(id) initWithNumberOfCities:(NSInteger)numCities { 
    self = [super init]; 
    if (self) { 
        myArray = [[NSMutableArray alloc]initWithCapacity:numCities]; 
    } 
    return self; 
} 

然后在您的 main 方法中,删除对 [Metropolis initialize] 的调用。原因是当你说:

Metropolis* myMetropolis = [[Metropolis alloc]init];

或者

Metropolis* myMetropolis = [[Metropolis alloc]initWithNumberOfCities:10];

init 方法在分配发生后被内联调用。

于 2012-09-10T17:14:01.240 回答
1

初始化的方法是-(void)init;这个方法应该在你的 Metropolis 实现中被覆盖。在这种情况下,您正在调用- (void)initialize;这是错误的。

因此,只需在 Metropolis 的实现中更改- (void)initialize {-(void)init {并删除行:[Metropolis initialize];in main。

在下面的注释之后,正确的 init 方法应该是:

-(id) init { 
self = [super init]; 
if (self) { 
    myArray = [[NSMutableArray alloc]initWithCapacity:10]; 
    } 
    return self; 
} 
于 2012-09-10T17:24:35.263 回答
0

您不需要编写 getter 或创建支持实例变量。您可以使用 Objective-C 2.0 的 @property 语法。

@property (strong) NSString *name;
@property (assign) NSInteger age;
@property (assign) NSInteger population;

- (void)setName:(NSString*)name age:(NSInteger)age population:(NSInteger)population;
- (void)nextDay;

然后使用self.name,访问属性self.ageself.population或者如果您需要访问支持变量本身,_name, _age, _population

于 2012-09-10T17:27:13.803 回答