Cocoa touch 在cocoa中设置引用类型

Cocoa touch 在cocoa中设置引用类型,cocoa-touch,Cocoa Touch,我希望这是一个简单的问题,因为我对可可非常陌生。我创建了一个以UIImageView作为属性的对象。我在另一个过程中设置属性,例如 Bleep* bleep = [[Bleep alloc]init]; bleep.heading = heading; bleep.distance = distance; bleep.instanceId = instanceId; ... bleep.imgView = [[UIImageView alloc] initWithFrame:CGRectMake

我希望这是一个简单的问题,因为我对可可非常陌生。我创建了一个以UIImageView作为属性的对象。我在另一个过程中设置属性,例如

Bleep* bleep = [[Bleep alloc]init];
bleep.heading = heading;
bleep.distance = distance;
bleep.instanceId = instanceId;
...
bleep.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, bleepImg.size.width, bleepImg.size.height)];
未引发任何错误,但imgView为nil 0x0。什么行为会导致这种情况?我正在创建bleep对象,将值类型设置得很好,但是这个对象似乎不想保存任何内容

我的班级:

@interface Bleep : NSObject {

}
@property float x;
@property float y;
@property float distance;
@property int instanceId;
@property UIImageView* imgView;
@property float heading;

@end

@implementation Bleep
@synthesize x;
@synthesize y;
@synthesize distance;
@synthesize instanceId;
@synthesize heading;
@synthesize imgView;

@end

您没有在这里显示足够的代码,但我猜您没有正确地执行该属性。你申报了吗,例如:

@property (nonatomic, retain) UIImageView *imgView;
?

那你是合成它还是自己做的?如果你做了后者,你可能会犯错误,在你的设定者中通过self引用它

如果不是这些,请显示更多代码

以下是您的代码的外观:

@interface Bleep : NSObject {
    UIImageView *imgview;
}
@property(nonatomic, retain) UIImageView* imgView;

@implementation Bleep
@synthesize imgView;

@end

类中的指针通过属性公开。这似乎有点重复,但在Xcode 4中,多次键入这些内容的痛苦已经消失了。

发布@interface和setImgView:或@synthesis的相关部分会有所帮助。请注意,声明属性的默认setter语义是assign,因此,如果您没有指定任何setter语义属性或指定assign,那么该类将负责管理声明的属性的内存。Objective-C2.0如果未声明任何实例变量,则会自动创建一个备份实例变量。@bavariable:奇怪的是,Apple代码中仍然充满了这些变量。在过去一年半的时间里,我下载了无数的例子。关于这个问题的讨论一直在进行