gpt4 book ai didi

ios - stringWithContentsOfFile 仅适用于 NSUTF16StringEncoding

转载 作者:行者123 更新时间:2023-11-29 02:52:27 25 4
gpt4 key购买 nike

我正在尝试在 iOS 中读取 .csv 文件,当我使用 NSUTF8StringEncoding 时,它会打印 null - 当我使用 NSUTF16StringEncoding 时,它会将我的文件打印为中文字符。任何人都可以告诉我这里发生了什么吗?我已将 .csv 拖到主项目目录中,并复制文件。这是我的示例代码:

 NSError *error;
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Crane_Data" ofType:@"csv"];
NSString *fileData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"TEST");
NSLog(@"%@",fileData);
NSLog(@"TEST");

使用错误测试后:

 if (!fileData) NSLog(@"%@", error);

我用 NSUTF8 得到了这个

2014-06-20 22:20:57.351 CollectionView[6084:390557] Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0xaf5a7b0 {NSFilePath=/Users/Dylan/Library/Developer/CoreSimulator/Devices/D3F54177-0F08-47A5-9FE0-785E2AF0691A/data/Containers/Bundle/Application/81942FA8-BB67-4E8D-826E-7D1A445DD9B0/CollectionView.app/Crane_Data.csv, NSStringEncoding=4}

并且使用 NSUTF16 在控制台上没有打印任何错误

最佳答案

编码是平面ASCII:

我从注释中取出字节并将它们格式化为 char 字符串,然后创建了一个 NSString:

char* bytes = "\x66\x6f\x72\x74\x75\x6e\x65\x5f\x69\x64\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x61\x63\x74\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x74\x79\x70\x65\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x72\x6f\x6f\x74\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x73\x74\x72\x69\x6e\x67\x2c\x66\x6f\x72\x74\x75\x6e\x65\x5f\x64\x65\x73\x63\x72\x69\x70\x74\x69\x6f\x6e\x0d\x31\x2c\x31\x2c\x63\x6f\x6d\x70\x6f\x73\x69\x74\x69\x6f\x6e\x2c\x70\x2c\x4d\x61\x6b\x65\x20\x61\x6e\x20\x6f\x72\x69\x67\x61\x6d\x69\x20\x63\x72\x61\x6e\x65\x20\x2c\x0d\x32\x2c\x31\x2c\x63\x6f\x6d\x70\x6f\x73\x69\x74\x69\x6f\x6e\x2c\x70\x2c\x57\x72\x69\x74\x65\x20\x6f\x6e\x65\x20\x6c\x69\x6e\x65\x20\x6f\x66\x20\x73\x6f\x6d\x65\x74\x68\x69\x6e\x67\x2e\x2c\x54\x68\x69\x73\x20\x63\x6f\x6d\x70\x6f\x73\x69\x74\x69\x6f\x6e\x20\x63\x68\x61\x6c\x6c\x65\x6e\x67\x65\x20\x69\x73\x20\x69\x6e\x74\x65\x6e\x64\x65\x64\x20\x74\x6f\x20\x62\x65\x20\x63\x72\x65\x61\x74\x69\x76\x65\x2e\x20\x49\x74\x20\x63\x61\x6e\x6e\x6f\x74\x20\x62\x65\x20\x61\x20\x6c\x00";
NSUInteger bytesLength = strlen(bytes);
NSString *string = [[NSString alloc] initWithBytes:bytes length:bytesLength encoding:NSASCIIStringEncoding];
NSLog(@"string: %@", string);

NSLog 输出:

string: fortune_id,fortune_act,fortune_type,fortune_root,fortune_string,fortune_description
1,1,composition,p,Make an origami crane ,
2,1,composition,p,Write one line of something.,This composition challenge is intended to be creative. It cannot be a l

以下是初始回复:
如果是utf-16有三种可能:
NSUTF16StringEncoding
NSUTF16BigEndianStringEncoding
NSUTF16LittleEndianStringEncoding。
三个都试试。

如果失败获取数据:
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
然后记录它:
NSLog(@"fileData: %@", fileData);
发布so的前一百个字节。应该很容易弄清楚格式。
如果您有该信息,请同时发布它应该是什么。

关于ios - stringWithContentsOfFile 仅适用于 NSUTF16StringEncoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24337648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com