0
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
3
  • How do you define -constructUnknown? Commented Jun 7, 2010 at 15:03
  • i have tested the constructor on a single pointer:it works Commented Jun 7, 2010 at 15:07
  • Show us Favorite.h? I'm curious whether you've properly subclassed NSObject. Commented Jun 7, 2010 at 15:13

2 Answers 2

2

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{ ... }
Sign up to request clarification or add additional context in comments.

7 Comments

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: If the object is autoreleased, and you want to keep it around, then you do need to retain it.
Ah, true. My bad. Does indeed seem this would be the problem.
yeah.that works.thank you.retaining an object means it stays in memory and not be released out of his scope?
@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.
|
0

Just in case someone reads this and still doesn't reach a solution, my problem was a little different I declared the object as:

@class LoginViewController;

@interface LoginViewDelegate : NSObject <UIApplicationDelegate> {

}

....

@property (nonatomic, retain) AppConfiguration *CurrentAppConfig;

....

@End

And when I was calling it:

[[self.CurrentAppConfig alloc] init];

I was getting the same error, all I had to do was use the synthesize keyword:

@implementation LoginViewDelegate

....

@synthesize CurrentAppConfig;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.