我正在尝试在发送产品ID列表后对从Apple Server收到的SKProduct数组进行排序。
我想通过使用以下内容进行排序:
NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"self.price" ascending:YES];
NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arrayWithObject:lowestPriceToHighest]];
但是我收到了错误消息:Unable to access variable "lowestPriceToHighest"
我的排序描述符是否被错误定义?
答案 0 :(得分:7)
尝试删除“自我”
NSSortDescriptor *lowestPriceToHighest = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];
NSArray *sortedProducts = [products sortedArrayUsingDescriptors:[NSArray arrayWithObject:lowestPriceToHighest]];
答案 1 :(得分:4)
假设你有var products: [SKProduct]
,你可以这样做:
products.sortInPlace {
return Float($0.price) < Float($1.price)
}
答案 2 :(得分:1)
products.sort(by: { (p0, p1) -> Bool in
return p0.price.floatValue < p1.price.floatValue
})
答案 3 :(得分:0)
尝试:
NSArray *products = [response.products sortedArrayUsingComparator:^(id a, id b) {
NSDecimalNumber *first = [(SKProduct*)a price];
NSDecimalNumber *second = [(SKProduct*)b price];
return [first compare:second];
}];
从这个答案:
答案 4 :(得分:0)
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"_strPrice"
ascending:sortFlag selector:@selector(localizedStandardCompare:)] ;
尝试使用此选择器