0

我有一堂课

@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,我的值为空。请帮忙。

4

2 回答 2

0

这与前向声明无关。当您转发声明一个类时,您应该在使用任何类属性/方法之前#import该文件。.h

问题是属性appRecordiniconDownloader尚未创建,因此 is 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];
    }

    //...
}

或者,您也可以覆盖initinIconDownloader类并在appRecord其中创建属性,这样nil您就不会在分配值时使用它。

希望有帮助!

于 2013-10-04T06:29:10.733 回答
0

您没有初始化appRecord对象。这就是为什么你得到空值。只需在您的init方法中初始化appRecord ,例如:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        appRecord = [[AppRecord alloc]init];
    }
    return self;
}

类似地,您必须在 init 定义中初始化urlSingle变量:

-(id)init
    {
        self = [super init];
        if (self) {

            urlSingle = URL_STRING_HERE;
        }
        return self;
    }

现在你试试

于 2013-10-04T06:36:24.970 回答