Iphone 目标C使用自定义类指针填充数组
最喜欢的。m:Iphone 目标C使用自定义类指针填充数组,iphone,class,arrays,Iphone,Class,Arrays,最喜欢的。m: -(Favorite*)constructUnknown; 完成收藏夹 - (Favorite*)constructUnknown{ self=[super init]; if (self) { image=[UIImage imageNamed:@"unknown.png"]; } return self; } 异常可能是因为未保留图像。试一试 @interface Favorite : NSObject { NSString *identity; b
-(Favorite*)constructUnknown;
完成收藏夹
- (Favorite*)constructUnknown{
self=[super init];
if (self) {
image=[UIImage imageNamed:@"unknown.png"];
}
return self;
}
异常可能是因为未保留
图像。试一试
@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
顺便说一句,初始值设定项应命名为-initXXX
,并按惯例返回id
。e、 g
image = [[UIImage imageNamed:@"unknown.png"] retain];
万一有人读到这篇文章,仍然没有找到解决方案,我的问题就有点不同了,我将对象声明为:
-(id)initWithUnknown{ ... }
我遇到了同样的错误,我所要做的就是使用synthesis关键字:
[[self.CurrentAppConfig alloc] init];
如何定义-constructUnknown
?我在一个指针上测试了构造函数:它可以显示我们最喜欢的.h?我很好奇您是否正确地对NSObject进行了子类化。不需要保留UIImage
,因为类方法imageNamed:
根据所有权准则返回一个自动删除的对象。不过,在初始化器命名方面有一点很好。@jshier:如果对象是自动删除的,并且您想保留它,那么您确实需要保留它。啊,是的。我的错。看起来这确实是个问题。是的。这很有效。谢谢。保留一个对象意味着它保留在内存中,不会被释放出他的范围吗?@palominoz:最好从所有权的角度考虑-retain
表示“从现在起,此对象是图像的所有者”。记住在收藏夹的-dealoc
中释放它,因为它不再是图像的所有者。
@class LoginViewController;
@interface LoginViewDelegate : NSObject <UIApplicationDelegate> {
}
....
@property (nonatomic, retain) AppConfiguration *CurrentAppConfig;
....
@End
[[self.CurrentAppConfig alloc] init];
@implementation LoginViewDelegate
....
@synthesize CurrentAppConfig;