【问题标题】:Search Within Array Object in Swift在 Swift 中的数组对象内搜索
【发布时间】:2015-02-26 09:29:26
【问题描述】:

我有一个餐馆数组,它们是具有名称、类型、位置等的对象。如何只搜索一个餐馆名称,或者只搜索一个位置而不搜索整个数组对象?

例如我的数组有 2 个 Restaurant 对象。我只想搜索名称是否包含“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)")
                }
            }

【问题讨论】:

    标签: ios arrays search swift


    【解决方案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!);
    

    【讨论】:

    • 他在 swift 中说不是在 Objective-C 中
    • @Leena 在swift中使用filteredArrayUsingPredicate有什么问题?
    • @VitaliyGozhenko 我的意思是说这个问题与swift有关,所以答案应该是swift而不是Objective-C......
    • @VitaliyGozhenko 谢谢你的例子。我尝试使用filteredArrayUsingPredicate,但在swift中遇到了这个错误'[AnyObject]' does not have a member named 'filteredArrayUsingPredicate'
    • @Rachel 我认为这是因为您尝试 filteredArrayUsingPredicate 的对象不是 NSArray 对象,而是另一个对象...
    【解决方案2】:

    尝试使用 filter(_:) 函数。来自 Apple 开发者:

    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
    

    来源:https://developer.apple.com/library/ios/documentation/General/Reference/SwiftStandardLibraryReference/Array.html

    【讨论】:

    • 谢谢,我通读了。但是过滤功能是否可以用于查找“名称”是否包含“xxx”或“yyy”?你介意提供一个简短的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 2018-07-05
    • 2017-05-21
    • 2011-04-07
    相关资源
    最近更新 更多