一些信息: 我有两个不同的类,“House”和“Treasury”。它们都在同一个 NSMutableArray 中,称为“结构”。House 有一个 (int) 变量,称为“所有权”。我使用一个 for 循环(100 个循环)在一个区域内创建房屋和金库。然后我有另一个循环遍历“结构”并为其分配一个“所有权”整数。在那个循环中,我使用 for(Houses *h in structures) 但由于某种原因,循环也循环通过结构中的“金库”。不过,财政部没有所有权变量,所以我收到错误消息,告诉我“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)
我知道我可以制作一个名为“treasuries”的不同 NSMutableArray,但如果可能的话,我想将所有内容都保存在一个数组中。我想知道为什么即使我指定遍历“House *h in structure”,它仍然会遍历国库。如果您需要以下任何其他信息评论,我会添加。谢谢!