いくつかの情報: 私は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」を反復するように指定したのに、なぜそれが財務省を反復するのか知りたいです。以下の追加情報コメントが必要な場合は、追加します。ありがとう!