可以说我有两个对象:
如果我输入以下查询:“ Big Blue Bike”,我应该收到对象Big Bike。
我正在尝试从 public static int getScreenWidth(Context c) {
WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
Display display = null;
if (wm != null) {
display = wm.getDefaultDisplay();
}
Point size = new Point();
if (display != null) {
display.getSize(size);
}
return size.x;
}
public static int getScreenHeight(Context c) {
WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
Display display = null;
if (wm != null) {
display = wm.getDefaultDisplay();
}
Point size = new Point();
if (display != null) {
display.getSize(size);
}
return size.y;
}
函数中找出实现方法。
目前,我正在按照以下方式使用某些东西:
NSPredicate
但是,由于“ Blue”不在对象中,因此该谓词将从已过滤的数组中消除所有对象。
是否有一种let typePredicate = NSPredicate(format: "type CONTAINS[c] %@", text);
格式可以让我考虑这些无关的声明,并且仍然从“蓝色巨人自行车”返回“ Big Bike”?
答案 0 :(得分:0)
我仅举一个简单的示例如何实现此目的,这只是一个示例,只是为了提供一些想法,以便您可以继续进行下去,假设我们将创建一个名为Vehicle
的类,稍后我们将创建2您刚才提到的其他类BigBike
和SmallBike
,然后将介绍如何使用查询字符串来解析该类
让我们创建一个名为Vehicle
的类,该类包含所有信息,如果您在数组中有单个类型的对象,这将非常有用。
import Foundation
enum Size {
case big
case small
}
enum Type {
case Bike
case Car
}
class Vehicle:NSObject {
let size:Size
let type:Type
init(with vehicleType:Type, andSize vehicleSize:Size) {
self.size = vehicleSize
self.type = vehicleType
}
override func value(forKey key: String) -> Any? {
if key == "size" {
if self.size == .big {
return "big"
} else if self.size == .small {
return "small"
}
} else if key == "type" {
if self.type == .Bike {
return "bike"
} else if self.type == .Car {
return "car"
}
}
return nil
}
func vehicleDescription() {
let aType = (type == .Bike) ? "Bike" : "Car"
let aSize = (size == .big) ? "Big" : "Small"
print("this vehicle is \(aType) of size \(aSize)")
}
}
现在我们将创建您在问题BigBike
和SmallBike
中提到的两个对象,如下所示,它们只是Vehicle
类的子类
import Foundation
class BigBike:Vehicle {
}
和另外一个SmallBike
import Foundation
class SmallBike:Vehicle {
}
现在您有2个类BigBike
和SmallBike
现在可以过滤从这2个类创建的对象,
您有包含这2个类的对象的数组,例如,让一个数组并附加BigBike
和SmallBike
的对象,如下所示,
var vehicles:NSMutableArray = NSMutableArray()
//add some objects for above array like below for testing:
func setupVehicleArray() {
for i in 0..<15 {
if i < 5 {
let vehicle = BigBike(with: .Bike, andSize: .big)
vehicles.add(vehicle)//append(vehicle)
}
if i > 5 && i < 10 {
let vehicle = BigBike(with: .Bike, andSize: .small)
vehicles.add(vehicle)
}
if i > 11 && i < 15 {
let vehicle = BigBike(with: .Bike, andSize: .big)
vehicles.add(vehicle)
}
}
}
现在vehicles
包含BigBike
和SmallBike
的对象,现在让使用谓词进行查询和过滤这些对象
func makeQuery() {
let query = "big blue bike"
let aPredicate = NSPredicate { (obj:Any?, test:[String : Any]?) -> Bool in
var checkFurther = false
if let aObj = obj as? Vehicle {
if let size = aObj.value(forKey: "size") as? String {
if query.contains(size) {
checkFurther = true
}
}
if let type = aObj.value(forKey: "type") as? String {
if query.contains(type) {
if checkFurther {
return true
}
}
}
}
return false
}
let filteredArray = vehicles.filtered(using: aPredicate)
for aVehicle:Vehicle in filteredArray as! [Vehicle] {
aVehicle.vehicleDescription()
}
}
这只是您可以尝试的示例示例,还为其他查询提供了值,您可以根据需要修改谓词。
上面给出的结果如下:
this vehicle is Bike of size Big
this vehicle is Bike of size Big
this vehicle is Bike of size Big
this vehicle is Bike of size Big
this vehicle is Bike of size Big
this vehicle is Bike of size Big
this vehicle is Bike of size Big
this vehicle is Bike of size Big
如果您将查询字符串更改为small green bike
,则会得到如下结果
this vehicle is Bike of size Small
this vehicle is Bike of size Small
this vehicle is Bike of size Small
this vehicle is Bike of size Small
希望这会有所帮助..:)