1

I'm having an NSObject class where i have an init method defined something like below,

- (id)initWithPlistName:(NSString *)plistFileName{
    if (self = [super init]) {
        plistName = plistFileName;
        plistContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] 
                                                                pathForResource:plistName ofType:@"plist"]]; // this plistContent array is not allocating in memory
    }
    return self;
}

I'm calling this method in my applications AppDelegate Class didFinishLaunchingWithOptions method, plistContent is my iVar of type NSArray but whenever control comes to plistContent alloc init line and while returning self, there is no memory allocated for my array. What may be the problem happening here, Any help is appreciated in advance.

3
  • How have u allocated the initializing of object in appDelegate Commented Sep 25, 2012 at 12:37
  • show d code of ur didFinish in appDelegate.. Commented Sep 25, 2012 at 12:41
  • i have edited my answer, please look .. Commented Sep 25, 2012 at 13:02

3 Answers 3

1

I suppose you have not changed the Datatype of your plist root key in your plist flie from dictionary to array

Sign up to request clarification or add additional context in comments.

Comments

1

Check file exists:

NSString *path = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
if(path)
{
   plistContent = [[NSArray alloc] initWithContentsOfFile:path]; 
}
else
{
  NSLog(@"File Not exists");
}

1 Comment

Your answer is spot on but you don't need to use NSFileManager to check because pathForResource:ofType will return nil if the file doesn't exist. I hope you don't mind me editing your answer :/
0

Try this

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"plistName" ofType:@"plist"];
NSDictionary *myDict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *array = [NSArray arrayWithArray:[myDict objectForKey:@"Root"]];

Comments

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.