未从文件中检索内容
我遇到了这个有线问题,我无法从文件中获取内容并用它启动我的 NSMutableArray。
这是我的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Does file exist?: %i", [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory]]);
NSMutableArray *tempArr;
tempArr = [[NSMutableArray alloc] initWithContentsOfFile:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory]];
尝试此操作时,initWithContentsOfFile 返回(null)。检查文件是否存在的行将“1”打印到控制台。
这是我用来保存数据的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[length.text writeToFile:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory] atomically:NO];
我在不同的程序中使用或多或少完全相同的代码,没有任何问题。
这里真的需要一些帮助,也许我现在只是盲目的......
I've got this wired problem, I cannot get the content from the file and initiate my NSMutableArray with it.
Here's my code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Does file exist?: %i", [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory]]);
NSMutableArray *tempArr;
tempArr = [[NSMutableArray alloc] initWithContentsOfFile:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory]];
When trying this, initWithContentsOfFile returns (null). The row checking if the file exist prints '1' to the console.
This is the code I'm using to save the data:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[length.text writeToFile:[NSString stringWithFormat: @"%@/length.txt", documentsDirectory] atomically:NO];
I'm using more or less exactly the same code in a different program without problems.
Really need some help here, perhaps I'm just blind for the moment...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您尝试从文件内容创建数组时, 文件必须采用plist格式,并且最外层的plist元素必须是
< ;数组>
。如果它不具有该格式,初始化将失败并且您的数组将为nil
。您通过将
NSString
写入文件来创建文件,这意味着您可能应该将其读入NSString
,而不是NSArray
>。When you try to create an array from the contents of a file, the file must be in plist format, and the outer-most plist element must be
<array>
. If it doesn't have that format, initialization will fail and your array will benil
.You're creating the file by writing an
NSString
to a file, which means you should probably be reading it in to anNSString
, not anNSArray
.NSArray 的
initWithContentsOfFile:
方法的文档说:您的代码片段中没有包含
length
的声明,但我猜测length.text
返回一个 NSString 对象,而不是 NSArray。因此,您需要使用 NSString(而不是 NSArray)中的initWithContentsOfFile:
从文件中读回该内容。The docs for NSArray's
initWithContentsOfFile:
method say:You don't include the declaration of
length
in your code snippet, but I'm guessing thatlength.text
returns an NSString object, not an NSArray. So you'd need to read that back from a file usinginitWithContentsOfFile:
from NSString, not NSArray.