开发者

Setting a reference type in cocoa

I'm hoping this is a simple question, as I'm very new to cocoa. I created an object which has a UIImageView as a property. I set the property within another procedure, e.g.

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)];

No error is t开发者_开发百科hrown, but imgView is nil (0x0). What behavior would cause this? I am creating the bleep object, setting value types just fine, but this object doesn't seem to want to hold anything.

My classes:

@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


You are not showing enough code here, but my guess is that you did not do the property properly. Did you declare it, e.g.:

@property (nonatomic, retain) UIImageView *imgView;

?

Then did you synthesize it or make set/get yourself? If you did the latter, you may have made the mistake of referencing it through self in your setter.

If it's not one of these things, show more code.

...

Here's what your code should look like:

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

@implementation Bleep
@synthesize imgView;

@end

The pointer in the class is being exposed through the property. This seems kind of repetitive, but in Xcode 4 much of the misery of typing these things out multiple times is gone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜