This question already has an answer here:
这个问题在这里已有答案:
- removing duplicates from array in objective c 8 answers
从目标c 8答案中删除数组中的重复项
ok new to objective c. I have my app going to a web site and pulling company data. Company name address etc. I want to display the state from each company. But I only want one of each state shown. For example if I have CA,CA,CA,AZ,AZ,AZ,NY. I only want to display on my tableview CA,AZ,NY. I am have tried distinctUnionOfObjects, but I think I am using it wrong. any help?
对目标c不熟悉。我让我的应用程序访问网站并提取公司数据。公司名称地址等我想显示各公司的状态。但我只想要显示每个州中的一个。例如,如果我有CA,CA,CA,AZ,AZ,AZ,NY。我只想在我的桌面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
2 个解决方案
#1
Suppose you have a Company
object with the following interface:
假设您有一个具有以下界面的Company对象:
@interface Company : NSObject
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *state;
@end
Next, let's say you do the following:
接下来,假设您执行以下操作:
// 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"]];
The uniqueStates
array & uniqueStatesSet
set will both contain two objects, @"CA"
and @"AZ"
(two ways of getting a unique set objects).
uniqueStates数组和uniqueStatesSet集都包含两个对象,@“CA”和@“AZ”(两种获取唯一集合对象的方法)。
#2
NSArray *companies = …;
NSOrderedSet *states = [NSOrderedSet orderedSetWithArray:[companies valueForKey:@"state"]];
If you have an ordered list of unique items, you should use an ordered set to model it.
如果您有唯一项目的有序列表,则应使用有序集对其进行建模。
#1
Suppose you have a Company
object with the following interface:
假设您有一个具有以下界面的Company对象:
@interface Company : NSObject
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *state;
@end
Next, let's say you do the following:
接下来,假设您执行以下操作:
// 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"]];
The uniqueStates
array & uniqueStatesSet
set will both contain two objects, @"CA"
and @"AZ"
(two ways of getting a unique set objects).
uniqueStates数组和uniqueStatesSet集都包含两个对象,@“CA”和@“AZ”(两种获取唯一集合对象的方法)。
#2
NSArray *companies = …;
NSOrderedSet *states = [NSOrderedSet orderedSetWithArray:[companies valueForKey:@"state"]];
If you have an ordered list of unique items, you should use an ordered set to model it.
如果您有唯一项目的有序列表,则应使用有序集对其进行建模。