Objective c Plists在模拟器上加载,但不在iPhone上加载

Objective c Plists在模拟器上加载,但不在iPhone上加载,objective-c,arrays,plist,Objective C,Arrays,Plist,我把装满字符串的数组保存到我的plists中,在模拟器上一切正常,但当我用iPhone加载应用程序时,保存到plists中的数组返回null。代码如下: NSFileManager *fileManager = [NSFileManager defaultManager]; // Get path to balance.plist in the Documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains(

我把装满字符串的数组保存到我的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];
}


有什么建议吗?

所以我发现您必须从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];

我不懂密码。。。这似乎与你说的和想做的不一样——我不知道你的意思,你能澄清一下吗?谢谢。
balanceLabels
(和其他)是标签数组吗?balanceLabels和其他是保存标签数组的属性strings@Daij-杰恩:哎呀,我不知道当时在想什么。我的错误。