如何检查 NSMutableArray 是否包含包含特定文本的对象?

发布于 2024-12-13 13:00:17 字数 76 浏览 2 评论 0原文

如何检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我做我的改变 2024-12-20 13:00:17

像这样的东西应该可以工作:

@interface NSMutableArray (JRAdditions) 

- (void) replaceStringObjectsContainingString:(NSString *) str withString:(NSString *) newString;

@end

@implementation NSMutableArray (JRAdditions)

- (void) replaceStringObjectsContainingString:(NSString *) str withString:(NSString *) newString {

   for(unsigned i = 0; i < [self count]; ++i) {
      id obj = [self objectAtIndex:i];

      if(![obj isKindOfClass:[NSString class]]) continue;

      NSString *replaced = [str stringByReplacingOccurrencesOfString:str withString:newString];
      [self replaceObjectAtIndex:i withObject:replaced];

   }
}

@end

然后你可以这样使用它:

NSMutableArray *array = ...;

[array replaceStringObjectsContainingString:@"blah" withString:@"foo"];

Something like this should work:

@interface NSMutableArray (JRAdditions) 

- (void) replaceStringObjectsContainingString:(NSString *) str withString:(NSString *) newString;

@end

@implementation NSMutableArray (JRAdditions)

- (void) replaceStringObjectsContainingString:(NSString *) str withString:(NSString *) newString {

   for(unsigned i = 0; i < [self count]; ++i) {
      id obj = [self objectAtIndex:i];

      if(![obj isKindOfClass:[NSString class]]) continue;

      NSString *replaced = [str stringByReplacingOccurrencesOfString:str withString:newString];
      [self replaceObjectAtIndex:i withObject:replaced];

   }
}

@end

You'd then use it as such:

NSMutableArray *array = ...;

[array replaceStringObjectsContainingString:@"blah" withString:@"foo"];
著墨染雨君画夕 2024-12-20 13:00:17

您可以在 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.

执笏见 2024-12-20 13:00:17

您可以迭代数组,检查每个对象,然后在必要时进行替换。

或者您可以使用更奇特的方法:

- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)anObject

我还没有尝试过,但您可以在对象中创建一个方法,该方法接受字典或数组,其中第一个元素是要比较的文本,第二个元素是要替换的文本:

- (void) replaceTextWithParameters:(NSArray*)parameters {
    if([self.yourText isEqualToString:[parameters objectAtIndex:0]) {
        self.yourText = [parameters objectAtIndex:1];
    }
}

那么您只会有调用:

NSArray *parameterArray = [NSArray arrayWithObjects:@"Text to search", @"replacementText", nil];
[yourArray makeObjectsPerformSelector:@selector(replaceTextWithParameters:) withObject:parameterArray];

在运行时它比循环数组的每个元素要快得多(特别是如果你有很多元素)

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 :

- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)anObject

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 :

- (void) replaceTextWithParameters:(NSArray*)parameters {
    if([self.yourText isEqualToString:[parameters objectAtIndex:0]) {
        self.yourText = [parameters objectAtIndex:1];
    }
}

Then you would only have to call :

NSArray *parameterArray = [NSArray arrayWithObjects:@"Text to search", @"replacementText", nil];
[yourArray makeObjectsPerformSelector:@selector(replaceTextWithParameters:) withObject:parameterArray];

It would be a lot faster at run time than looping on each element of your array (especially if you have many elements)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文