2014-12-30 3 views
0

이름, 유형, 위치 등이있는 객체의 레스토랑이 있습니다. 전체 배열 객체를 검색하지 않고 레스토랑 이름이나 위치 만 검색하려면 어떻게해야합니까?스위프트의 배열 객체 내 검색

예를 들어 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)") 
       } 
      } 

답변

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!); 
+0

그는 신속하게 Objective - C – Leena

+0

@ Leena에서 신속하게 'filteredArrayUsingPredicate'를 사용하는 데 어떤 문제가 있습니까? –

+1

@VitaliyGozhenko 질문이 신속하고 객관적인 대답 대신 신속하게 응답해야한다고 말하고자합니다 ...... – Leena

0

시도 필터 (_^함수를 사용한다. 애플 개발자에서 :

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

+0

덕분에, 내가 그것을 읽어 여기에 작은 예입니다. 그러나 "이름"에 "xxx"또는 "yyy"가 포함되어 있는지 찾기 위해 필터 기능을 사용할 수 있습니까? 짧은 예제를 제공해 주시겠습니까? – Onichan