2015-05-07 120 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