0

我正在为 textField 做自动完成功能的概念,这样做我收到以下错误:“cfstring length message sent to 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;

我哪里出错了。不明白它在哪里被释放了..

4

1 回答 1

2

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

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

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

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

OR

[subString2 release];
subString2 = [[NSString stringWithFormat:@"%@",substring] retain];
于 2012-12-11T10:41:45.967 回答