文字检测
2015-12-13 21:16
246 查看
苹果提供了一个方法,可以从一大串字符串中提取出有意义的字符串,比如URL,日期,地址等
let detector = try? NSDataDetector(types: NSTextCheckingTypes(NSTextCheckingAllTypes))
let inputText = inoutText.string
var resultString = ""
if let matches = detector?.matchesInString(inputText!, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0, (inputText! as NSString).length)) {
for match in matches {
let result = match
switch result.resultType {
case NSTextCheckingType.Link:
resultString += "Link: \(result.URL)\n"
case NSTextCheckingType.Date:
resultString += "Date: \(result.date)\n"
case NSTextCheckingType.PhoneNumber:
resultString += "Date: \(result.phoneNumber)\n"
case NSTextCheckingType.Address:
resultString += "Date: \(result.addressComponents)\n"
default:
resultString += "Other: \(result.description)\n"
}
}
}
print(resultString)
outputText.stringValue = resultString
let detector = try? NSDataDetector(types: NSTextCheckingTypes(NSTextCheckingAllTypes))
let inputText = inoutText.string
var resultString = ""
if let matches = detector?.matchesInString(inputText!, options: NSMatchingOptions.ReportProgress, range: NSMakeRange(0, (inputText! as NSString).length)) {
for match in matches {
let result = match
switch result.resultType {
case NSTextCheckingType.Link:
resultString += "Link: \(result.URL)\n"
case NSTextCheckingType.Date:
resultString += "Date: \(result.date)\n"
case NSTextCheckingType.PhoneNumber:
resultString += "Date: \(result.phoneNumber)\n"
case NSTextCheckingType.Address:
resultString += "Date: \(result.addressComponents)\n"
default:
resultString += "Other: \(result.description)\n"
}
}
}
print(resultString)
outputText.stringValue = resultString
相关文章推荐
- nova http 409 虚拟机状态重置
- [javase学习笔记]-4.4 函数的重载
- 申请内存的函数有哪些
- Q:应用程序无法正常启动(0xc000007b)
- 使用Windows Live发布博客到博客园
- 操作系统---基础题目汇总十三
- TCP/IP详解卷1 读书笔记:第二十八章 SMTP和POP3协议
- vmalloc 实现
- 源码安装zabbix2.4
- 内核空间与用户空间的通信方式
- SMO(Sequential Minimal Optimization) 伪代码(注释)
- 1-1、Spark 的local模式安装
- poj_1019
- JgibbLDA输出结果说明与示例
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
- 适配器模式
- 如何让IE9以下版本认识html5元素
- 深入浅出—闭包(整理篇)适合新手
- 跨域
- iOS之多线程