NSURLConnection后字符串更改

时间:2013-09-28 14:44:25

标签: ios cocoa nsurlconnection

这是我的ViewController.h的一部分:

@interface ViewController : UIViewController <UITextFieldDelegate> {
    NSString *idUser;
    NSMutableData *responseData;
    NSMutableData *responseDataPersonal;
}

这是我的.m:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    if (URLtype == 1)
        responseData = [[NSMutableData alloc] init];
    else if (URLtype = 3)
        responseDataPersonal = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (URLtype == 1)
        [responseData appendData:data];
    else if (URLtype == 2)
        [responseDataPersonal appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    ...
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
     if(URLtype == 1) 
     {
        URLtype = 2;
        // responseData contains this line of text: "true;1234567"
        NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];

            // idUser is set to 1234567
            NSArray *txtString = [txt componentsSeparatedByString: @";"];
            idUser = [txtString objectAtIndex: 1];

            // This NSLog PERFECTLY logs 1234567
            NSLog(@"%@",idUser);


            NSString *post = @"task=something";
            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

            NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

            NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
            [request setURL:[NSURL URLWithString:@"http://www.site.com/script.php"]];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];

            [[NSURLConnection alloc] initWithRequest:request delegate:self];
        }
    }


    else if (URLtype == 2)
    {
        NSLog(@"%@ blabla", idUser);
    }
}

我希望代码末尾的NSLog能够正常工作,但它一直让我在那一行上发生EXC_BAD_ACCESS崩溃。 如果我将NSLog行改为:

,肯定有一些非常奇怪的原因
 NSString *idUser2 = [NSString stringWithFormat:@"%@", idUser];
    NSLog(@"%@ BLA", idUser2);

..它有时会崩溃或每次记录不同的消息!以下是一些例子:

<__NSMallocBlock__: 0x777df70> BLA

HTTP connection to www.site.com:80 BLA

HTTPHeaderDict 0x856d870 [0x856d878]> { <CFBasicHash 0x855a2a0 [0x1b3b4d8]>{type = immutable dict, count = 5,
entries =>
    1 : Content-Length = <CFString 0x85723d0 [0x1b3b4d8]>{contents = "36"}
    3 : Accept-Encoding = <CFString 0x25b48f8 [0x1b3b4d8]>{contents = "gzip, deflate"}
    4 : Content-Type = <CFString 0x3e420 [0x1b3b4d8]>{contents = "application/x-www-form-urlencoded"}
    5 : Accept-Language = <CFString 0x8572a00 [0x1b3b4d8]>{contents = "en-us"}
    6 : Accept = <CFString 0x25b4308 [0x1b3b4d8]>{contents = "*/*"}
}
 } BLA

最近它更喜欢在NSString *idUser2 line.

崩溃..

我做错了什么? :/ 提前致谢

2 个答案:

答案 0 :(得分:1)

试试这个

NSString * theString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

答案 1 :(得分:0)

尝试做:

idUser = nil;

位于“connectionDidFinishLoading”方法的顶部。我的猜测是“idUser”未必设置,而是在到达NSLog线时的某些垃圾或释放值。