nspredicate

NSPredicate

…衆ロ難τιáo~ 提交于 2019-12-28 10:10:04
转: 点击打开链接 NSPredicate是一个Foundation类,它指定数据被获取或者过滤的方式。它的查询语言就像SQL的WHERE和正则表达式的交叉一样,提供了具有表现力的,自然语言界面来定义一个集合被搜寻的逻辑条件。相比较抽象的谈论它,展示NSPredicate的使用方法更加容易,所以我们来重新审视 NSSortDescriptor 中使用的示例数据集吧: 索引 1 2 3 4 名 Alice Bob Charlie Quentin 姓 Smith Jones Smith Alberts 年龄 24 27 33 31 @interface Person : NSObject @property NSString *firstName; @property NSString *lastName; @property NSNumber *age; @end @implementation Person - (NSString *)description { return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName]; } @end #pragma mark - NSArray *firstNames = @[ @"Alice", @"Bob", @"Charlie", @"Quentin

NSSortDescriptor 的使用

删除回忆录丶 提交于 2019-12-28 10:09:33
NSPredicate是什么? NSPredicate 是预测的意思 但是我们常翻译成谓词 它可以干什么? 使用NSPredicate可以定义模糊查询条件 根据一定的条件 我们就可以从一个数组中快速找出 符合一定条件的元素对象 本次的示范我们沿用上次讲的 NSSortDescriptor 的使用 里面的代码 我们只需要稍微的修改一下 导航栏右边的按钮 为‘搜索年龄大于20的对象’ 然后再把点击左上角按钮的业务代码修改为如下: - (IBAction)sortAge:(id)sender { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age>20"]; //创建NSPredicate对象 并定义查询条件 self.datas = [[self.datas filteredArrayUsingPredicate:predicate] mutableCopy]; //使用数组的filteredArrayUsingPredicate方法 获取符合我们指定条件的对象 [self.tableView reloadData]; } 这个时候我们点击左上角的按钮 就可以查询出数组里面 年龄大于20的对象 除了 > 号之外 我们还可以用IN 查询两个数组的交集 我们新建一个程序 来测试IN 的用法

NSPredicate

爷,独闯天下 提交于 2019-12-28 10:09:12
一、简介   NSPredicate 指定数据被获取或者过滤的方式,是一个Foundation类。   Cocoa框架中的NSPredicate用于指定过滤器的条件(即查询),它的原理和用法都像SQL的 WHERE 和正则表达式一样,作用相当于数据库的过滤器。   最常用到的函数:+ predicateWithFormat: 创建谓词串 + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == 'Herbie'"]; //注意:如果谓词串中的文本块未被引用,则被看做是键路径,即需要用引号表明是字符串,单引号,双引号均可.键路径可以在后台包含许多强大的功能    二、谓词语法 范例数据源: 索引 0 1 2 3 名 Alice Bob Charlie Quentin 姓 Smith Jones Smith Alberts 年龄 24 27 33 31 背景: @interface Person : NSObject @property NSString *firstName; @property NSString *lastName; @property

NSPredicate 谓词

≯℡__Kan透↙ 提交于 2019-12-28 10:08:49
比较运算符 /**比较运算符 * >:大于 * <:小于 * >=:大于等于 * <=:小于等于 * =,==:等于 * !=,<>:不等于 * between:左边的表达式等于右边的表达式的值或者介于它们之间。右边是一个有两个指定上限和下限的数值的数列(指定顺序的数列)。比如,1 BETWEEN { 0 , 33 },或者$INPUT BETWEEN { $LOWER, $UPPER }。 NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"age<%d" , 30 ]; NSArray *array = [persons filteredArrayUsingPredicate :predicate]; in(包含) name以a开头的 predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"]; name以ba结尾的 predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"]; name中包含字符a的 predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"]; like

iOS中的谓词(NSPredicate)使用

[亡魂溺海] 提交于 2019-12-28 10:08:30
iOS中的谓词(NSPredicate)使用 首先,我们需要知道何谓谓词,让我们看看官方的解释: The NSPredicate class is used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering. NSPredicate类是用来定义逻辑条件约束的获取或内存中的过滤搜索。 可以使用谓词来表示逻辑条件,用于描述对象持久性存储在内存中的对象过滤。其实意思就是: 我是一个过滤器,不符合条件的都滚开 。 一、NSPredicate的基本语法 我们使用一门语言,无论是外语还是计算机语言,总是从语法开始的,这样我们才能正确的把握逻辑。所以我们从语法开始说起。在这部分我们仅关心其语法的使用 只要我们使用 谓词(NSPredicate) 都需要为谓词定义 谓词表达式 ,而这个表达式必须是一个返回BOOL的值。 谓词表达式由表达式、运算符和值构成。 1.比较运算符 比较运算符如下 =、==:判断两个表达式是否相等,在谓词中=和==是相同的意思都是判断,而没有赋值这一说 NSNumber *testNumber = @123; NSPredicate *predicate = [NSPredicate predicateWithFormat

NSPredicate for two NSNumber arrays

落爺英雄遲暮 提交于 2019-12-25 11:25:09
问题 I have a bit of a hard time writing a predicate for my search functionality and thought you'd be bale to help. So basically I have two arrays of NSNumbers. I want my predicate to satisfy the following: If a number's integerValue in array A matches any integerValue in array B. I don't want to use any sort of loop for this solution. Here's what I have so far ANY integerValue == ANY //how do I pass the entire array here and ask for the integerValue of each member? 回答1: The ANY operator will

NSPredicate with core data, search word with boundaries in string attribute

假装没事ソ 提交于 2019-12-25 09:29:18
问题 I feel like the answer to my question might already be somewhere on this forum, but after hours of scouring I just can't quite seem to get it right. I've never used SQL before so please bear with me. I have a core data model with only one entity and that entity has an attribute called "definitions" (each entity in the database is a dictionary entry). What I want to do is search the definitions for an input string with word boundaries . If I do a simple CONTAINS[cd] search with NSPredicate and

Not quite understanding NSMetadataQuery

你说的曾经没有我的故事 提交于 2019-12-25 08:08:21
问题 I am looking for a class to use from the Cocoa API to perform a Spotlight search on the entire system. I looked at NSMetadataQuery and believe this is the class for this, however I don't understanding how to do this; primarily NSPredicate . I would like to search the system for a file named "test123.html" for example and get its full path. Examples are greatly appreciated. My code: NSMetadataQuery *q = [[NSMetadataQuery alloc] init]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@

Using NSPredicateEditor, is there a way to get all of the Finder search categories for free?

孤人 提交于 2019-12-25 07:49:08
问题 I'm adding an NSPredicateEditor to my app to allow the user to seach for specific files and categories of files. The default NSPredicateEditor template from Inteface Builder adds a control containing only "name", "address", and "sign" categories. I'm looking for more specific categories. I know that I can add menu items to these menus in Interface Builder, but I would greatly prefer not to maintain a list myself of all of the various categories and values for file searches. In Finder, a great

Swift - Search using UISearchController and NSPredicate with an array of structs

核能气质少年 提交于 2019-12-25 06:47:05
问题 Currently I'm trying to set up a search bar for my tableView. I want to filter the user's input with NSPredicate to display filtered data from an array of struct objects. But when I try to call .filteredArrayUsingPredicate() on such an array, I get the following error [Course] is not convertible to 'NSArray' . Below is my code and a link to the repo. I'm also open to any better methods of approach. import UIKit import SwiftyJSON class CourseTableViewController: UITableViewController,