Cripto
Cripto

Reputation: 3751

Populated array not giving expected results

I've done a lot of object oriented programming in Java but im pretty new to objective C.

Am I doing the following properly.

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

Code in question inside the viewdidload of one of my views

  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];

    }

so this for loop is iterating over a plist that I have. A series of paths.

for each path, it is creating an instance of Packages and adding it to an array of packages.

However, when do I do this

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

I get an empty result back no matter what!

and even when I do this

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

It prints out null.

What am I doing wrong.

EDIT

This is how the picture folder looks like

enter image description here

The code to pull it use to work when all of the pictures where in the Objects directory without any of the sub directories. I hope I edited the code properly to handle the sub dir

Edit 2

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

on the line

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

which successfully prints

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",

When I do

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

which outputs

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
)}

But as you can see in the image above, the file structure does exist.

Solution:

I accepted an answer even though it wasnt the real answer, it was the clear path to find out what was wrong.

The problem was that you can not have referenced folders containing sub folders using the code provided. I just many folders.

Upvotes: 0

Views: 67

Answers (1)

Bryan Chen
Bryan Chen

Reputation: 46598

in your initWithPath:, you have NSString *folderPath = which declare a local variable and assign some value to it. However you did not save the value so self.folderPath is nil

- (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;
}

for logging error

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

NEVER do NSLog([Someclass someMethod]), use NSLog(@"%@", [Someclass someMethod]) instead

Upvotes: 2

Related Questions