0

我试图回忆我保存在目录中的图像以在 UICollectionView 中显示它。这是执行此操作的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSMutableArray *allImagesArray = [[NSMutableArray alloc] init];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *location=@"Hats";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    for(NSString *str in directoryContent){
        NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
        NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
        if(data)
        {
            UIImage *image = [UIImage imageWithData:data];
            [allImagesArray addObject:image];
        }}}

但是,我收到最后一行显示的警告,告诉我 allImagesArray 的本地声明隐藏了实例变量。我不知道为什么会这样。如果您想查看我的其他代码,请随时询问。任何帮助是极大的赞赏。
这是我的 .h 文件

@interface HatsViewController : UICollectionViewController <UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSMutableArray *allImagesArray;

 }
4

1 回答 1

0

你可能在你的接口文件中声明它——你的 UIViewController 已经“知道”了 allImagesArray。

Xcode 在警告中所说的内容类似于:“嘿,你已经在声明它了,而你又在viewDidLoad做它!小心”

要修复它,只需以这种方式启动您的阵列:

代替NSMutableArray *allImagesArray = [[NSMutableArray alloc] init];

将其替换为allImagesArray = [[NSMutableArray alloc] init];

BINGO 更新:确实,正如你刚才所说,你已经在接口文件中声明了它,所以你不需要再次声明它viewDidLoad

于 2013-08-18T08:48:02.887 回答