2013-04-04 31 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