2013-04-04 72 views
-1

我想要一個看起來像這個player.type.property的結果,一個例子是UILabel,self.label.text。 .text是兩個類的屬性。兩類的屬性

我有一個建議是,做這樣的事情:

player.type = [[MyCustomObject alloc] init]; 
player.type.property = @"value"; 

雖然我不太清楚究竟如何去正確地這樣做,每次我都試過的方法是行不通的。

這是我曾嘗試:

Marketplace.h 
#import "Item.h" 
@interface Marketplace : NSObject 
@property (nonatomic, assign) Item *market; 

Item.h 
@interface Item : NSObject 
@property (nonatomic, assign) int price; 

Starter.m 
#import "Marketplace.h" 
#import "Item.h" 
@implementation MainGameDisplay 
{ 
    Marketplace *market; 
    Item *itemName; 
} 

-(void) executedMethod { 
    market.itemName = [[market alloc] init]; 
    //2 errors: "Property 'itemName not found on object of type 'MarketPlace'" and "No visible @interface for 'MarketPlace' declares the selector alloc" 
    market.itemName.price = 5; //"Property 'itemName' not found on object of type 'Marketplace*'" 
} 
+0

@VitalyS。我不知道如何定義屬性類型並初始化它。而通過MyCustomObject,它指的是哪個對象,玩家還是類型? – PappaSmalls 2013-04-04 10:48:04

+0

你究竟想要什麼?你嘗試過什麼方法?請詳細說明你的問題。 – HAS 2013-04-04 10:59:39

+0

@VitalyS。我編輯了我的問題來展示我所做的。 – PappaSmalls 2013-04-04 11:11:14

回答

1

每個指向類對象的指針必須是alloc init,所以你需要在其類中覆蓋 - (id)init。

Item.h 
@interface Item : NSObject 
@property (nonatomic) NSInteger price; 


Marketplace.h 
#import "Item.h" 
@interface Marketplace : NSObject 
@property (nonatomic, strong) Item *item;//Item is a class, must use strong or retain 
Marketplace.m 
-(id)init{ 
if (self = [super init]) { 
    self.item = [[Item alloc] init];//Item must alloc together when MarcketPlace init 
} 
return self; 
} 

*然後你只需初始化賣場

@implementation MainGameDisplay 
{ 
    Marketplace *market; 
    Item *itemName; 
} 

-(void) executedMethod { 
    market = [Marketplace alloc] init]; 
//Now you can access 
    market.item.price = 5; 
} 
0

1。使一個名爲PlayerType的接口把一些屬性放在那裏併合成它們。 2.現在創建一個名爲Player的Interface並在其中導入PlayerType接口。 3.使PlayerType接口的屬性像@property(nonatomic,strong)PlayerType *類型。

  1. 現在使玩家變量,它將允許您訪問屬性的屬性。
+0

我已經完成了它,但它仍然無法工作。如果你在問題中檢查我的編輯,我相信你會看到問題。 – PappaSmalls 2013-04-04 11:26:23