var t="";for(int i=0;i++<12;)Console.Write("On the {0}{1} day of Christmas, my true love gave to me: {2}\n",i,i<2?"st":i<3?"nd":i<4?"rd":"th",t="|a partridge in a pear tree.|two turtle doves, and |three french hens,|four calling birds,|five gold rings|six geese a-lay@seven swans a-swimm@eight maids a-milk@nine ladies danc@ten lords a-leap@eleven pipers pip@twelve drummers drumm@".Replace("@","ing,|").Split('|')[i]+t);
var t="";
for(int i = 0; i++ < 12;)
Console.Write("On the {0}{1} day of Christmas, my true love gave to me: {2}\n",
i,
i < 2 ? "st" : i < 3 ? "nd" : i < 4 ? "rd" : "th",
t="|a partridge in a pear tree.
|two turtle doves, and
|three french hens,
|four calling birds,
|five gold rings
|six geese a-lay
@seven swans a-swimm
@eight maids a-milk
@nine ladies danc
@ten lords a-leap
@eleven pipers pip
@twelve drummers drumm@"
.Replace("@","ing,|")
.Split('|')[i]+t);
I think the garbage comes from the logging:
[NSString stringWithUTF8String:self.rawData.bytes]
Here you are saying that you want an NSString
from this C-string (= array of bytes terminated by zero). The problem is that the bytes
method of NSData
does not return data terminated by a zero, because it’s an ordinary array, not a C-string. Therefore the NSString
initializer grabs even the bytes after the end of the received NSData
, until it reaches some zero byte previously stored in memory.
попробуйте следующее:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[rawData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%@",rawData);
}
И что вы объявили rawData как ???
Чтобы создать NSString
из NSData
, вы должны использовать initWithData: encoding:
, например,
NSString *str = [[NSString alloc] initWithData:self.rawData
encoding:NSUTF8StringEncoding];
NSLog(@"Before: %@", str);
[str release];
] Обработка байтов NSData
как строки C может вызвать некоторые уязвимости системы безопасности.