未从文件中检索内容

发布于 2024-09-16 06:09:15 字数 1002 浏览 0 评论 0原文

我遇到了这个有线问题,我无法从文件中获取内容并用它启动我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅过去的我 2024-09-23 06:09:15

当您尝试从文件内容创建数组时, 文件必须采用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 be nil.

You're creating the file by writing an NSString to a file, which means you should probably be reading it in to an NSString, not an NSArray.

梦旅人picnic 2024-09-23 06:09:15

NSArray 的 initWithContentsOfFile: 方法的文档说:

返回值 初始化为包含文件内容的数组
由 aPath 指定,如果文件则为 nil
无法打开或内容
文件无法解析为数组。
返回的对象可能不同
比原来的接收者。

您的代码片段中没有包含 length 的声明,但我猜测 length.text 返回一个 NSString 对象,而不是 NSArray。因此,您需要使用 NSString(而不是 NSArray)中的 initWithContentsOfFile: 从文件中读回该内容。

The docs for NSArray's initWithContentsOfFile: method say:

Return Value An array initialized to contain the contents of the file
specified by aPath or nil if the file
can’t be opened or the contents of the
file can’t be parsed into an array.
The returned object might be different
than the original receiver.

You don't include the declaration of length in your code snippet, but I'm guessing that length.text returns an NSString object, not an NSArray. So you'd need to read that back from a file using initWithContentsOfFile: from NSString, not NSArray.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文