views:

107

answers:

2
favorites = [[NSMutableArray alloc] init];

for (int i=0; i<9; i++) {

  [favorites addObject:[[[Favorite alloc] constructUnknown] autorelease]];



 }

i'm getting:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[Favorite size]: unrecognized selector sent to instance 0x380d9c0'

why?

Favorite is my custom class, favorites an array containing 9 instances of my custom class

edit:

Favorite.h:

-(Favorite*)constructUnknown;

Favorite.m:

 - (Favorite*)constructUnknown{


self=[super init];

if (self) {
    image=[UIImage imageNamed:@"unknown.png"];
}

return self;

}

COMPLETE FAVORITES.h

@interface Favorite : NSObject {

NSString *identity;
bool ready;

UIImage *image;
NSURL *telephone;


}

@property (nonatomic,retain) UIImage *image;
@property (nonatomic,retain) NSURL *telephone;
@property (nonatomic,retain) NSString *identity;

//passare unknown al nome per costrutire un oggetto unknown.
-(Favorite*)constructWithID:(NSString*)name withPhoto:(UIImage*)photo andNumber:(NSString*)number;

-(Favorite*)constructUnknown;
-(NSURL*) convertToUrl:(NSString*)phone;
- (UIImage*) getImage;

@end
A: 

anyone willing to help?i am crushing my mind here and cannot go on.

palominoz
+2  A: 

The exception is likely because your image is not retained. Try

image = [[UIImage imageNamed:@"unknown.png"] retain];

BTW, initializers should be named as -initXXX and return an id by convention. e.g.

 -(id)initWithUnknown{ ... }
KennyTM
Doesn't need to retain the `UIImage`, as the class method `imageNamed:` returns an autoreleased object, as per the ownership guidelines.Good point on the initializer naming though.
jshier
@jshier: If the object is autoreleased, and you want to keep it around, then you *do* need to retain it.
mipadi
Ah, true. My bad. Does indeed seem this would be the problem.
jshier
yeah.that works.thank you.retaining an object means it stays in memory and not be released out of his scope?
palominoz
@palominoz: It's better to think in terms of ownership. `-retain` means "from now on this object is the owner of the image". Remember to `-release` it in the Favorite's `-dealloc` as it is no longer the owner of the image.
KennyTM
@KennyTM so it didnt work because i was calling the property "image", but the instance of the object was not the owner of the image, right?Is it correct to retain objects in properties every time a custom class is built, or is there some way useful not to retain it?
palominoz
@palominoz: The instance of the object *should be* the owner, so you *should* `-retain` it.
KennyTM
@KennyTM got it, ty kenny
palominoz