This is my array
@property (nonatomic, strong) NSMutableArray *searchResults;
I initialized it in viewDidLoad function. I want to remove all objects from this array when current input in search bar is changed and add populate it using new elements. But when I do
[self.searchResults removeAllObjects];
It won't add new elements. same goes with returning an array to self.searchResults
But when I don't remove elements from an array and append elements, it adds elements with no problem. I'm really having a hard time figuring out what's wrong.
viewDidLoad func
- (void) viewDidLoad {
[super viewDidLoad];
self.searchBar.delegate = self;
self.searchBar.showsCancelButton = YES;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.searchResults = [[NSMutableArray alloc] init];
[self searchHandler:self.searchBar];
}
here is adding new elements.
- (NSMutableArray *)getProductList: (NSString *)text withArray: (NSMutableArray *) arrayResult{
[self.searchResults removeAllObjects];
[manager POST:url parameters:parameter
success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(@"Length: %lu", (unsigned long)[responseObject count]);
int length = [responseObject count];
NSString *key;
for (int i=0; i<length; i++) {
key = [NSString stringWithFormat:@"%d", i];
[self.searchResults addObject:responseObject[key]];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
checking the array
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
NSLog(@"changed text: %@", searchText);
//[searchResults removeAllObjects];
self.searchResults = [self getProductList:searchText withArray:self.searchResults];
NSLog(@"Length of current array: %lu", (unsigned long)[self.searchResults count]);
for (NSString *item in self.searchResults) {
NSLog(@"%@", item);
}
[self.tableView reloadData];
}