0

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 thrown, 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
George Johnston
  • 31,652
  • 27
  • 127
  • 172
  • It would help to post the relevant parts of your `@interface` and your `setImgView:` or `@synthesize`. – Anomie Apr 16 '11 at 03:57

1 Answers1

0

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.

Rob
  • 11,446
  • 7
  • 39
  • 57
  • 1
    Note that the default setter semantics for declared properties is `assign`, so if you do not specify any setter semantics attribute (or specify `assign`) then the class is responsible for managing the declared property’s memory. –  Apr 16 '11 at 04:08
  • Actually, there’s no more need to declare an instance variable. Objective-C 2.0 automatically creates a backing instance variable if none is declared. –  Apr 16 '11 at 04:09
  • @Bavarious: curious then that the Apple code is still full of them. I have downloaded countless examples in the last year and a half. – Rob Apr 16 '11 at 04:31
  • There's been discussion of this issue on SO http://stackoverflow.com/questions/4903651/is-there-any-reason-to-declare-ivars-if-youre-using-properties-exclusively-in-ob/4904470#4904470 – jscs Apr 16 '11 at 05:08