0

일부 정보 : "House"와 "Treasury"의 두 가지 클래스가 있습니다. 둘 다 "구조체"라고하는 동일한 NSMutableArray 안에 있습니다. 하우스에는 "소유권"이라는 (int) 변수가 있습니다. for 루프 (100 루프)를 사용하여 한 지역에 주택과 국채를 생성합니다. 그런 다음 "구조체"를 반복하고 "소유권"정수를 할당하는 다른 루프가 있습니다. 그 루프에서는 (Houses * h 구조체)를 사용하지만 어떤 이유로 루프가 구조체의 "treasury"를 반복합니다. 재무부에는 소유권 변수가 없으므로 "setOwnership :"이 존재하지 않는다는 오류 메시지가 나타납니다. 나는 Cocos2D도 사용하고 있지만 중요하지 않습니다.Obj-C의 오류 "인스턴스로 보낸 인식 할 수없는 선택기"

// 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 

재무부 :

여기

코드처럼 보이는 것입니다 난 그냥 다른있는 NSMutableArray는 "국채"라고하지만 가능하면 하나 개의 배열에 모든 것을 유지하고 싶습니다 만들 수 있다는 것을 알고

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) 

:

이 75,252,073,210는 오류입니다. "구조에서 하우스 * h"반복을 지정 했음에도 불구하고 여전히 국채로 반복되는 이유를 알고 싶습니다. 아래에 추가 정보 의견이 필요하면 추가하겠습니다. 감사합니다.

+1

-1로 변경해야합니다. –

+0

오, 고마워, 바로 지금 고칠거야. – Telinir

+0

은 오류를 찾기 위해 더 많은 코드가 필요합니다. 아마도 재무부 수업 코드도 게시해야합니다. – Raptor

답변

4

당신은 구문입니다 오브젝티브 C에 빠른 열거을 사용하는 경우 :

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

당신이 포인터로 각 항목을 치료, 통해 배열의 모든 항목을 루핑하고있는 MyClass 개체로, 이 항목이 실제로 있는지 여부는입니다. 그것은 이 아니라이 무엇을한다고 생각합니까? 반복문에서 선언 한 유형의 배열에있는 항목에 대해서만 반복됩니다.

일 것입니다. HouseTreasury 항목은 별도의 배열에 저장하십시오. 같은 구조에 두는 특별한 이유가 있습니까?

당신이 정말로 그들을 함께 저장하려면

, 당신은 사용할 수 있습니다

for (id item in structures) { 
    if ([item isKindOfClass:[House class]]) { 
     House *house = (House *) item; 
     [house setOwnership:...]; 
    } else { 
     continue; 
    } 
} 
+0

감사합니다. 정말 빠른 열거 형을 이해하고 내 코드에서 무엇이 잘못되었는지 도움이되었습니다. – Telinir

+0

문제가 없습니다 @ 텔리 니르, 도와 줘서 다행! –

+0

또한 이유는 "구조체"를 전체적으로 사용하고 변수를 수정하는 기능이 많이 있었기 때문에 별도의 빠른 열거 형을 사용하여 약 6 개의 배열을 실행하는 것이 엉망이었습니다. 이것은 정확히 내가 원했던 것입니다 : D – Telinir

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 *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]; 
    } 
} 
+0

정말 고마워요! 그것은 효과가 있었다. -edit- Craig는 상황에 더욱 적합한 솔루션을 게시했지만이 방법을 계속 염두에 둘 것입니다. 고맙습니다! – Telinir

+0

문제 없습니다. 그리고 그게 제가 체크 마크보다 +1을 좋아하는 이유입니다! –

+0

죄송합니다. 불행히도 +1 할 수 없습니다. 내가 충분히 명성을 얻은 후에는 주변에 도착할 것입니다. – Telinir

관련 문제