Objective c 数组重叠不工作

Objective c 数组重叠不工作,objective-c,arrays,json,uitableview,Objective C,Arrays,Json,Uitableview,Xcode问题。我有一个nsmutablearray,它正在提取json对象。示例公司:名称、州、地址、电话号码等 我拉的信息只是罚款。我想在表视图中显示每个状态的1个。它工作正常,但显示相同状态的多个气泡。但我只想展示每个州的一个。我正在使用一些代码,但它不返回任何状态。我已经看到了很多例子,这应该是有效的,但它没有返回任何结果。如果我跳过下面的代码,它会显示所有状态。我还尝试了for循环。并尝试将数组设置为NSSet。什么都没用。有什么想法吗??? 我的问题是这个代码 //Creat

Xcode问题。我有一个nsmutablearray,它正在提取json对象。示例公司:名称、州、地址、电话号码等

我拉的信息只是罚款。我想在表视图中显示每个状态的1个。它工作正常,但显示相同状态的多个气泡。但我只想展示每个州的一个。我正在使用一些代码,但它不返回任何状态。我已经看到了很多例子,这应该是有效的,但它没有返回任何结果。如果我跳过下面的代码,它会显示所有状态。我还尝试了for循环。并尝试将数组设置为NSSet。什么都没用。有什么想法吗??? 我的问题是这个代码

    //Create states only array...remove duplicate states here
NSArray *statesArray = [companies valueForKeyPath:@"@distinctUnionOfObjects.state"];

return statesArray;
这是我的全部代码。请帮助我解决这个问题已经有一周了

@interface StatesTableViewController ()

@property (nonatomic) NSString *name;
@property (nonatomic) NSString *state;

@end

@implementation StatesTableViewController

NSArray *companies;
NSArray *statesArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *address = @"http://www.companiesFeed";
    NSURL *url = [[NSURL alloc] initWithString:address];

    //laod the data on a background queue..
    //if we were connecting to a an online url then we need it
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        companies = [self readCompanies:url];//, statesSet = [self readCompanies:url];

        //now that we have the data, reload the table data on the main ui thread
        [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
    });
}

//new code
- (NSArray *)readCompanies:(NSURL *)url {
    //create a nsurlrequest with the given Url
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:
                             NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0];

    //get the data
    NSURLResponse *response;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

    //now create a nsdictionary from the json data
    NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data
                                                                   options:0 error:nil];

    //create a new array to hold the comanies
    NSMutableArray *companies = [[NSMutableArray alloc] init];

    //get an array of dictionaries with the key "company"
    NSArray *array = [jsonDictionary objectForKey:@"companies"];

    //iterate throught the array of dictionaries
    for (NSDictionary *dict in array) {
        //create a new company object with information in the dictionary
        Company *company = [[Company alloc] initWithJSONDictionary:dict];

        //add the Company object to the array
        [companies addObject:company ];

    }


    //Create states only array...remove duplicate states here
    NSArray *statesArray = [companies valueForKeyPath:@"@distinctUnionOfObjects.state"];

    return statesArray;
     //return companies;
}

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

#pragma mark -table view controller methods
//change uniqueValue errors to companies

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {    
    //return[companies count];
    return [statesArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"CellIDState";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil){
        //single line on table view
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        // dual line on table view
        cell = [[UITableViewCell alloc]                       initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }

    //edit single state
    //Company *company = [companies objectAtIndex:indexPath.row];
    Company *company = [statesArray objectAtIndex:indexPath.row];

    //cell.textLabel.text = company.company_id;
    cell.textLabel.text = company.state;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",company.companyName];
    //adds cheveron to tableviewl
    [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];

    return cell;
}

#pragma mark - navigation
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

}

@end

我能够通过使用谓词和块来检查一组现有状态来获得唯一的状态列表。像这样:

NSMutableArray *statesArray = [NSMutableArray array];
    [companies filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        Company *dict = (Company *)evaluatedObject;
        BOOL seen = [statesArray containsObject:dict];
        if (!seen) {
            [statesArray addObject:dict];
        }
        return !seen;
    }]];
    NSLog(@"unique states:  %@", statesArray);
其中公司阵列如下所示:

{[{
  "company_id": "22",
  "entityformSubmissionID": "22",
  "companyName": "house of waffles",
  "companySlogan": " where your home is!",
  "address1": "123 here",
  "address2": "thre",
  "city": "everywhere",
  "state": "CA",
  "zipCode": "94531",
  "phoneNumber": "777-777-7777",
  "email": "test@test.com",
  "additionalCompanyInfo": "all additional company info is going here",
  "sales": "There are no sales!",
  "rentalTerms": "Terms is dont break our stuff jerks!",
  "companyLogo": "/16x16icon.png"
}]

在您尝试获取keypath之前,companys数组结构是什么样子的我已经尝试了日志,但没有得到任何输出。可能是放错了地方。如果我的过滤器只针对一个州,我会得到这个..2015-05-13 15:30:04.232公司列表[3574:100861]数组:CA这是正确的。只有一个CA。表视图上没有显示任何内容。我收到一个Xcode错误..StatesTableViewController.m:81:17:在类型为“NSMutableArray*”的对象上找不到读取字典元素的预期方法,这可能是tableview控制器的问题吗?代码81是[NSArray*Companys[@Companys]filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOLid evaluatedObject,确定代码现在没有错误。但表视图上仍然没有结果。2015-05-13 16:46:10.895公司列表[3978:116117]唯一状态:,