无法在 XCODE 4.2 中读取文件,但在 4.0 中可以工作?

发布于 2024-12-14 08:22:10 字数 896 浏览 1 评论 0原文

我已经从 XCODE 4 升级到 4.2,现在遇到了问题。

以下代码在 4.2 之前工作,用于读取“filePath”中的文件:

// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile; 
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];

// Load array
NSArray* myArray = [myString componentsSeparatedByString:@"\r"];
NSLog (@"\n \n Number of elements in myArray = %i", [myArray count]);

在 4.2 中,以下代码行中的“initWithContentsOfFile”已被弃用:

NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];

...并且应该根据手册替换为以下内容:

NSString *myString = [NSString stringWithContentsOfFile:filePath encoding: NSUTF8StringEncoding error:&err];

并且我无法得到这个通过替换代码行来读取同一文件中的记录。顺便说一句,我已经定义了 &err。

当我 NSLog myString 时,我得到(null)。

我有点迫切地想解决这个问题,非常感谢任何帮助。

干杯

I have upgraded from XCODE 4 to 4.2 and now i have problems.

The following code worked pre 4.2 to read the file in "filePath":

// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile; 
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];

// Load array
NSArray* myArray = [myString componentsSeparatedByString:@"\r"];
NSLog (@"\n \n Number of elements in myArray = %i", [myArray count]);

With 4.2 the "initWithContentsOfFile" in the following code line is deprecated:

NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath];

...and should be replaced with the below according to the manual:

NSString *myString = [NSString stringWithContentsOfFile:filePath encoding: NSUTF8StringEncoding error:&err];

and i can not get this to read the records in the same file by replacing the code line. BTW, i have defined the &err.

When i NSLog myString i get (null).

I am getting a bit desperate to solve this and would very much appreciate any help.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

涫野音 2024-12-21 08:22:10

NSLog err 变量(如果有错误)。还有NSLog filePath

也许编码不是 UTF-8,您确定编码吗?

最好的非 UTF-8 编码是 NSMacOSRomanStringEncoding,它支持 8 位字符。

NSLog the err variable if there is an error. Also NSLog filePath.

Perhaps the encoding is not UTF-8, are you sure about the encoding?

The best non-UTF-8 encoding bet is NSMacOSRomanStringEncoding which supports 8-bit characters.

笙痞 2024-12-21 08:22:10

尝试:

    NSError* error = nil;
    NSStringEncoding encoding;
    NSString *fileContent = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error];

如果这不起作用,请尝试使用您的代码:NSASCIIStringEncoding

Try :

    NSError* error = nil;
    NSStringEncoding encoding;
    NSString *fileContent = [NSString stringWithContentsOfFile:filePath usedEncoding:&encoding error:&error];

If that does not works, try in your code : NSASCIIStringEncoding

场罚期间 2024-12-21 08:22:10

该文件可能不包含 UTF8 编码的字符串。请参阅苹果的文档,其中有一个读取不知道编码的文件的示例: 使用未知编码读取数据

您需要使用[string|init]WithContentsOfFile:usedEncoding:error 方法,如果失败,在最终向用户呈现错误消息之前,您还可以尝试其他一些操作(例如,尝试将其读取为NSAttributedString)。

例如,您可以这样做:

// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile; 
NSStringEncoding encoding;
NSError *error;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath usedEncoding:&encoding error:&error];

if (!myString) {
  myString = [[NSString alloc] encoding:NSUTF8StringEncoding error:&error]
}

if (!myString) {
  myString = [[NSString alloc] encoding:NSISOLatin1StringEncoding error:&error]
}

if (!myString) {
  NSLog(@"error: %@", error);
  return;
}


// Load array
NSArray* myArray = [myString componentsSeparatedByString:@"\r"];
NSLog (@"\n \n Number of elements in myArray = %i", [myArray count]);

The file probably doesn't contain a UTF8 encoded string. See apple's documentation, which has an example of reading a file where you do not know the encoding: Reading data with an unknown encoding

You need to use the [string|init]WithContentsOfFile:usedEncoding:error method, and if that fails there are a few more things you can try before finally presenting an error message to the user (for example, try reading it as an NSAttributedString).

For example, you could do this:

// Fill myString with questions from the .txt file and then read .txt file
NSString *filePath = whatFile; 
NSStringEncoding encoding;
NSError *error;
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath usedEncoding:&encoding error:&error];

if (!myString) {
  myString = [[NSString alloc] encoding:NSUTF8StringEncoding error:&error]
}

if (!myString) {
  myString = [[NSString alloc] encoding:NSISOLatin1StringEncoding error:&error]
}

if (!myString) {
  NSLog(@"error: %@", error);
  return;
}


// Load array
NSArray* myArray = [myString componentsSeparatedByString:@"\r"];
NSLog (@"\n \n Number of elements in myArray = %i", [myArray count]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文