0

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;
}
17
  • 1
    Please post information on the crash as well as how you're creating your singleton. Commented Aug 8, 2013 at 15:47
  • Explain about self.tutorial and also about crash. Commented Aug 8, 2013 at 15:48
  • Yes, what is self.tutorial and how is it declared? Commented Aug 8, 2013 at 15:49
  • Also, how are you loading the plist into an array (tutorial ?)? Commented Aug 8, 2013 at 15:49
  • 1
    @Vergmort - You need to search How to fetch plist file in IOS on google. Its simple. Commented Aug 8, 2013 at 15:56

1 Answer 1

2

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];
Sign up to request clarification or add additional context in comments.

2 Comments

your array is: NSArray* finalArray = [gamingVoicesArray objectForKey: @"Texts"];
Yes, I just forgot it. Thanks, now I can see my array of strings. Now I am working to make each string a label.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.