我有一系列餐馆,这些餐馆是名称,类型,位置等的对象。我如何只搜索餐馆名称,或者只搜索不搜索整个数组对象的位置?
例如我的数组有2个餐馆对象。我只想搜索Name是否包含“United”。如果我搜索整个对象,则会因为国家而显示。
[<Restaurant: 0x7fcb0ad80950, objectId: LA74J92QDA, localId: (null)> {
City = "New York";
Country = "United States";
Name = United Bakery;
RestaurantLoc = "<PFGeoPoint: 0x7fcb0af6cbc0, latitude: 40.759212, longitude: -73.984632>";
State = NY;
Street = "1 main st.";
Zipcode = 10055;
}, <Restaurant: 0x7fcb0af6cfc0, objectId: 0aKFrpKN46, localId: (null)> {
City = "New York";
Country = "United States";
Name = Wendys Bakery;
RestaurantLoc = "<PFGeoPoint: 0x7fcb0af6cd60, latitude: 40.759210, longitude: -73.984631>";
State = NY;
Street = "1 main st.";
Zipcode = 10055;
}]
这是创建对象数组的块:
let userGeoPoint = currentLocGeoPoint
var query = PFQuery(className:"Restaurant")
query.whereKey("RestaurantLoc", nearGeoPoint:userGeoPoint, withinMiles:50)
query.limit = 2
query.findObjectsInBackgroundWithBlock {
(object: [AnyObject]!, error: NSError!) -> Void in
if object != nil {
println(object)
} else {
println("The getObject request failed with error: \(error)")
}
}
答案 0 :(得分:1)
您可以使用filteredArrayUsingPredicate
。这是一个小例子:
NSString *searchString = @"a";
NSArray *filteredProducts = [products filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@", searchString]];
夫特:
let searchString = "text_to_search"
let predicate = NSPredicate(format: "title CONTAINS[cd] %@", searchString);
let filteredArray = array.filteredArrayUsingPredicate(predicate!);
答案 1 :(得分:0)
尝试使用filter(_ :)函数。来自Apple Developer:
Returns an array containing the elements of the array for which a provided closure indicates a match.
Use this method to return a new array by filtering an existing array. The closure that you supply for includeElement: should return a Boolean value to indicate whether an element should be included (true) or excluded (false) from the final collection