-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*'"
}
4

2 回答 2

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;
}

*然后您只需启动 Marketplace

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

-(void) executedMethod {
    market = [Marketplace alloc] init];
//Now you can access
    market.item.price = 5;
}
于 2013-04-04T12:11:03.387 回答
0

1. 制作一个名为 PlayerType 的接口 将一些属性放在那里并合成它们。2. 现在创建一个名为 Player 的接口并在那里导入 PlayerType 接口。3. 创建一个PlayerType 接口的属性,如@property(nonatomic, strong) PlayerType *type。

  1. 现在将 Player 设为变量,它将允许您访问属性的属性。
于 2013-04-04T11:15:06.503 回答