如何检查 NSMutableArray 是否包含包含特定文本的对象?
如何检查 NSMutableArray 是否包含包含特定文本的对象?然后,如果找到一段文本,它将用另一段文本替换它。
谢谢
How can I check if an NSMutableArray contains an object which contains certain text? Then, if there is one piece of text which has been found, it will replace it with another bit of text.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西应该可以工作:
然后你可以这样使用它:
Something like this should work:
You'd then use it as such:
您可以在 for 循环内对 NSMutableArray 中的所有对象执行检查,使用
[object rangeOfString:@"text"] 用于检查范围。
You can perform a check inside a for loop for all objects in the NSMutableArray , use
[object rangeOfString:@"text"] for check the range.
您可以迭代数组,检查每个对象,然后在必要时进行替换。
或者您可以使用更奇特的方法:
我还没有尝试过,但您可以在对象中创建一个方法,该方法接受字典或数组,其中第一个元素是要比较的文本,第二个元素是要替换的文本:
那么您只会有调用:
在运行时它比循环数组的每个元素要快得多(特别是如果你有很多元素)
You can either iterate on your array, do the checking on each object and then replace if necessary.
Or you could use something fancier with :
I havent tried it but you could create a method in your object that takes a dictionary, or an array with first element being the text to compare and second element the text to replace :
Then you would only have to call :
It would be a lot faster at run time than looping on each element of your array (especially if you have many elements)