如何在运行时从iPhone的文档目录中读取? [英] how can i read from the documents directory in the iphone at run time?

查看:79
本文介绍了如何在运行时从iPhone的文档目录中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iphone应用程序中实现一个.plist文件,其中包含一些我稍后将在该应用程序中使用的初始参数,我创建了一个NSMutableArray myArray和一个带有一些键的NSMutableDictionary,然后创建了该文件:

I'm implemanting in an iphone app a .plist file that contain some initial parameters that i'll use later in the app, i create a NSMutableArray myArray with a NSMutableDictionary with some keys then i create the file:

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"Initial.plist"];
[myArray writeToFile:filePath atomically:YES];

我将该文件的内容访问为:

i acccess the content of this file as:

NSMutableArray *temp= [[NSMutableArray alloc]initWithContentsOfFile:filePath];

在模拟器中一切正常,但是当我在设备中运行测试时,temp的内容始终为null,我不知道为什么我得到nil Array可能是在访问文件时出错了.

everthing works fine in the simulator but when i run a test in a device the content of temp always is null, i don't have any idea why i get a nil Array maybe i'm doing somthing wrong accessing the file.

推荐答案

在模拟器上以及在设备上filePath的值是什么?

What's the value of filePath on the simulator, and on the device?

在这种情况下,使用stringByAppendingString在我看来似乎很奇怪-难道不是stringByAppendingPathExtension吗?我猜是因为这个原因,它在路径中丢失了一个"/",但是您说它在模拟器上可以工作...

Use of stringByAppendingString seems odd to me in this case -- shouldn't that be stringByAppendingPathExtension instead? I would guess that it's missing a "/" in the path because of that, but you say it works on the simulator...

这篇关于如何在运行时从iPhone的文档目录中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆