-2

当我在下面记录 vaule 时responseString,它​​总是有随机字符 - 这是 php 问题还是 ios 问题?

NSURLResponse *response;
NSError *error;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(@"%@", responseString);

NSString *success = @"200200";
[success dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%lu", (unsigned long)responseString.length);
NSLog(@"%lu", (unsigned long)success.length);
//responseString.length should = success.length but doesn't :(
4

2 回答 2

0

字符串是不可变的,所以这一行不做任何事情:

[success dataUsingEncoding:NSUTF8StringEncoding];

你的意思是把它分配给某个东西吗?

NSData *successData = [success dataUsingEncoding:NSUTF8StringEncoding];
于 2013-09-15T01:12:01.417 回答
0

[responseData bytes]只是返回一个指向字节的指针responseData没有NUL 终止。因此,您会在responseString.

为了正确转换,请使用

NSString *responseString = [NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
于 2013-09-15T05:52:20.273 回答