目标C中的全局变量

时间:2012-09-10 17:08:24

标签: objective-c

我是Objective C的新手并且已经坚持这个问题已经5天了))我要做的是为城市和大都市的简单任务编写实现。我有类具有属性和类都市的类City,它具有通过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.

这是我的实施:

City.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

City.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

Metropolis.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

Metropolis.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

的main.m

#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;
}

3 个答案:

答案 0 :(得分:1)

我已经重写了我的答案,使其更加完整,并将其他答案中产生的其他一些想法纳入其中,尤其是@Hannes Sverrisson

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

更好的方法,更好的意思是更符合典型的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方法。

答案 1 :(得分:1)

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

因此,只需在您的Metropolis实施中将- (void)initialize {更改为-(void)init {,然后删除主广告中的[Metropolis initialize];行。

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

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

答案 2 :(得分: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.nameself.ageself.population访问属性,或者如果您需要自己访问支持变量,_name_age,{ {1}}。