2015-05-07 81 views
0

确定新对象c。 我有我的应用程序去一个网站,并拉动公司的数据。公司名称地址等。我想显示每个公司的状态。但我只想要显示每种状态中的一种。例如,如果我有CA,CA,CA,AZ,AZ,AZ,NY。我只想在我的tableview CA,AZ,NY上展示。我尝试了distinctUnionOfObjects,但我认为我错误地使用了它。任何帮助?删除对象数组中的重复对象

#import "StatesTableViewController.h" 

@interface StatesTableViewController() 

@end 

@implementation StatesTableViewController 

NSArray *companies; 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSString *address = @"http://www.Feed"; 
    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]; 

     //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]; 


    } 


    //return the array of Company objects 
    return companies; 







} 
//trying to get 1 state here? should i create a new array? 
//added code to show 1 of each state. companies array now uniquevalues 
//NSArray* uniqueValues = [companies valueForKeyPath:[NSString stringWithFormat:@"distinctUnionOfObjects.%@",@"state"]]; 

//or 
//static NSArray *uniqueValues = nil; 
//if (uniqueValues == nil){ 
// uniqueValues = [NSArray arrayWithArray:[companies valueForKeyPath:[NSString stringWithFormat:@"distinctUnionOfObjects.%@",@"state"]]]; 
//} 
//or 

//companies = [companies valueForKeyPath:@"@distinctUnionOfObjects.state"]; 

//end added code 



- (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 [uniqueValues count]; 
    return [companies 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]; 
    } 

    //Company *company = [uniqueValues objectAtIndex:indexPath.row]; 
    Company *company = [companies 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 
+0

我试过NSordered集。但是我得到这个错误。初始化器元素不是编译时常量。是否应该在我退回公司阵列之前或之后添加? – cmdace

回答

1

假设你有一个Company对象具有以下接口:

@interface Company : NSObject 
@property (nonatomic) NSString *name; 
@property (nonatomic) NSString *state; 
@end 

接下来,让我们说你做到以下几点:

// Creating & adding a few companies 
Company *company1 = [Company new]; 
company1.name = @"Some Company"; 
company1.state = @"CA"; 

Company *company2 = [Company new]; 
company2.name = @"Some Company"; 
company2.state = @"CA"; 

Company *company3 = [Company new]; 
company3.name = @"Some Company"; 
company3.state = @"CA"; 

Company *company4 = [Company new]; 
company4.name = @"Some Company"; 
company4.state = @"AZ"; 

Company *company5 = [Company new]; 
company5.name = @"Some Company"; 
company5.state = @"AZ"; 

self.companies = @[company1, company2, company3, company4, company5]; 

NSArray *uniqueStates = [self.companies valueForKeyPath:@"@distinctUnionOfObjects.state"]; 
NSSet *uniqueStatesSet = [NSSet setWithArray:[self.companies valueForKey:@"state"]]; 

uniqueStates阵列& uniqueStatesSet集都将包含两个对象,@"CA"@"AZ"(获取唯一集对象的两种方法) 。

+0

它从一个网站的网址提取公司数据。所以我不需要知道如何添加公司。只是如何过滤现有的数组。 – cmdace

+0

我只包含这些行,所以你会明白我是一个公司对象的实例。只是跳过那部分,并利用其余部分。 – Rufel

+0

我得到这个错误:使用未声明的标识符'自我'。任何想法 – cmdace

1
NSArray *companies = …; 

NSOrderedSet *states = [NSOrderedSet orderedSetWithArray:[companies valueForKey:@"state"]]; 

如果您有唯一项目的有序列表,则应使用有序集合来对其进行建模。

+0

好的我从Amin试过了上面的。我在阵列公司归来之后插入了它。我得到一个错误,这个Initializer元素不是编译时常量 – cmdace

+0

你能显示确切的代码吗? –

+0

确切代码如上。因此,最重要的部分是去网址,并拉动公司的信息。底部是获取状态并将其推送到表视图。我不知道如何或将要应用代码来仅显示每个状态之一。 – cmdace