Vergmort
Vergmort

Reputation: 399

Calling an array of strings located on a Property List

I need to bring an ARRAY of strings, located on a .plist. I'm making a singleton in order to make it easier. Here is an example of my plist.

<plist version="1.0">
<array>
    <dict>
        <key>id</key>
        <integer>1</integer>
        <key>Texts</key>
        <array>
            <string>Text number one</string>
            <string>Text number two</string>
            <string>Text number three</string>
        </array>
    </dict>
    <dict>
        <key>id</key>
        <integer>2</integer>
        <key>Texts</key>
        <array>
            <string>Text number one</string>
            <string>Text number two</string>
            <string>Text number three</string>
        </array>
    </dict>
</array>
</plist>

and here is my method to bring the array of strings:

- (NSArray*)getStoryTutorialForEnvironmentId:(int)envId andPage:(int)pageNumber{

      NSArray* storyArray = [self.tutorial objectAtIndex:envId];
        NSDictionary* gamingVoicesArray = [storyArray objectAtIndex:pageNumber];
        NSArray *finalArray = [gamingVoicesArray objectForKey:@"Texts"];
        return finalArray;
}

the thing is, I'm getting a crash. I don't know how to make my final array in order to return the text array when calling the singletone. What I'm making wrong?

EDIT: CRASH LOG -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xaa49e70

EDIT 2:

- (id)init{
    self = [super init];
    if (self != nil) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        if ([defaults objectForKey:@"StoryText"] != nil) {
            NSArray* environment_story_temp = [defaults objectForKey:@"MyPlist"];
            self.tutorial = [[NSMutableArray alloc] initWithArray:environment_story_temp];
        }else{
            NSString* plistPath = [[[NSBundle mainBundle] resourcePath]
                                   stringByAppendingPathComponent:@"MyPlist.plist"];
            if (plistPath != nil) {
                self.tutorial = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
                [defaults setObject:self.tutorial forKey:@"MyPlist"];
                [defaults synchronize];
            }
        }
    }
    return self;
}

Upvotes: 0

Views: 95

Answers (1)

Jsdodgers
Jsdodgers

Reputation: 5312

It looks like you are trying to get objectAtIndex twice as if your data is an array of arrays of dictionaries, while it is actually just an array of dictionaries.

Try changing:

  NSArray* storyArray = [self.tutorial objectAtIndex:envId];
  NSDictionary* gamingVoicesArray = [storyArray objectAtIndex:

to:

  NSDictionary* gamingVoicesArray = [self.tutorial objectAtIndex:pageNumber];

Upvotes: 2

Related Questions