我有一个名为电影的自定义类。它很简单,具有三个属性:
。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 Tittle, theBlurb = Empty Blurb
图像总是说它是空的?我不明白这一点。如您所见,我是从添加到项目中的文件中设置它的????