获取isEqualToString:无法识别的选择器发送到实例错误

时间:2011-09-28 18:26:00

标签: iphone objective-c cocoa-touch ipad memory-management

我在点击同一个UIButton两次时遇到此错误,只有在无法通过服务器的请求时才会第二次崩溃。

-[SendHelloFax isEqualToString:]: unrecognized selector sent to instance 0x12b49150

更新:我使用断点并发现它崩溃的行是:

    if (!errorMessage || [errorMessage isEqualToString:@""]) 
{
        errorMessage = @"Failed to send fax. Please check your WiFi or 3G connection and try again.";
    }

方法:

- (IBAction)sendFaxButtonClicked:(id)sender
{
    NSString *errorMessage;
    int rcode = [MyDataSource sendFax:self.appointment phone_call_id:self.phone_call_id document_url:self.document_url targetId:self.contact_id targetName:self.name.text targetNumber:self.faxNumber.text coverSheetMessage:self.coverSheetMessage.text errorMessage:&errorMessage];

    if (rcode) {
        if (!errorMessage || [errorMessage isEqualToString:@""]) {
            errorMessage = @"Failed to send fax. Please check your WiFi or 3G connection and try again.";
        }

        UIAlertView *someError = [[UIAlertView alloc] initWithTitle: @"Error" message:errorMessage delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
        [someError show];
        [someError release];
        return;
    }

    if (!rcode) {   
        [self dismissModalViewControllerAnimated:FALSE];
    }
}

+(int)sendFax:(int)appointment_id phone_call_id:(int)phone_call_id document_url:(NSString*)document_url targetId:(int)contactId targetName:(NSString*)targetName targetNumber:(NSString*)targetNumber coverSheetMessage:(NSString*)coverSheetMessage errorMessage:(NSString**)errorMessage
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/ichrono/20110715/60b88126/fax_send/", [self getMyHost]]];

    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
    [self addCurrentUserLoginToPostRequest:request];        

    [request setPostValue:[NSString stringWithFormat:@"%d", contactId] forKey:@"fax_contact_id"];
    [request setPostValue:[NSString stringWithFormat:@"%d", appointment_id] forKey:@"appointment_id"];
    [request setPostValue:[NSString stringWithFormat:@"%d", phone_call_id] forKey:@"phone_call_id"];

    [request setPostValue:document_url forKey:@"url_to_fax"];
    [request setPostValue:targetNumber forKey:@"fax_number"];
    [request setPostValue:targetName forKey:@"full_name"];
    [request setPostValue:coverSheetMessage forKey:@"coversheet_message"];


    [request startSynchronous];

    NSError *error = [request error];
    NSString *responseString;
    if (!error) {
        responseString = [request responseString];
    } else {
        return -1;
    }

    NSMutableDictionary *temp = [responseString JSONValue];
    *errorMessage = [temp valueForKey:@"errors"];

    if ([[temp valueForKey:@"status"] isEqualToString:@"ok"]) {
        return 0;
    } else {
        return -1;
    }   
}

2 个答案:

答案 0 :(得分:1)

最好的办法是errorMessage未初始化,即使NSString不为0,也未设置为rcode

尝试:NSString *errorMessage - nil;并检查以确保在此实例中设置了errorMessage

答案 1 :(得分:1)

在方法开始时,您定义 - 无需初始化 - :


NSString *errorMessage;

你实际上是在定义一个尚未设置为有效值的id(使用alloc-init或者只是nil的有效字符串),因此errorMessage指向的内存区域内容无效,即它可以是任何东西:一个无效区域(将导致EXC_BAD_ACCESS)或现在dealloc'd类以前采取的另一个区域(如在您的情况下:这将引发无效的选择器异常)或在最坏的情况下由另一个区域采取的区域现在dealloc'd NSString(难以调试!)。 稍后你调用sendFax:phone_call_id:...方法,该方法返回rcode并具有更新errorMessage指针的副作用,让它指向一个有效的地址(或nil)。它没有,然后当您检查errorMessage时,您会收到上述错误之一。 我想从你的代码中设置rcode时发生了一些错误。在这种情况下,此方法必须为errorMessage指定一些内容,即使是nil或空字符串,以避免此错误。所以sendFax:方法可能缺少这个细节。