在 objective c 中订购 Array/Dictionary
Order an Array/Dictionary in objective c
我正在尝试一个获取当前位置坐标的小项目,然后根据当前位置找到附近的咖啡店并将它们显示在 UITableView 中。到目前为止,我已经设法用信息加载 JSON 脚本,将其解析为字典,并将其显示在 UITableView 中。我现在要做的是尝试按 'distance' 或 'name'(按字母顺序)对信息进行排序。我怎样才能做到这一点?这是一些代码来展示我到目前为止所做的事情。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
if (!self.didFindLocation) {
self.didFindLocation = YES;
[self.locationManager stopUpdatingLocation];
}
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
CLLocation *crnLoc = [locations lastObject];
NSString *latitudeString = [NSString stringWithFormat:@"%.10f",crnLoc.coordinate.latitude];
NSString *longitudeString = [NSString stringWithFormat:@"%.10f",crnLoc.coordinate.longitude];
NSString *urlString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?client_id=ACAO2JPKM1MXHQJCK45IIFKRFR2ZVL0QASMCBCG5NPJQWF2G&client_secret=YZCKUYJ1WHUV2QICBXUBEILZI1DMPUIDP5SHV043O04FKBHL&ll=%@,%@&query=coffee&v=20140806&m=foursquare", latitudeString, longitudeString];
NSURL *url = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *responseDictionary = [dataDictionary objectForKey:@"response"];
NSDictionary *venueDictionary = [responseDictionary objectForKey:@"venues"];
for (NSDictionary *dict in venueDictionary) {
NSString *name = [dict objectForKey:@"name"];
NSDictionary *locationDictionary = [dict objectForKey:@"location"];
NSString *address = [NSString stringWithFormat: @"%@", [locationDictionary objectForKey:@"address"]];
if (address == nil) {
address = @"No address provided";
}
NSString *distance = [NSString stringWithFormat: @"%@", [locationDictionary objectForKey:@"distance"]];
if (distance == nil) {
distance = @"No coordinates provided";
}
NSLog(@"Name: %@ Address: %@ distance: %@", name, address, distance);
CoffeeStore *store = [[CoffeeStore alloc] initWithName:name address:address distance:distance];
[resultArray addObject:store];
}
self.storeArray = resultArray;
[self.tableView reloadData]; }
和
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CoffeeStore *selectedStore = self.storeArray[indexPath.row];
cell.textLabel.text = selectedStore.name;
cell.detailTextLabel.text = selectedStore.distanceString;
return cell;
}
您的 resultArray
包含一个 CoffeeStore
对象数组,这些对象的 init 方法有一个 distance
。在调用 reloaddata
之前,您需要根据距离对数组进行排序。希望这些对象有 distance
属性 并且您可以按 属性 排序,如下所示:
NSSortDescriptor *distanceDesc = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
self.storeArray = [resultArray sortedArrayUsingDescriptors:@[distanceDesc]];
我正在尝试一个获取当前位置坐标的小项目,然后根据当前位置找到附近的咖啡店并将它们显示在 UITableView 中。到目前为止,我已经设法用信息加载 JSON 脚本,将其解析为字典,并将其显示在 UITableView 中。我现在要做的是尝试按 'distance' 或 'name'(按字母顺序)对信息进行排序。我怎样才能做到这一点?这是一些代码来展示我到目前为止所做的事情。
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
if (!self.didFindLocation) {
self.didFindLocation = YES;
[self.locationManager stopUpdatingLocation];
}
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
CLLocation *crnLoc = [locations lastObject];
NSString *latitudeString = [NSString stringWithFormat:@"%.10f",crnLoc.coordinate.latitude];
NSString *longitudeString = [NSString stringWithFormat:@"%.10f",crnLoc.coordinate.longitude];
NSString *urlString = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?client_id=ACAO2JPKM1MXHQJCK45IIFKRFR2ZVL0QASMCBCG5NPJQWF2G&client_secret=YZCKUYJ1WHUV2QICBXUBEILZI1DMPUIDP5SHV043O04FKBHL&ll=%@,%@&query=coffee&v=20140806&m=foursquare", latitudeString, longitudeString];
NSURL *url = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *responseDictionary = [dataDictionary objectForKey:@"response"];
NSDictionary *venueDictionary = [responseDictionary objectForKey:@"venues"];
for (NSDictionary *dict in venueDictionary) {
NSString *name = [dict objectForKey:@"name"];
NSDictionary *locationDictionary = [dict objectForKey:@"location"];
NSString *address = [NSString stringWithFormat: @"%@", [locationDictionary objectForKey:@"address"]];
if (address == nil) {
address = @"No address provided";
}
NSString *distance = [NSString stringWithFormat: @"%@", [locationDictionary objectForKey:@"distance"]];
if (distance == nil) {
distance = @"No coordinates provided";
}
NSLog(@"Name: %@ Address: %@ distance: %@", name, address, distance);
CoffeeStore *store = [[CoffeeStore alloc] initWithName:name address:address distance:distance];
[resultArray addObject:store];
}
self.storeArray = resultArray;
[self.tableView reloadData]; }
和
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
CoffeeStore *selectedStore = self.storeArray[indexPath.row];
cell.textLabel.text = selectedStore.name;
cell.detailTextLabel.text = selectedStore.distanceString;
return cell;
}
您的 resultArray
包含一个 CoffeeStore
对象数组,这些对象的 init 方法有一个 distance
。在调用 reloaddata
之前,您需要根据距离对数组进行排序。希望这些对象有 distance
属性 并且您可以按 属性 排序,如下所示:
NSSortDescriptor *distanceDesc = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
self.storeArray = [resultArray sortedArrayUsingDescriptors:@[distanceDesc]];