cfstring长度消息发送到deallocated实例?

时间:2012-12-11 10:33:07

标签: iphone xcode autocomplete

我正在为textField做自动完成功能的概念,这样做我收到以下错误: “cfstring length message发送到deallocated instance”

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   if( textField == txtcity)
    {

     autocompleteTableView.hidden = NO;

     NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
    }
}

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

    subString2=[NSString stringWithFormat:@"%@",substring];


    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;
    [data release];

    NSURL *jsonUrl =[NSURL URLWithString:[NSString stringWithFormat:@"http://210.90.32.122/services/AutoService.svc/GetCities/?p=%@&k=%@",substring,txtId.text]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:jsonUrl];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                                  delegate:self];
     self.connection = connection;
    [connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{
    [receivedData setLength:0];

}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{
    [receivedData appendData:data];

}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    parser = [[NSXMLParser alloc] initWithData:receivedData];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
    [parser release];
    NSLog(@"%@",arr2);
    if([arr2 count]!=0)
    {

        self.autocompleteUrls = [[NSMutableArray alloc] init];
        if(autocompleteTableView)
            [autocompleteTableView removeFromSuperview];
        autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(88, 428, 200,[arr2 count]*20) style:UITableViewStyleGrouped];
        autocompleteTableView.delegate = self;
        autocompleteTableView.dataSource = self;
        autocompleteTableView.scrollEnabled = YES;
        autocompleteTableView.rowHeight=20;

        [self.view addSubview:autocompleteTableView];
               for(int i=0;i<[arr2 count];i++)
        {
            NSString *curString = [[arr2 objectAtIndex:i] valueForKey:@"Name"];

            NSRange substringRange = [curString rangeOfString:subString2];  //error at this line 

            if (substringRange.location == 0)
                [autocompleteUrls addObject:curString];

        }
    }


    [autocompleteTableView reloadData];
    txtcity.text=subString2;
    [txtcity resignFirstResponder];

}

我的subString2变为null。但是我在保留它时保留了它

@property(nonatomic,retain)NSString * subString2;

我出错的地方。不明白它被解除分配的地方..

1 个答案:

答案 0 :(得分:2)

你实际上并没有保留它。你设置对象变量subString2不是属性,这就是原因。试试这个:

self.subString2 = [NSString stringWithFormat:@"%@",substring];

但是通过属性从对象获取对象变量不是很好的做法。有两种选择:

[subString2 release];
subString2 = [[NSString stringWithFormat:@"%@",substring] copy];

OR

[subString2 release];
subString2 = [[NSString stringWithFormat:@"%@",substring] retain];