填充数组未给出预期结果

时间:2014-03-24 03:23:20

标签: ios objective-c cocoa-touch

我已经在Java中完成了很多面向对象的编程,但对于目标C来说我还是很新的。

我是否正确地做了以下事情。

Package.h

@interface Packages : NSObject
@property (nonatomic) NSArray *pictureArray;
@property (nonatomic) NSString *folderPath;
- (id)initWithPath:(NSString *)path;
-(NSArray *)getPackageItems;
-(NSString *)getFolderPath;
@end

Package.m

@implementation Packages

- (id)initWithPath:(NSString *)path
{
    self = [super init];
    if (self)
    {
        NSString *folderPath = [NSString stringWithFormat:@"%@/Objects/%@", [[NSBundle mainBundle] bundlePath], path]; //Path
        NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath  error: nil];
        self.pictureArray = [fileList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", @"png",  @"(content BEGINSWITH %@)", @"object_"]];
    }
    for (NSString *s in self.pictureArray) {
        NSLog(@"%@", s);
    }
    return self;
}

-(NSArray *)getPackageItems{
    return self.pictureArray;
}

-(NSString *)getFolderPath{
return self.folderPath;
}    
@end

我的一个观点的viewdidload中有问题的代码

  NSMutableArray *packages = [[NSMutableArray alloc] init];
    for(id key in temp){
        NSLog(@"key=%@ value=%@", key, [temp objectForKey:key]);
        Packages *tmp = [[Packages alloc] initWithPath:[temp objectForKey:key]];
        [packages addObject:tmp];

    }

所以这个for循环遍历了我拥有的一个plist。一系列路径。

对于每个路径,它创建一个Packages实例并将其添加到一个包数组中。

但是,我什么时候这样做

NSArray *something = [packages[1] getPackageItems];

无论如何,我得到一个空的结果!

甚至当我这样做时

NSLog(@"%@", [packages[0] getFolderPath]);

打印出null。

我做错了什么。

修改

这是图片文件夹的样子

enter image description here

当Objects目录中没有任何子目录的所有图片时,拉它的代码用来工作。我希望我能正确编辑代码以处理子目录

修改2

NSError *error;
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.folderPath  error: &error];
NSLog(@"%@, %@", fileList , error);

就行了

NSString *folderPath = [NSString stringWithFormat:@"%@/Objects", [[NSBundle mainBundle] bundlePath], path]; //Path

成功打印

2014-03-23 21:25:32.200 ScrollBar[4475:60b] (
    "object_eyes_0_0.png",
    "object_eyes_10_1.png",
    "object_eyes_10_2.png",
    "object_eyes_10_3.png",
    "object_eyes_10_4.png",
    "object_eyes_10_5.png",
    "object_eyes_10_6.png",
    "object_eyes_10_7.png",

当我这样做时

NSString *folderPath = [NSString stringWithFormat:@"%@/Objects/%@/objects", [[NSBundle mainBundle] bundlePath], path]; //Path

输出

2014-03-23 21:27:46.951 ScrollBar[4496:60b] (null), Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x8e2dda0 {NSUnderlyingError=0x8e2d020 "The operation couldn’t be completed. No such file or directory", NSFilePath=/Users/dev/Library/Application Support/iPhone Simulator/7.1/Applications/8B55D440-5727-471E-9BCC-1513C190740E/ScrollBar.app/Objects/object_eye_0_0/objects, NSUserStringVariant=(
    Folder
)}

但是正如您在上图中看到的那样,文件结构确实存在。

解决方案:

虽然不是真正的答案,但我接受了答案,这是找出问题的明确途径。

问题是您不能使用提供的代码引用包含子文件夹的文件夹。我只是很多文件夹。

1 个答案:

答案 0 :(得分:2)

initWithPath:中,您NSString *folderPath =声明了一个局部变量并为其指定了一些值。但是,您没有保存该值,因此self.folderPathnil

- (id)initWithPath:(NSString *)path
{
    self = [super init];
    if (self)
    {
        self.folderPath = [NSString stringWithFormat:@"%@/Objects/%@", [[NSBundle mainBundle] bundlePath], path];
        NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.folderPath  error: nil];
        self.pictureArray = [fileList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", @"png",  @"(content BEGINSWITH %@)", @"object_"]];
    }
    for (NSString *s in self.pictureArray) {
        NSLog(@"%@", s);
    }
    return self;
}

用于记录错误

NSError *error;
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.folderPath  error: &error];
NSLog(@"%@, %@", fileList , error);

永远不要NSLog([Someclass someMethod]),而是使用NSLog(@"%@", [Someclass someMethod])