繁体   English   中英

目标C中的全局变量

[英]global variable in 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;
}

我重写了我的答案,使其更加完整,并将其他答案中产生的其他一些想法纳入其中,尤其是@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方法。

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

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

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

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

您不需要编写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_population

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM