开发者

Getting isEqualToString: unrecognized selector sent to instance error

开发者 https://www.devze.com 2023-04-09 06:50 出处:网络
I\'m getting this error when clicking the same UIButton twice, it crashes the second time only when the request to the server is not able to go through.

I'm getting this error when clicking the same UIButton twice, it crashes the second time only when the request to the server is not able to go through.

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

Update: I used breakpoints and found that the line it is crashing at is:

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

Methods:

- (IBAction)sendFaxButtonClicked:(id)sender
{
    NSString *errorMessage;
    int rc开发者_高级运维ode = [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;
    }   
}


Best bet is that errorMessage is not initialized and not set to a NSString even though rcode is not 0.

Try: NSString *errorMessage - nil; and checking to insure errorMessage is set in this instance.


When, at the beginning of your method, you define - without initialization -:


NSString *errorMessage;

you're in fact defining an id which has not been set to a valid value (that is a valid string using alloc-init or simply nil) so the memory area content pointed by errorMessage is not valid, that is it can be anything: an invalid area (which will lead to EXC_BAD_ACCESS) or another area previously taken by a now dealloc'd class (like in your case: this will raise an invalid selector exception) or in the worst case an area taken by another now dealloc'd NSString (difficult to debug!). Later you call sendFax:phone_call_id:... method which returns rcode and has as side effect to update the errorMessage pointer by let it point to a valid address (or nil). It doesn't and then when you check errorMessage you get one of the errors above. I suppose from your code that when rcode is set some error occurred. In such case this method must assign something to errorMessage, even a nil or an empty string, to avoid this error. So probably the sendFax: method is missing this detail.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号