objective C filling array with custom class pointers
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*)constructU开发者_如何转开发nknown{
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
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{ ... }
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;
精彩评论