StringSearchKit
这是一个简单的库,用于macOS/iOS/tvOS,它使用Trie实现快速、内存中的基于前缀的字符串搜索。
要求
- macOS 10.11+
- iOS 9+
- tvOS 9+
安装
StringSearchKit可通过CocoaPods获得。要安装它,只需在Podfile中添加以下行
pod "StringSearchKit"
为什么这个框架有用?
因为它使得在应用程序中实现 自动完成 功能变得非常容易,正如我在自己的macOS™应用中所做的那样 - Sequence Diagram
如何开始?
使用
StringDictionary(withStrings: [String], preserveCase: Bool = false)
StringDictionary(withTextFileNamed: <在您的Bundle中的文本文件名>, preserveCase: Bool = false)
StringDictionary(withTextFilepath: <文本文件的路径>), preserveCase: Bool = false
使用上述任一初始化器创建一个 StringDictionary
实例。
从文件初始化时,它应仅包含每行一个字符串。
字符串不限于单个单词,但在示例中为了简单起见,是这样。
示例文件内容:
act
apologise
apology
app
apple
一旦实例化,可以使用 StringDictionary
快速找到给定前缀的字符串(不区分大小写)。
let stringDictionary = StringDictionary(withStrings: ["act", "app", "apple", "apologise", "apology"])
let searchResults = stringDictionary.strings(withPrefix: "app")
// searchResults will then contain ["app", "apple"]
搜索结果的字体保持
如果在任何 init
方法中设置了 preserveCase: true
(出于向后兼容性考虑默认为 false
),则结果将以添加原始单词时的相同字体返回。
例如:
let stringDictionary = StringDictionary(withStrings: ["Act", "App", "Apple", "Apologise", "Apology"], preserveCase: true)
let searchResults = stringDictionary.strings(withPrefix: "app")
// searchResults will then contain ["App", "Apple"]
作者
Mike O. Abidakun [email protected]
许可
StringSearchKit遵循MIT许可。有关更多信息,请参阅LICENSE文件。