iOS:如何避免在搜索栏中输入特殊字符? [英] iOS: How to avoid special characters being entered in a search bar?

查看:435
本文介绍了iOS:如何避免在搜索栏中输入特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能看错了,但现在,如果用户在搜索栏中输入某些特殊字符,应用程序将崩溃。这些包括\和'。

I may be looking at this incorrectly, but right now, if a user enters certain special characters in a search bar, the app will crash. These include \ and '.

撇号是一个问题,因为不是和不会会导致崩溃

The apostrophe is a problem, in that words like "isn't" and "doesn't" will cause a crash

我该如何解决这个问题?

How should I go about working around this problem?

非常感谢

Many thanks

推荐答案

在使用搜索字符串进行搜索之前,先对其进行过滤,如下所示:

Before doing a search using the search string, pre-filter it, like this:

NSString *safeSearchString = [[searchString componentsSeparatedByCharactersInSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]] componentsJoinedByString:@""];

这篇关于iOS:如何避免在搜索栏中输入特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆