2

I am working on iOS application.In one scenario I am sending phone number and some text message to server like this

- (void)postData:(NSString *)message :(NSString *)mobileno
{

NSLog(@"postData is called");
@try
{
    NSString *urlString = [NSString stringWithFormat:@"http://anlgelist.commonshell.net/api/account"];
    NSURL *url = [[NSURL alloc] initWithString:urlString];
    NSMutableURLRequest *urlRequest1=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSMutableDictionary *postDictionary=[[NSMutableDictionary alloc]init];
  [postDictionary setValue:message forKey:@"Message"];
  [postDictionary setValue:mobileno forKey:@"MobileNumber"];
      SBJsonWriter *jsonWriter = [SBJsonWriter new];
   NSError *error = nil;
    NSLog(@"~~~~~~~~ postDictionary is %@",postDictionary);

    NSString * jsonDataString =[jsonWriter stringWithObject:postDictionary];

    if ( ! jsonDataString )
    {
        NSLog(@"Error: %@", error);
    }

    NSData *requestData = [NSData dataWithBytes:[jsonDataString UTF8String] length:[jsonDataString length]];
    [urlRequest1 setHTTPMethod:@"PUT"];
    [urlRequest1 setValue:@"application/json" forHTTPHeaderField:@"content-type"];
    [urlRequest1 setHTTPBody: requestData];
    connection1 = [NSURLConnection connectionWithRequest:urlRequest1 delegate:self];

}
@catch (NSException *exception)
{
    ////NSLog(@"exception is %@",exception);

    @throw exception;
}

}

I am continuously calling this web service to send the details.But sometimes I am getting the following error and app getting crashed.

<Error>: -[__NSCFDictionary _expandedCFCharacterSet]: unrecognized selector sent to instance 0x14fdf350

I had not faced this kind of error previously.I googled but not found the solution.

Crash Report is

enter image description here

  • 2
    What is the stack trace of the crash? – Wain Feb 27 '14 at 11:20
  • 1
    That doesn't really help. Clean the device so it saves logs. Use Xcode to get a stack trace when the exception is thrown so you can see which line it's on. – Wain Feb 27 '14 at 11:32
  • On which line you get expiation? – Oleg Sobolev Feb 27 '14 at 11:42
  • here NSString * jsonDataString =[jsonWriter stringWithObject:postDictionary]; @OlegSobolev –  Feb 27 '14 at 11:46
  • jsonWriter really accepts a parameter of NSDictionary? Maybe wrong encoding? – Oleg Sobolev Feb 27 '14 at 11:50
  • We can't do it for you, since you haven't provided an exception stack dump, but if you examine the stack you will see precisely where you're calling out to some function and precisely where that function encounters an error. The error is caused because an NSDictionary has been substituted for some other object, possibly an NSAttributedString, or maybe some sort of text view. – Hot Licks Feb 27 '14 at 12:33
  • BTW: Had you actually Googled "unrecognized selector sent to instance" you would have gotten thousands of hits. – Hot Licks Feb 27 '14 at 12:36

1 Answers1

-2

I am not sure it will work fine, but in your case the object has released and you try to access that, So i have prefered this solution,please try this.

SBJsonWriter *jsonWriter = [[SBJsonWriter new]retain];

Satish
  • 133
  • 11