ios 在 Swift 中使用谓词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24176605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:50:49  来源:igfitidea点击:

Using Predicate in Swift

iosobjective-cswiftios7predicate

提问by levitatejay

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

我正在为我的第一个应用程序完成教程(学习 Swift):http: //www.appcoda.com/search-bar-tutorial-ios7/

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

我被困在这部分(Objective-C 代码):

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c]         %@", searchText];
    searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}

Can anyone advise how to create an equivalent for NSPredicate in Swift?

谁能建议如何在 Swift 中为 NSPredicate 创建等效项?

回答by Chuck

This is really just a syntax switch. OK, so we have this method call:

这实际上只是一个语法切换。好的,所以我们有这个方法调用:

[NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];

In Swift, constructors skip the "blahWith…" part and just use the class name as a function and then go straight to the arguments, so [NSPredicate predicateWithFormat: …]would become NSPredicate(format: …). (For another example, [NSArray arrayWithObject: …]would become NSArray(object: …). This is a regular pattern in Swift.)

在 Swift 中,构造函数会跳过“blahWith...”部分,只使用类名作为函数,然后直接进入参数,所以[NSPredicate predicateWithFormat: …]会变成NSPredicate(format: …). (再举个例子,[NSArray arrayWithObject: …]会变成NSArray(object: …)。这是 Swift 中的常规模式。)

So now we just need to pass the arguments to the constructor. In Objective-C, NSString literals look like @"", but in Swift we just use quotation marks for strings. So that gives us:

所以现在我们只需要将参数传递给构造函数。在 Objective-C 中,NSString 文字看起来像@"",但在 Swift 中我们只对字符串使用引号。所以这给了我们:

let resultPredicate = NSPredicate(format: "name contains[c] %@", searchText)

And in fact that is exactly what we need here.

事实上,这正是我们在这里所需要的。

(Incidentally, you'll notice some of the other answers instead use a format string like "name contains[c] \(searchText)". That is not correct. That uses string interpolation, which is different from predicate formatting and will generally not work for this.)

(顺便说一句,您会注意到其他一些答案使用的是格式字符串,如"name contains[c] \(searchText)"。这是不正确的。它使用字符串插值,这与谓词格式不同,通常不适用于此。)

回答by Ji?í Zahálka

Working with predicate for pretty long time. Here is my conclusion (SWIFT)

使用谓词已经很长时间了。这是我的结论(SWIFT)

//Customizable! (for me was just important if at least one)
request.fetchLimit = 1


//IF IS EQUAL

//1 OBJECT
request.predicate = NSPredicate(format: "name = %@", txtFieldName.text)

//ARRAY
request.predicate = NSPredicate(format: "name = %@ AND nickName = %@", argumentArray: [name, nickname])


// IF CONTAINS

//1 OBJECT
request.predicate = NSPredicate(format: "name contains[c] %@", txtFieldName.text)

//ARRAY
request.predicate = NSPredicate(format: "name contains[c] %@ AND nickName contains[c] %@", argumentArray: [name, nickname])

回答by gbk

Example how to use in swift 2.0

示例如何在 swift 2.0 中使用

let dataSource = [
    "Domain CheckService",
    "IMEI check",
    "Compliant about service provider",
    "Compliant about TRA",
    "Enquires",
    "Suggestion",
    "SMS Spam",
    "Poor Coverage",
    "Help Salim"
]
let searchString = "Enq"
let predicate = NSPredicate(format: "SELF contains %@", searchString)
let searchDataSource = dataSource.filter { predicate.evaluateWithObject(
var stringArray = ["foundation","coredata","coregraphics"]
stringArray = stringArray.filter { 
 func filterContentForSearchText(searchText:NSString, scopes scope:NSString)
{
    //var searchText = ""

    var resultPredicate : NSPredicate = NSPredicate(format: "name contains[c]\(searchText)", nil)

    //var recipes : NSArray = NSArray()

    var searchResults = recipes.filteredArrayUsingPredicate(resultPredicate)
}
.contains("core") }
) }

You will get (playground)

你会得到(游乐场)

enter image description here

在此处输入图片说明

回答by Gurunath Sripad

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

您可以使用 swift 中可用的过滤器来过滤数组中的内容,而不是像在 Objective-C 中那样使用谓词。

An example in Swift 4.0 is as follows:

Swift 4.0 中的一个例子如下:

func filterContentForSearchText(searchText:NSString, scope:NSString)
{
   searchResults = recipes.filter { name.rangeOfString(searchText) != nil  }
}

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

在上面的示例中,由于数组中的每个元素都是一个字符串,因此您可以使用该contains方法来过滤数组。

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

如果数组包含自定义对象,则该对象的属性可用于类似地过滤元素。

回答by PREMKUMAR

Use The Below code:

使用以下代码:

    let resultPredicate = NSPredicate(format: "SELF.name contains[c] %@", "value")

    if let sortedDta = yourDataArrayName.filtered(using: resultPredicate) as? NSArray {

 //enter code here.

        print(sortedDta)
    }

回答by Harris

I think this would be a better way to do it in Swift:

我认为这将是在 Swift 中做到这一点的更好方法:

func filterContentForSearchText(searchText: String, scope: String) {
    var resultPredicate = NSPredicate(format: "name contains[c]         %@", searchText)
    searchResults = (recipes as NSArray).filteredArrayUsingPredicate(resultPredicate)
}

回答by Hitesh Chauhan

// change "name" and "value" according to your array data.

// 根据您的数组数据更改“名称”和“值”。

// Change "yourDataArrayName" name accroding to your array(NSArray).

// 根据您的数组(NSArray)更改“yourDataArrayName”名称。

func filterContent(forSearchText searchText: String, scope: String) {
        var resultPredicate = NSPredicate(format: "name contains[c]         %@", searchText)
        searchResults = recipes.filtered(using: resultPredicate)
    }

回答by Saranjith

In swift 2.2

在快速 2.2

##代码##

In swift 3.0

在快速 3.0

##代码##