0

私はObjectiveCを初めて使用し、この問題にすでに5日間取り組んでいます))私がしなければならないのは、都市と大都市に関する簡単なタスクの実装を作成することです。プロパティを持つクラスCityと、createCityメソッドを介してcityオブジェクトを追加するグローバル配列を持つクラスmetropolisがあります。このタスクを実装しましたが、この配列は何も返しません。誰か助けてもらえますか?

タスクの一部は次のとおりです。

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

次に、メイン メソッドで、[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 {し、main の行: を削除するだけです。-(void)init {[Metropolis initialize];

以下のコメントの後、適切な init メソッドは次のようになります。

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

ゲッターを記述したり、バッキング インスタンス変数を作成したりする必要はありません。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.age、を使用します。self.population_name_age_population

于 2012-09-10T17:27:13.803 に答える