2011-10-07 4 views
-1
@interface Category : NSObject 

@property(nonatomic) NSInteger ID; 
@property(nonatomic, retain) NSString *Name; 
@property(nonatomic, retain) NSString *Description; 

@end 

제품 클래스객체 속성

@interface Product : NSObject 

@property(nonatomic) NSInteger ID; 
@property(nonatomic, retain) NSString *Name; 
@property(nonatomic, retain) NSString *Description; 

@property (비 원자, 유지) 종류 * 카테고리;

@end 

어떻게 유형 카테고리의 속성을 만들려면 어떻게해야합니까. 전혀 가능합니까? 수업 ProductCategory는 다음 추가 import이 필요하지만 확실하지 않습니다 같은 파일에 정의되어있는 경우

#import "Category.h" // file, where class Category is defined 

@interface Product : NSObject 

@property(nonatomic) NSInteger ID; 
@property(nonatomic, retain) NSString *Name; 
@property(nonatomic, retain) NSString *Description; 
@property(nonatomic, retain) Category *category; 

@end 

2) : 당신은 클래스 Product 다음 별도의 파일에 정의되어있는 경우

+1

코드를 실행 해보았 으면 좋겠다고 생각합니다. 어쩌면 당신은 상단에'#import "Category.h"를 추가 할 필요가 있습니다. – darvids0n

답변

1

1) 해당 클래스 Categiry은 먼저 Product으로 정의됩니다. 두 클래스는 다음이 @class 태그를 사용해야 서로 형식의 개체를 가지고있는 경우

3) :

@class Product; 

@interface Category : NSObject 
@property(nonatomic, retain) Product *product; 
@end 

@interface Product : NSObject 
@property(nonatomic, retain) Category *category; 
@end 
+0

위대한! 그것은 작동합니다. – gangt

3

을 당신은 당신의 전방 선언을 추가 할 필요를 제외하고는,이처럼 그것을 할 (당신의 @interface 라인 전) Product.h에 Category 클래스 :

@Class Category; 

그리고

#import "Category.h" 
Product.m

에서 헤더를 가져옵니다
1

예, 가능하며 게시 한 구문이 정확합니다. 당신은 문제가 있다면, 아마도 당신은 Product.h의 상단에

#import "Category.h" 

을 할 필요가 있기 때문에 될 수있다, 당신은 @synthesize 카테고리에 있는지 확인해야합니다. 그러나 다른 속성을 사용하고 있으므로 이미 알고 있다고 가정합니다.

관련 문제