0

いくつかの情報: 私は2つの異なるクラス、「家」と「財務」を持っています。これらは両方とも「構造体」と呼ばれる同じNSMutableArray内にあります。Houseには、「所有権」と呼ばれる(int)変数があります。私はforループ(100ループ)を使用して、エリア全体に家や宝物を作成します。次に、「構造」をループして「所有権」整数を割り当てる別のループがあります。そのループでは、for(Houses * h in structure)を使用しますが、何らかの理由で、ループは構造内の「財務省」もループします。ただし、財務省には所有権変数がないため、「setOwnership:」が存在しないというエラーが表示されます。私もCocos2Dを使用していますが、それは問題ではありません。

コードは次のようになります。

// Variable
NSMutableArray *structures;


-(void) InitializeStructures
{
    structures = [[NSMutableArray alloc]init];

    for(int i = 0; i < initialColonies; i++)
    {
        House *h = [[House alloc]initWithFile:@"House.png"];
        h.position = [self ReturnPositionOnMap]; // Returns a point in playing area

        Treasury *t = [[Treasury alloc]initWithFile:@"Treasury.png"];
        t.position = [self ReturnFarPointNearOrigin:h.position];

        [structures addObject:h];
        [self addChild:h z:1];

        [structures addObject:t];
        [self addChild:t z:1];
    }
}

-(void) AssignOwnerships
{
    for(House *h in structures)
    {
        // Simplified to only show where the error occurs.
        h.ownership = [self ReturnID]; // Error occurs here.
        // The error ONLY occurs when it is iterating through a Treasury.
    }
}

構造:

#import "CCSprite.h"
#import "ImportedGoods.h"

@interface Structure : CCSprite
{
    int _integrity;
    int _ID;
    bool _occupied;
    int _occupiedTimeLeft;
    int _occupiedTimeMax;
    Faction _faction;
}
@property(nonatomic, assign) int integrity;
@property(nonatomic, assign) int ID;
@property(nonatomic, assign) bool occupied;
@property(nonatomic, assign) int occupiedTimeLeft;
@property(nonatomic, assign) int occupiedTimeMax;
@property(nonatomic, assign) Faction faction;
@end

#import "Structure.h"

@implementation Structure

@synthesize integrity = _integrity;
@synthesize ID = _ID;
@synthesize occupied = _occupied;
@synthesize occupiedTimeLeft = _occupiedTimeLeft;
@synthesize occupiedTimeMax = _occupiedTimeMax;
@synthesize faction = _faction;

@end

家:

#import "Structure.h"
#import "ImportedGoods.h"

@interface House : Structure
{
    int _ownership;
}
@property(nonatomic, assign) int ownership;
@end

#import "House.h"

@implementation House

@synthesize ownership = _ownership;

@end

財務省:

#import "Structure.h"

@interface Treasury : Structure
{
    int _storedWood;
    int _storedWater;
    int _storedStone;
    int _storedFood;
}
@property(nonatomic, assign) int storedWood;
@property(nonatomic, assign) int storedWater;
@property(nonatomic, assign) int storedStone;
@property(nonatomic, assign) int storedFood;
@end

#import "Treasury.h"

@implementation Treasury

@synthesize storedFood = _storedFood;
@synthesize storedWood = _storedWood;
@synthesize storedStone = _storedStone;
@synthesize storedWater = _storedWater;

@end

これはエラーです:

2011-11-07 18:45:29.016 Virtual World[788:10a03] -[Treasury setOwnership:]: unrecognized selector sent to instance 0x7498cc0
2011-11-07 18:45:29.022 Virtual World[788:10a03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Treasury setOwnership:]: unrecognized selector sent to instance 0x7498cc0'
*** First throw call stack:
(0x17fb052 0x198cd0a 0x17fcced 0x1761f00 0x1761ce2 0xcbe33 0xc2193 0xc1e5f 0xc1a34 0x3fc81 0xc185e 0x176151d 0x1761437 0x39b25 0x36ee2 0x17fcec9 0x91f91 0x92adf 0x94991 0x869a30 0x869c56 0x850384 0x843aa9 0x27affa9 0x17cf1c5 0x1734022 0x173290a 0x1731db4 0x1731ccb 0x27ae879 0x27ae93e 0x841a9b 0xc07ef 0x20c5 0x1)
terminate called throwing an exception(gdb) 

「財務」と呼ばれる別のNSMutableArrayを作成できることは知っていますが、可能であればすべてを1つの配列に保持したいと思います。「House*h instructures」を反復するように指定したのに、なぜそれが財務省を反復するのか知りたいです。以下の追加情報コメントが必要な場合は、追加します。ありがとう!

4

2 に答える 2

4

構文であるObjective-Cで高速列挙を使用する場合:

for (MyClass *item in someArray) {
    ...
}

あなたがしているのは、配列内のすべてのアイテムをループし各アイテムをMyClassオブジェクトへのポインタとして扱い、それが実際のアイテムであるかどうかに関係なくです。ループで宣言したタイプの配列内のアイテムを反復処理するだけで、思ったとおりに動作しません。

おそらくあなたがすべきHouseことは、あなたとTreasuryアイテムを別々の配列に保存することです。それらを同じ構造にする特別な理由がありますか?

それらを本当に一緒に保存したい場合は、次を使用できます。

for (id item in structures) {
    if ([item isKindOfClass:[House class]]) {
        House *house = (House *) item;
        [house setOwnership:...];
    } else {
        continue;
    }
}
于 2011-11-08T02:26:02.170 に答える
3

あなたの仮定

for(House *h in structures)
    {
        // Simplified to only show where the error occurs.
        h.ownership = [self ReturnID]; // Error occurs here.
        // The error ONLY occurs when it is iterating through a Treasury.
    }

配列からHouseオブジェクトのみを選択するのは間違っています。配列からすべてのオブジェクトを取得します。 House *h in structures配列がHouseオブジェクトでいっぱいであることをコンパイラーに通知します。

次のように変更する必要があります

for (id h in structures){
// Simplified to only show where the error occurs.
     if ([h respondsToSelector:@selector(setOwnership:)]){
          h.ownership = [self ReturnID];
     }
}
于 2011-11-08T02:26:30.917 に答える