Obj-C 中的错误“无法识别的选择器发送到实例”
一些信息: 我有两个不同的班级,“众议院”和“国库”。 它们都位于同一个 NSMutableArray 中,称为“结构”。 House 有一个名为“所有权”的 (int) 变量。 我使用 for 循环(100 个循环)在一个区域内创建房屋和金库。 然后我有另一个循环,它循环遍历“结构”并为其分配一个“所有权”整数。 在该循环中,我使用 for(Houses *h in Structures),但由于某种原因,该循环也循环遍历结构中的“treasury”。 不过,财政部没有所有权变量,因此我收到错误消息,告诉我“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,但我想将所有内容保留在一个中如果可能的话数组。 我想知道为什么它仍然遍历国库,即使我指定遍历“House *h in Structures”。 如果您需要任何其他信息,请在下面评论,我会添加。 谢谢!
Some information:
I have two different classes, "House" and "Treasury".
They are both inside the same NSMutableArray called "structures".
House has a (int) variable called "ownership".
I use a for loop (100 loops) to create houses and treasuries across an area.
Then I have another loop that loops through "structures" and assigns an "ownership" integer to it.
In that loop I use for(Houses *h in structures) but for some reason the loop also loops through the "treasury"s in structures as well.
Treasury has no ownership variable though and so I get that error telling me that "setOwnership:" does not exist.
I am also using Cocos2D but that shouldn't matter.
Here is what the code looks like:
// 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.
}
}
Structure:
#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
House:
#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
Treasury:
#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
This is the error:
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)
I know that I can just make a different NSMutableArray called "treasuries" but I'd like to keep everything in one array if possible.
I want to know why it still iterates through treasuries even though I specified to iterate through "House *h in structures".
If you need any additional information comment below and I will add.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您认为
只会从数组中选取 House 对象的假设是错误的。您将从数组中获取每个对象。
结构中的 House *h
告诉编译器数组充满了 House 对象。你应该将其更改为
Your assumption that
will only pick the House objects from the array is wrong. You will get every object from the array.
House *h in structures
tells the compiler that array is full of House objects.You should change it to
当您在 Objective-C 中使用快速枚举时,语法如下:
您所做的是循环遍历数组中的每个项,将每个项视为指向一个
MyClass
对象,无论该项目实际是什么。它不做你认为它做的事情,它只是迭代数组中你在循环中声明的类型的项目。您应该可能做的是将您的
House
和Treasury
项目存储在单独的数组中。您是否有将它们放在同一结构中的具体原因?如果你真的想将它们存储在一起,你可以使用:
When you use fast enumeration in Objective-C, which is the syntax:
What you're doing is looping over every item in the array, treating each item as a pointer to a
MyClass
object, whether that's what the item actually is or not. It does not do what you think it does, which is only iterate over the items in the array that are of the type you declare in your loop.What you should probably do is store your
House
andTreasury
items in separate arrays. Do you have a specific reason for putting them in the same structure?If you really want to store them together, you can use: