0

这是我的 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.

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

4

2 回答 2

1

试试这个

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

于 2013-09-28T15:28:11.570 回答
0

尝试做一个:

idUser = nil;

在“ connectionDidFinishLoading”方法的顶部。我的猜测是“ idUser”不一定已设置,而是在到达该 NSLog 行时是一些垃圾或释放值。

于 2013-09-28T14:53:40.533 回答