swift 报错 Call can throw, but it is not marked with 'try' and the error is not handled

在开发中使用正则表达式时报了这样的问题:Call can throw, but it is not marked with ‘try‘ and the error is not handled,即:

let regex:NSRegularExpression = NSRegularExpression(pattern: "^1[3|4|5|7|8][0-9]{9}$", options: .caseInsensitive)

先看下在OC中的使用

  NSError *error = NULL;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^1[3|4|5|7|8][0-9]{9}$" options:NSRegularExpressionCaseInsensitive error:&error];

NSTextCheckingResult *result = [regex firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];

  if (result) {

return YES;

}

return NO;

对比一下发现原因是没有添加错误处理,可以通过添加try解决。

最终解决后代码:

  do{

let regex:NSRegularExpression = try NSRegularExpression(pattern: "^1[3|4|5|7|8][0-9]{9}$", options: .caseInsensitive)

let result:NSTextCheckingResult = regex.firstMatch(in: self, options:.reportProgress, range: NSMakeRange(0, self.count))!

if result != nil {

return true

}

}catch { }

return false

swift 报错 Call can throw, but it is not marked with 'try' and the error is not handled

原文地址:https://www.cnblogs.com/cui-cui/p/10109855.html

时间: 2024-11-05 21:07:01

swift 报错 Call can throw, but it is not marked with 'try' and the error is not handled的相关文章

docker 报错: Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

centos 启动docker服务报错: Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details. 1,create daemon.json in /etc/docker/ 2, put this in it: { &q

[已解决]报错: Creating Server TCP listening socket 127.0.0.1:6379: bind: No error

问题: windows下安装Redis第一次启动报错: [2368] 21 Apr 02:57:05.611 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error 解决: 在命令行中运行 redis-cli.exe 127.0.0.1:6379>shutdown not connected>exit 然后重新运行 redis-server.exe redis.windows.conf 启动成功! from:

加压压缩文件报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now

压缩包是直接weget 后面加官网上的tar包地址获取的 [[email protected] ~]# tar -zxvf /home/hadoop/hadoop-2.6.5-src.tar.gz gzip: stdin: not in gzip formattar: Child returned status 1tar: Error is not recoverable: exiting now[[email protected] ~]# tar -xvf /home/hadoop/hadoo

针对CSV备份报错,Event ID:8194,Source:VSS

平台:Windows server 2012 卷类别:CSV(cluster shared volume) 存储:DELL equalogic PS 6100 软体:HIT 现象:针对CSV进行备份的时候,调用Microsoft的VSS能备份成功,但是不能调用DELL equalogic的VSS来备份,且Windows不断报以下错误:VSS 8194 经多放资料查证,本VSS报错与备份无关(Microsoft和赛门铁克都有资料说明),但是此报错看着很烦,而且过多,容易覆盖掉其他Error,解决该

tomcat中安装JDK报错详细解决过程

在安装tomcat时解压安装JDK时出现如下报错:[[email protected] src]# wget 'http://download.oracle.com/otn-pub/java/jdk/8u65-b17/jdk-8u65-linux-i586.tar.gz'--2016-01-18 04:21:56--  http://download.oracle.com/otn-pub/java/jdk/8u65-b17/jdk-8u65-linux-i586.tar.gz正在解析主机 dow

MySQL server has gone away报错原因分析/

在平时和开发的交流 以及 在论坛回答问题的或称中会发现这个问题被问及的频率非常高. 程序中报错: MySQL server has gone away 是什么意思? 如何避免? 因此,感觉有必要总结一下发生这个问题的原因.今天正好看到一篇外文blog总结的比较好,就翻译过来了 原文:http://ronaldbradford.com/blog/sqlstatehy000-general-error-2006-mysql-server-has-gone-away-2013-01-02/ 原因1.

AspectJ报错:error at ::0 can't find referenced pointcut XXX

今天在使用AspectJ进行注解切面时,遇到了一个错误. 切点表达式就是无法识别——详细报错信息如下: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xingoo' defined in class path resource [bean.xml]: Initialization of bean fail

R语言的各种报错及其解决方法

前言: 人不能在同个地方栽两次跟头,那样的生活效率太低.所以我一直以来都保持着随手记录的习惯.现在把近半个月来遇到的关于R语言的错误分享一下,希望能帮助到一些朋友,同时也希望各位朋友把遇到的已解决的.未解决的错误提出来一下,完善彼此的知识点,谢谢! 目录: 连接数据库报错:negative length vectors are not allowed 连接数据库报错:first argument is not an open RODBC channel 连接数据库报错:incorrect num

exchange服务器新建用户报错

最近升级了exchange2013,升级完成后对系统功能进行验证发现ECP新建用户时会报错报错内容如下: Active Directory operation failed on my.domain.controller. This error is not retriable. Additional information: A device attached to the system is not functioning. Active directory response: 000000