0

i am newbie in iOS development, i want to add autocomplete textfield in my ap i write a code for that like as

- (void)viewDidLoad
{
 [self get data];
}
-(void)getdata
{
NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 1000;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)      {
    if (!error) {
        [allObjects addObjectsFromArray:objects];
        if (objects.count == limit) {

            skip += limit;
            [query setSkip: skip];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                [allObjects addObjectsFromArray:objects];
                self.qpinname=[allObjects valueForKey:@"GPIN"];
                self.locationarray=[allObjects valueForKey:@"Location"];
                self.latitude=[self.locationarray valueForKey:@"lat"];
                self.longitude=[self.locationarray valueForKey:@"lng"];
                self.address=[allObjects valueForKey:@"Address"];
                NSLog(@"Address %@",self.address);
                self.usernameArray=[allObjects valueForKey:@"AddedBy"];
            }];
        }
    }
    else
    {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];
}

then i got my data array and i want to show it on my table view like as

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
[self.autoaddress removeAllObjects];
for(NSString *curString in self.address)
{
    NSRange substringRange = [curString rangeOfString:substring];
    if (substringRange.location == 0) {
        [self.autoaddress addObject:curString];
    }
}
[_autocompleteTableView reloadData];
}
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
_autocompleteTableView.hidden = NO;

NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section
{
return self.autoaddress.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] ;
}

cell.textLabel.text = [self.autoaddress objectAtIndex:indexPath.row];
return cell;
}

then i got error in code

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
[self.autoaddress removeAllObjects];
for(NSString *curString in self.address)
{
    NSRange substringRange = [curString rangeOfString:substring];
    if (substringRange.location == 0) {
        [self.autoaddress addObject:curString];
    }
}
[_autocompleteTableView reloadData];
}

Here i got error in line NSRange substringRange = [curString rangeOfString:substring]; -[NSNull rangeOfString:]: unrecognized selector sent to instance i find it in google but i not get solution please help me for this thanks.

Ramesh
  • 177
  • 1
  • 3
  • 18

1 Answers1

0

Your self.address property is probably NSNull or it contains NSNull if it is an array. You can check if object is NSNull the following way:

if(![object isEqual:[NSNull null]])
{
    //do something if object is not equals to [NSNull null]
}

The code is from this answer.

EDIT: If you use NSMutableArray you can call the method [self.address removeObjectIdenticalTo:[NSNull null]] to remove all NSNull objects from the array after self.address=[allObjects valueForKey:@"Address"];

Community
  • 1
  • 1
Georgi
  • 674
  • 7
  • 21