NSURLConnection делегируют didReceiveData конечные символы в данных

C#, 421 Символ

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);
5
задан jsamsa 10 July 2009 в 14:50
поделиться

3 ответа

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.

6
ответ дан 18 December 2019 в 09:09
поделиться

попробуйте следующее:

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [rawData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"%@",rawData);
}

И что вы объявили rawData как ???

1
ответ дан 18 December 2019 в 09:09
поделиться

Чтобы создать NSString из NSData , вы должны использовать initWithData: encoding: , например,

NSString *str = [[NSString alloc] initWithData:self.rawData
                                      encoding:NSUTF8StringEncoding];
NSLog(@"Before: %@", str);
[str release];

] Обработка байтов NSData как строки C может вызвать некоторые уязвимости системы безопасности.

11
ответ дан 18 December 2019 в 09:09
поделиться