Plists在模拟器上加载但不在iPhone上

时间:2014-08-17 21:21:56

标签: objective-c arrays plist

我的数组中填充了保存到我的plists的字符串,一切都在模拟器上正常运行但是当我用我的iPhone加载应用程序时,保存到plists的数组将返回null。这是代码:

NSFileManager *fileManager = [NSFileManager defaultManager];


// Get path to balance.plist in the Documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                     NSDocumentDirectory,
                                                     NSUserDomainMask, YES
                                                     );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"balance.plist"];
NSString *path2 = [documentsDirectory stringByAppendingPathComponent:@"interest.plist"];
NSString *path3 = [documentsDirectory stringByAppendingPathComponent:@"principal.plist"];
NSString *path4 = [documentsDirectory stringByAppendingPathComponent:@"dates.plist"];
NSString *path5 = [documentsDirectory stringByAppendingPathComponent:@"paymentperiods.plist"];
NSMutableArray *testArray;
NSMutableArray *interestArray;
NSMutableArray *principalArray;
NSMutableArray *dateArray;
NSMutableArray *paymentPeriods;
// Check to see if the plist exists, if not create it
if ([fileManager fileExistsAtPath: path])
{
    // If the file exists, read the array from file
    testArray = [[NSMutableArray alloc] initWithArray:balanceLabels];
    interestArray =[[NSMutableArray alloc] initWithArray:interestLabels];
    principalArray = [[NSMutableArray alloc] initWithArray:pricipalLabels];
    dateArray = [[NSMutableArray alloc] initWithArray:dateValues];
    paymentPeriods = [[NSMutableArray alloc] initWithArray:paymentAmounts];

}
else
{
// If the file doesn’t exist, create an empty array
balanceLabels = [[NSMutableArray alloc] init];

}


[testArray writeToFile:path atomically:YES];
[interestArray writeToFile:path2 atomically:YES];
[principalArray writeToFile:path3 atomically:YES];
[dateArray writeToFile:path4 atomically:YES];
[paymentPeriods writeToFile:path5 atomically:YES];

}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

所以我想你必须从NSString更改为NSURL

NSURL *documentsDirectory = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory
                                                           inDomains:NSUserDomainMask].firstObject;
NSURL *path = [documentsDirectory URLByAppendingPathComponent:@"balance.plist"];
NSURL *path2 = [documentsDirectory URLByAppendingPathComponent:@"interest.plist"];
NSURL *path3 = [documentsDirectory URLByAppendingPathComponent:@"principal.plist"];
NSURL *path4 = [documentsDirectory URLByAppendingPathComponent:@"dates.plist"];
NSURL *path5 = [documentsDirectory URLByAppendingPathComponent:@"paymentperiods.plist"];
NSMutableArray *testArray;
NSMutableArray *interestArray;
NSMutableArray *principalArray;
NSMutableArray *dateArray;
NSMutableArray *paymentPeriods;
// Check to see if the plist exists, if not create it
if ([fileManager fileExistsAtPath:[documentsDirectory path]])
{
    // If the file exists, read the array from file
    testArray = [[NSMutableArray alloc] initWithArray:balanceLabels];
    interestArray =[[NSMutableArray alloc] initWithArray:interestLabels];
    principalArray = [[NSMutableArray alloc] initWithArray:pricipalLabels];
    dateArray = [[NSMutableArray alloc] initWithArray:dateValues];
    paymentPeriods = [[NSMutableArray alloc] initWithArray:paymentAmounts];

}
else
{
// If the file doesn’t exist, create an empty array
balanceLabels = [[NSMutableArray alloc] init];

}
[testArray writeToURL:path atomically:YES];
[interestArray writeToURL:path2 atomically:YES];
[principalArray writeToURL:path3 atomically:YES];
[dateArray writeToURL:path4 atomically:YES];
[paymentPeriods writeToURL:path5 atomically:YES];