favorites = [[NSMutableArray alloc] init];
for (int i=0; i<9; i++) {
[favorites addObject:[[[Favorite alloc] constructUnknown] autorelease]];
}
我得到了:
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** - [收藏大小]:无法识别的选择器发送到实例0x380d9c0'
为什么?
收藏夹是我的自定义类,收藏夹包含9个自定义类的实例
编辑:
Favorite.h:
-(Favorite*)constructUnknown;
Favorite.m:
- (Favorite*)constructUnknown{
self=[super init];
if (self) {
image=[UIImage imageNamed:@"unknown.png"];
}
return self;
}
完成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
答案 0 :(得分:2)
异常可能是因为您的image
未被保留。尝试
image = [[UIImage imageNamed:@"unknown.png"] retain];
BTW,初始值设定项应命名为-initXXX
并按惯例返回id
。 e.g。
-(id)initWithUnknown{ ... }
答案 1 :(得分:0)
万一有人读到这个并且仍然没有找到解决方案,我的问题有点不同我将对象声明为:
@class LoginViewController;
@interface LoginViewDelegate : NSObject <UIApplicationDelegate> {
}
....
@property (nonatomic, retain) AppConfiguration *CurrentAppConfig;
....
@End
当我打电话时:
[[self.CurrentAppConfig alloc] init];
我得到了同样的错误,我所要做的就是使用synthesize关键字:
@implementation LoginViewDelegate
....
@synthesize CurrentAppConfig;