【发布时间】:2013-07-13 16:52:10
【问题描述】:
我有一个名为电影的自定义类。很简单,只有三个属性:
.h
@interface WTAMovie : NSObject
@property (strong, nonatomic) NSImage *theImage;
@property (strong, nonatomic) NSString *theTittle;
@property (strong, nonatomic) NSString *theBlurb;
@end
.m 有一个初始化器,它为属性设置一些默认值,如果记录自定义对象,则返回属性值的描述方法。像这样。
-(NSString *)description
{
NSString *desc = [NSString stringWithFormat:@"<Movie> - theImage = %@, theTittle = %@, theBlurb = %@", _theImage, _theTittle, _theBlurb];
return desc;
}
-(id)init
{
self = [super init];
if (self) {
_theTittle = @"New Tittle";
_theBlurb = @"Empty Blurb";
NSString* imageName = [[NSBundle mainBundle] pathForResource:@"reel" ofType:@"png"];
_theImage = [[NSImage alloc] initWithContentsOfFile:imageName];
NSLog(@"from the person calss init: %@", _theImage);
}
return self;
}
在我的 Document.m 文件初始化程序中,我正在创建一个新的 Movie 对象,将其放入定义为保存电影对象的数组中,然后记录该对象。像这样:
- (id)init
{
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
_movies = [NSMutableArray new];
WTAMovie *firstMovie = [WTAMovie new];
[_movies addObject:firstMovie];
for (id object in _movies)
{
NSLog(@"%@", object);
}
}
return self;
}
控制台的输出是:
2013-07-15 13:47:57.174 HomeworkFour[5044:303] - theImage = (null), theTittle = New Title, theBlurb = Empty Blurb
图像总是说它是空的?我不明白。如您所见,我是从添加到项目中的文件中设置它的????
【问题讨论】:
-
快速浏览一下,汤米,我认为您的房产被抬高了。这是对属性属性的快速参考。 stackoverflow.com/questions/9859719/…
-
你试过用 UIImage 代替 NSImage 吗?
-
它将始终显示为空。您唯一可以分配的是 name 属性,如果未分配,它将显示 nil。但是图像被加载在那里。它只是不会在没有被分配的情况下显示名称。加载图像后,它不再了解其资源。
标签: properties nsimage initwithcontentsoffile