Using Predicate in Swift

后端 未结 8 915
陌清茗
陌清茗 2020-12-07 12:16

I\'m working through the tutorial here (learning Swift) for my first app: http://www.appcoda.com/search-bar-tutorial-ios7/

I\'m stuck on this part (Objective-C code)

8条回答
  •  鱼传尺愫
    2020-12-07 12:32

    You can use filters available in swift to filter content from an array instead of using a predicate like in Objective-C.

    An example in Swift 4.0 is as follows:

    var stringArray = ["foundation","coredata","coregraphics"]
    stringArray = stringArray.filter { $0.contains("core") }
    

    In the above example, since each element in the array is a string you can use the contains method to filter the array.

    If the array contains custom objects, then the properties of that object can be used to filter the elements similarly.

提交回复
热议问题