如何在swift 3中使用NSPredicate过滤数组

编程入门 行业动态 更新时间:2024-10-26 22:27:25
本文介绍了如何在swift 3中使用NSPredicate过滤数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含多个字典的数组.

I have an arraycontaining several dictionaries.

{ DisplayName?:"Name of the employee" Age:28 Department:"Dept 2" }

我刚刚将我的 Objective-c 代码转换为 swift 并尝试像这样过滤.

I just converted my objective-c code into swift and trying to filter like this.

let exists = NSPredicate(format: "DisplayName2 CONTAINS[cd] \(searchText!)") let aList: Array<Any> = arrayDirectory.filter { exists.evaluate(with: $0) } if(aList.count>0) { arrayDirectory=aList facesCarousel.reloadData() }

但我总是将 aList 计数为 0.似乎没有过滤我的数组.如何在 swift 3 中编写正确的 NSPredicate 并使用它过滤我的数组.

But I am always getting the aList count as 0. It seems like not filtering my array. How can I write proper NSPredicatein swift 3 and filter my array using it.

推荐答案

与 ObjC 代码等效的原生 Swift 是

The native Swift equivalent to the ObjC code is

let filteredArray = arrayDirectory.filter { ($0["displayName2"] as! String).range(of: searchText!, options: [.diacriticInsensitive, .caseInsensitive]) != nil }

假设 arrayDirectory 是一个原生的 Swift Array.它还考虑了 case insensitive 和 diacritic insensitive 参数.

assuming arrayDirectory is a native Swift Array. It considers also the case insensitive and diacritic insensitive parameters.

更多推荐

如何在swift 3中使用NSPredicate过滤数组

本文发布于:2023-11-26 21:55:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   如何在   swift   NSPredicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!