SwiftDeveloper
SwiftDeveloper

Reputation: 7368

Objc live Json Parsing Only shows 1 item error

i have working objc tableview parsing codes with live entry. But shows only matched 1 row only. I want to show all matched rows. My working codes here.

My working remote json file

{
   "company" : [
        {
         "description" : "example company , Jordan",
         "id" : "90",
         "place_id" : "90"
      } {
         "description" : "example company 2 , Qatar",
         "id" : "362578",
         "place_id" : "362578"
      } {
         "description" : "example company 3 , Spain",
         "id" : "432589",
         "place_id" : "432589"
      },    
    {
         "description" : " ",
         "id" : "1",      
         "place_id" : "1"
      }
   ],
   "status" : "OK"
}

Objc codes here

#import "ViewController.h"
#import "Common/Constants.h"
#import "Place.h"

@interface ViewController (){
    NSMutableArray *response;
    Place *place;
    NSMutableArray *places;
}
@end


NSString *links;

@implementation ViewController
@synthesize toggleSwitch;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    [self.txtSearchField becomeFirstResponder];
    self.tableViewSearchResult.hidden = YES;

     self.toggleSwitch.on = true;


        self.switchLabel.text = @"Coms";

        NSString *valueToSave = @"http://bla.com/company.php?";
        [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
        [[NSUserDefaults standardUserDefaults] synchronize];


}




- (IBAction)backChange:(id)sender {
    if ([sender isOn] == YES) {
        _switchLabel.text = @"Firmalar";



        NSString *valueToSave = @"http://bla.com/company.php?";
        [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
        [[NSUserDefaults standardUserDefaults] synchronize];


        NSLog(@"");


    } else {


        NSString *valueToSave = @"http://bla.com/company2.php?";
        [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
        [[NSUserDefaults standardUserDefaults] synchronize];


  _switchLabel.text = @"Ülkeler";


        NSLog(@"");


          }






}





#pragma mark - 
-(void)searchForResult:(NSString *)input{


    NSString *savedValue = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"preferenceName"];


     NSString *urlString = [NSString stringWithFormat:@"%@gelen=%@",savedValue,input];

      NSLog(@"link = %@",urlString);

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];


}

#pragma mark - UITextField Delgate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if (![string isEqualToString:@""] && [string length]>0) {
        NSString *keyword = [textField.text stringByAppendingString:string];
        NSLog(@"String : %@",keyword);
        if ([keyword length]>=4) {
            [self searchForResult:keyword];
        }
    }
    return YES;
}
#pragma mark - UITableViewDelegate 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    place = [places objectAtIndex:indexPath.row];
    self.txtSearchField.text = place.placeName;
    NSLog(@"Selected placeID : %@",place.placeID);
    self.tableViewSearchResult.hidden = YES;
}

#pragma mark - UITableViewDataSource

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"cell"];
    }
    place = [places objectAtIndex:indexPath.row];
    NSLog(@"Place ID : %@, Place Name : %@",place.placeID,place.placeName);
    cell.textLabel.text = [place placeName];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [places count];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

When matched 1 more gives me this error (if matched 1 success showing)

Connection Error : (null)

I working about 7-8 hours but i don't resolve it :) I need your help thanks for everything. I think this is easy problem but still i don't resolve it.

Upvotes: 2

Views: 42

Answers (1)

PK20
PK20

Reputation: 1066

your json string should be like this

{
   "company" : [
        {
         "description" : "example company , Jordan",
         "id" : "90",
         "place_id" : "90"
      }, {
         "description" : "example company 2 , Qatar",
         "id" : "362578",
         "place_id" : "362578"
      }, {
         "description" : "example company 3 , Spain",
         "id" : "432589",
         "place_id" : "432589"
      },    
    {
         "description" : " ",
         "id" : "1",      
         "place_id" : "1"
      }
   ],
   "status" : "OK"
}

to satisfy your code and get more than 1 rec.

It is a valid josn as per RFC 4627 (JSON specfication). Hence you got 1 response.

Upvotes: 2

Related Questions