-1

我正在使用多个 iCarousels 并希望从目录中导入图片。我在顶部有 iCarousel 1,在底部有 iCarousel 2。我在用户拍照的iOS设备目录中有大约6个文件夹。我想将 iCarousel 1 分配给目录“apple”,将 iCarousel 2 分配给目录“green”。

以下是我到目前为止的代码。但是,当然,由于我正在为 2 个 imageArrays 设置路径,因此会出现“重新定义 ...”的错误。我应该如何使这段代码更简单?

此外,我在线路上也有警告说'NSMutableArray *_strong' from 'NSArray *_strong'imageArray2 = directoryContent;我真的想让这一切正常工作。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //configure carousel
    imageArray1 = (NSMutableArray *)[[NSFileManagerdefaultManager] directoryContentsAtPath: fPath];
    NSString *location=@"apple";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
    NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    imageArray1 = directoryContent;

    imageArray2 = [[NSMutableArray alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *location=@"green";
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];

    NSArray * directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
    imageArray2 = directoryContent;

    carousel1.type = iCarouselTypeLinear;
    carousel2.type = iCarouselTypeLinear;
}
4

1 回答 1

0

您两次声明了一些变量。

你可以这样做:

-(void)viewDidLoad
{
   [super viewDidLoad];

   //configure carousel
   imageArray1 = (NSMutableArray *)[[NSFileManager defaultManager] directoryContentsAtPath: fPath];
   NSString *location=@"apple";
   NSString *fPath = [documentsDirectory stringByAppendingPathComponent:location];
   NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
   imageArray1 = [directoryContent mutableCopy];

   imageArray2 = [[NSMutableArray alloc] init];
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   location=@"green";
   fPath = [documentsDirectory stringByAppendingPathComponent:location];

   directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
   imageArray2 = [directoryContent mutableCopy];

   carousel1.type = iCarouselTypeLinear;
   carousel2.type = iCarouselTypeLinear;
}
于 2013-08-29T03:07:42.290 回答