Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 无法在设备上读取/写入plist,但可以在模拟器中读取_Iphone_Objective C_Plist - Fatal编程技术网

Iphone 无法在设备上读取/写入plist,但可以在模拟器中读取

Iphone 无法在设备上读取/写入plist,但可以在模拟器中读取,iphone,objective-c,plist,Iphone,Objective C,Plist,我已经看过很多关于Stackoverflow的类似问题的帖子,但是我仍然不明白为什么我不能写/读我的plist。 下面是我将plist加载到应用程序中的方法。 这会将plist复制到设备的documents文件夹,而不是resources文件夹,对吗 -(void)loadPlist:(NSString*)plistString { NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDir

我已经看过很多关于Stackoverflow的类似问题的帖子,但是我仍然不明白为什么我不能写/读我的plist。 下面是我将plist加载到应用程序中的方法。 这会将plist复制到设备的documents文件夹,而不是resources文件夹,对吗

-(void)loadPlist:(NSString*)plistString {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", plistString]];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath:path]) {
    NSString *bundle = [[NSBundle mainBundle]pathForResource:plistString ofType:@"plist"];

    [fileManager copyItemAtPath:bundle toPath:path error:&error];
} else {
    NSLog(@"%@ already exists",path);
}
}
这是我写给plist的方式:

-(void)writeScore:(NSString*)objectstring forPlayer:(int)playerTag {
switch (playerTag) {
    case 1:
        NSLog(@"Wrote to 1");
        holeInt = [P1hole_Label.text integerValue];
        plistpath = [[NSBundle mainBundle]pathForResource:@"Scores" ofType:@"plist"];
        plistdata = [[NSMutableDictionary alloc]initWithContentsOfFile:plistpath];
        [plistdata setObject:[NSString stringWithString:objectstring] forKey:[NSString stringWithFormat:@"Hole %i", holeInt]];
        [plistdata writeToFile:plistpath atomically:YES];
        break; 
Etc.
下面是我如何阅读plist的:

-(void)createNewHoleLabel:(NSString*)plistString andSetX:(int)x {
//SetX; 0 = P1, 1 = P2 etc.

UILabel *label = [UILabel new];
[label setFrame:CGRectMake(80 + 53 * x, 93 + 26 * integer, 50, 25)];

NSString *path = [[NSBundle mainBundle]pathForResource:plistString ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithContentsOfFile:path];

label.text = [dict objectForKey:[NSString stringWithFormat:@"Hole %i", integer + 1]];

[label setAdjustsFontSizeToFitWidth:YES];
[label setTextAlignment:NSTextAlignmentCenter];
integer++;
[scrollview addSubview:label];
}

我希望你能帮助我。如果需要,请询问更多详细信息。

writeScore:forPlayer:

plistpath = [[NSBundle mainBundle]pathForResource:@"Scores" ofType:@"plist"];
...
[plistdata writeToFile:plistpath atomically:YES];

您正在尝试写入应用程序的捆绑包,但无法在设备上完成。您应该在文档目录中编写文档。

不,每个人都会犯这样的错误。