我嘗試使用NSInputStream在iOS中讀取一個大文件,用換行符分隔文件行(我不想使用componentsSeparatedByCharactersInSet
,因爲它使用了太多的內存)。如何讀取UTF-8的NSInputStream?
但是由於並非所有的行看起來都是UTF-8編碼的(因爲它們可能只是ASCII,相同的字節),我經常會得到Incorrect NSStringEncoding value 0x0000 detected. Assuming NSASCIIStringEncoding. Will stop this compatiblity mapping behavior in the near future.
警告。
我的問題是:有沒有辦法通過例如抑制這種警告設置編譯器標誌?
此外:它是保存來追加/連接兩個緩衝讀取,從字節流中讀取,然後將緩衝區轉換爲字符串,然後追加字符串可以使字符串損壞?
下面的示例方法演示了字符串轉換將放棄UTF-8字符的前半部分和後半部分,因爲它是無效的。
- (void)NSInputStreamTest {
uint8_t testString[] = {0xd0, 0x91}; // @"Б"
// Test 1: Read max 1 byte at a time of UTF-8 string
uint8_t buf1[1], buf2[1];
NSString *s1, *s2, *s3;
NSInteger c1, c2;
NSInputStream *inStream = [[NSInputStream alloc] initWithData:[[NSData alloc] initWithBytes:testString length:2]];
[inStream open];
c1 = [inStream read:buf1 maxLength:1];
s1 = [[NSString alloc] initWithBytes:buf1 length:1 encoding:NSUTF8StringEncoding];
NSLog(@"Test 1: Read %d byte(s): %@", c1, s1);
c2 = [inStream read:buf2 maxLength:1];
s2 = [[NSString alloc] initWithBytes:buf2 length:1 encoding:NSUTF8StringEncoding];
NSLog(@"Test 1: Read %d byte(s): %@", c2, s2);
s3 = [s1 stringByAppendingString:s2];
NSLog(@"Test 1: Concatenated: %@", s3);
[inStream close];
// Test 2: Read max 2 bytes at a time of UTF-8 string
uint8_t buf4[2];
NSString *s4;
NSInteger c4;
NSInputStream *inStream2 = [[NSInputStream alloc] initWithData:[[NSData alloc] initWithBytes:testString length:2]];
[inStream2 open];
c4 = [inStream2 read:buf4 maxLength:2];
s4 = [[NSString alloc] initWithBytes:buf4 length:2 encoding:NSUTF8StringEncoding];
NSLog(@"Test 2: Read %d byte(s): %@", c4, s4);
[inStream2 close];
}
輸出:
2013-02-10 21:16:23.412 Test[11144:c07] Test 1: Read 1 byte(s): (null)
2013-02-10 21:16:23.413 Test[11144:c07] Test 1: Read 1 byte(s): (null)
2013-02-10 21:16:23.413 Test[11144:c07] Test 1: Concatenated: (null)
2013-02-10 21:16:23.413 Test[11144:c07] Test 2: Read 2 byte(s): Б
那麼,你的問題是什麼? – 2013-02-10 14:27:02
@ 0x7fffffff我加了問題,對不起。它可能也是雙重的。 – Kreisquadratur 2013-02-10 18:35:20