我正在构建一个超级简单的应用程序,只是为了练习,我似乎无法在应用程序启动时加载 plist 文件。我正在使用实用程序应用程序模板,下面是我的一些代码。
这是我试图加载数据的地方,如果没有 plist 路径,则创建一个:
-(void) loadData
{
// load data here
NSString *dataPath = [self getFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dataPath];
NSLog(@"Does the file exist? %i", fileExists);
// if there is data saved load it into the person array, if not let's create the array to use
if (fileExists) {
NSLog(@"There is already and array so lets use that one");
personArray= [[NSMutableArray alloc] initWithContentsOfFile:dataPath];
NSLog(@"The number of items in the personArray %i", personArray.count);
arrayNumber.text = [NSString stringWithFormat:@"%i",personArray.count];
}
else
{
NSLog(@"There is no array so lets create one");
personArray = [[NSMutableArray alloc]init];
arrayNumber.text = [NSString stringWithFormat:@"%i",personArray.count];
}
}
这是我有一个可变数组然后尝试将其添加到 plist 的地方。// 将人员对象添加到数组中 [personArray addObject:personObject];
for (Person *obj in personArray)
{
NSLog(@"obj: %@", obj);
NSLog(@"obj.firstName: %@", obj.firstName);
NSLog(@"obj.lastName: %@", obj.lastName);
NSLog(@"obj.email: %@", obj.email);
}
// check the count of the array to make sure it was added
NSLog(@"Count of the personArray is %i", personArray.count);
arrayNumber.text = [NSString stringWithFormat:@"%i",personArray.count];
// check to see if the object was added
NSLog(@"The number of items in the personArray %i", personArray.count);
[personArray writeToFile:[self getFilePath] atomically:YES];
一切似乎都正常,我可以在我的数组中记录数据以确保它在那里,但是当我重新启动应用程序时,它永远不会拉入 plist 数据。我已经为此工作了一段时间,似乎无法取得任何进展。
谢谢!