user1447300
user1447300

Reputation:

Read from file.Plist Returns Null

Program that Creates multiple Plist's Paths for Different information.

But only one path is not working. (i think "writeToFile" is the problem)

code:

-(NSString *) createPath:(NSString *)withFileName
{
   NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,
YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *path = [documentsDirectory stringByAppendingPathComponent:withFileName];
   return path;
}

Path

NSLog = /var/mobile/Applications/02CABC0A-6B5B-4097-A9D1-4336BE8230B7/Documents/MessagesDB.plist

&

-(void) messagesDbFlush
{
    // save it to the file for persistency
    NSString *messagesDB_Path = [self createPath:_fileMessagesDB];
    [_messagesDB writeToFile:messagesDB_Path atomically:YES];
    NSMutableArray *ReturnsInfo = [[NSMutableArray alloc ]initWithContentsOfFile:messagesDB_Path];
    NSLog(@"ReturnsInfo is : %@", ReturnsInfo);
}

"ReturnsInfo" Array is Null :/

Anyone please help?

Upvotes: 0

Views: 530

Answers (2)

Jeremiah Smith
Jeremiah Smith

Reputation: 740

I once had the same error.
1) Check the name of the plist in the directory listing to match your coded one
2) Check Project settings, manually delete the pre-existing plist from the "Build Settings" > "Copy Bundle Resources", and drag drop from the list.
3) Select the plist in directory listing, check Utilities sidebar, check Identity & Type > Location as valid
4) If you deleted the app's "default" plist aka bundle identifier, add copy build phase, choose destination, choose pref folder as absolut path check "copy only when installing"

This solved my returning null.

And if all fails on the bundle identifier, you can always copy the plist to pref folder by code:

NSString *path = [@"~/Library/Preferences/com.MyCompany.MyApp.plist" stringByExpandingTildeInPath];

BOOL PrefsExist=[[NSFileManager defaultManager] fileExistsAtPath:path];
NSString *copyPrefsPath = [@"~/Library/Preferences/com.MyCompany.MyApp.plist" stringByExpandingTildeInPath];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (PrefsExist == 0)
    {
        // Copy the plist to prefs folder (one-time event)
        NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"com.MyCompany.MyApp" ofType:@"plist"];
        [fileManager copyItemAtPath:tessdataPath toPath:path error:&error];
    } else
    {
        // Read/Write the values from plist
    }

Upvotes: 1

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

i have stored following array with 5 objects in it, its working fine on my side, try it

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];
for(int i=0;i<5;i++)
    [array addObject:@"This is Demo String, You can write your own String here"];

NSString *_fileMessagesDB = @"MessagesDB.plist";
// save it to the file for persistency
NSString *messagesDB_Path = [self createPath:_fileMessagesDB];
[array writeToFile:messagesDB_Path atomically:YES];
NSMutableArray *ReturnsInfo = [[NSMutableArray alloc ]initWithContentsOfFile:messagesDB_Path];
NSLog(@"ReturnsInfo is : %@", ReturnsInfo);

Upvotes: 0

Related Questions