我有一堂课
@interface AppRecord : NSObject
@property (nonatomic, retain) NSString * urlSingle;
@property (nonatomic, retain) NSArray * image_url;
@end
它包含在另一个类中
@class AppRecord;
@interface IconDownloader : NSObject
@property (nonatomic, strong) AppRecord *appRecord;
@end
这是我的根视图控制器
#import "IconDownloader.h"
@implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageDownloadsInProgress = [NSMutableDictionary dictionary];
}
- (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath
{
IconDownloader *iconDownloader = [self.imageDownloadsInProgress objectForKey:indexPath];
if (iconDownloader == nil)
{
iconDownloader = [[IconDownloader alloc] init];
int imgArrCount=[appRecord.image_url count];
NSLog(@"Image array is********************** %@",appRecord.image_url);
for(int i=0;i<imgArrCount;i++)
{
iconDownloader.appRecord.urlSingle=[appRecord.image_url objectAtIndex:i];
NSLog(@"iconDownloader.appRecord.urlSingle---------------------%@",iconDownloader.appRecord.urlSingle);
}
}
}
@end
我可以在这里分配iconDownloader.appRecord.urlSingle吗,我的值为空。请帮助。
这与前向声明无关。转发声明一个类时,应在使用任何类的属性/方法之前#import
将.h
文件保存。
问题是尚未创建属性appRecord
in iconDownloader
,因此是nil
。在您的代码中,您应该这样做。
- (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath
//...
for(int i=0;i<imgArrCount;i++)
{
// First assign to the property so that it is not nil
iconDownloader.appRecord = appRecord;
// If required then make this assignment
iconDownloader.appRecord.urlSingle=[appRecord.image_url objectAtIndex:i];
}
//...
}
或者,您也可以覆盖init
inIconDownloader
类并在其中创建appRecord
属性,这样nil
在分配值时就不会如此。
希望对您有所帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句