【问题标题】:Add NSString to the end of every object in an array将 NSString 添加到数组中每个对象的末尾
【发布时间】:2011-09-19 22:35:13
【问题描述】:

我有一个这样的数组:

self.youtubeVideos = [NSArray arrayWithObjects: 

                      @"http://example.com/index.php?number=1&keyword=", 
                      @"http://example.com/index.php?number=2&keyword=",

                      nil];

我想在数组中每个对象的末尾添加一个名为“searchKeyword”的 NSString。

我该怎么做?

谢谢!

【问题讨论】:

  • 为什么将这些值存储在数组中?它们基本上都是一样的。你可以简单地创建一个这样的字符串:[NSString stringWithFormat:@"http://example.com/index.php?number=%i&keyword=", intValue]
  • 我是新手。我该怎么做?

标签: iphone objective-c cocoa-touch


【解决方案1】:

创建一个可变数组,然后逐步检查并修改每个元素。

NSMutableArray* fixedUrls = [NSMutableArray arrayWithCapacity:self.youtubeVideos.count];    

for (NSString* url in self.youtubeVideos)
{
    [fixedUrls addObject:[url stringByAppendingString:searchKeyword]];
}

self.youtubeVideos = fixedUrls;

【讨论】:

  • 他不需要可变数组,因为他不向数组追加任何内容。如果要更改数组的 SIZE 而不是其中的 VALUES,则只需要 NSMutableArray。
  • @gebirgsbaerbel:但是您只能通过将NSString 值替换为新值来更改它们,因此您需要构建一个包含这些新值的新数组。
  • @gebirgsbaerbel 正如 Jeremy 所说,dot 以具有不可变值 (NSString) 的不可变数组 (NSArray) 开头。他既不能改变数组的组成,也不能改变它的值,所以他需要使用 NSMutableArray 构建一个新数组,并在他前进的过程中创建新值。
  • 你说的是真的,我只是想向他说明这并不总是必要的,只是因为你想改变数组中的值。他似乎对 Objective-c 很陌生,我不希望他有这种误解。
【解决方案2】:

我的基于块的解决方案可能看起来有点矫枉过正,但如果您有几个这样的要求,它可能会有所帮助:

在 NSArray 上创建一个类别:

@interface NSArray (FunctionalTools)

- (NSArray *)arrayByPerformingBlock:(id  (^)(id element))performBlock;

@end


@implementation NSArray (FunctionalTools)

- (NSArray *)arrayByPerformingBlock:(id  (^)(id element))performBlock 
{
    NSMutableArray *array = [NSMutableArray array];
    for (id element in self){
            [array addObject:performBlock(element)];
    }
    return array;
}

@end

并像这样使用它:

#import "NSArray+FunctionalTools.h"

//....

self.youtubeVideos = [NSArray arrayWithObjects: 
                                  @"http://example.com/index.php?number=1&keyword=", 
                                  @"http://example.com/index.php?number=2&keyword=",
                                  nil];

self.youtubeVideos = [youtubeVideos arrayByPerformingBlock:^id(id element) { return [element stringByAppendingString:@"KeyWord"];}];
NSLog(@"%@", youtubeVideos);

甚至

self.youtubeVideos = [[NSArray arrayWithObjects: 
                                      @"http://example.com/index.php?number=1&keyword=", 
                                      @"http://example.com/index.php?number=2&keyword=",
                                      nil] arrayByPerformingBlock:^id(id element) { return [element stringByAppendingString:@"KeyWord"];}];

我将此示例合并到 sample project 中,我编写该代码是为了自学基于块的技术,以便在 Objective-C 中使用函数式编程。来自 Python 我总是错过像

这样的 List Comprehension
l = ['aa', 'ab','c','ad','dd']
l = [i+i for i in l if i.startswith('a')]

基于块的它看起来像这样:

NSArray *array = [NSArray arrayWithObjects:@"aa", @"ab",@"c",@"ad",@"dd", nil];
array = [array arrayByPerformingBlock:^id(id element) { return [element stringByAppendingString:element]; } 
                  ifElementPassesTest:^BOOL(id element) {return [element hasPrefix:@"a"];}];

【讨论】:

  • 这是一个简洁的 sn-p,如果您需要自己的实现并使用 enumerateObjectsUsingBlock:(void (^)(...)),我建议您考虑复制原始数组的元素否则阻止。
  • @aldi:正如我所说的那样——如果你只有一个需要填充的数组,这可能有点过头了。但从长远来看,这将有助于隐藏大量样板代码。
【解决方案3】:

您可以使用

将一些内容附加到现有的 NSString
NSString* string1 = @"String1";
NSString* string2 = @"String2";
NSString* string3 = [string1 stringByAppendingString:string2];

请注意,这将始终创建一个新的 NSString,因为 NSString 一旦创建就无法更改。为此,您需要一个 NSMutableString。

但是,您的字符串看起来如此相似,以至于将它们存储在数组中似乎不合逻辑。如果数组中的值之间的唯一区别是整数,则您可以随时从模板字符串轻松创建所有值。例如,这将使用一个模板字符串,并在下一行中将 %i 替换为整数值 4。

NSString* templateString = @"Ich habe %i Punkte";
NSString* scoreString = [NSString stringWithFormat:templateString, 4];

【讨论】:

  • 感谢有关如何立即执行此操作的好主意。我现在会使用 Array,但是一旦我发布了这个早期版本,我会考虑这个!
【解决方案4】:

您不必创建新数组。您可以将 mutablearray 中的对象替换为附加的字符串:

for(int i=0;i<self.youtubeVideos.count;i++){
    [self.youtubeVideos replaceObjectAtIndex:i 
    withObject:[[self.youtubeVideos objectAtIndex:i]  
    stringByAppendingString:searchKeyword]];
}

【讨论】:

    【解决方案5】:
    youtubeVideos = [NSMutableArray arrayWithObjects: 
                      @"http://example.com/index.php?number=1&keyword=", 
                      @"http://example.com/index.php?number=2&keyword=",
                      nil];
    
    for (NSString *s in youtubeVideos) {
        s = [s stringByAppendingString:@"KEYWORD"];
        // Do something with the new s here...
    }
    

    【讨论】:

      【解决方案6】:
      NSMutableArray *array = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString:@"http://example.com/index.php?number=1&keyword="],[NSMutableString stringWithString:@"http://example.com/index.php?number=2&keyword="],nil];
      for (NSMutableString *temp in array) {
          [temp appendString:searchKeyword];
      }
      

      【讨论】:

        猜你喜欢
        • 2011-06-16
        • 1970-01-01
        • 2023-03-07
        • 2015-07-17
        • 1970-01-01
        • 2017-06-22
        • 1970-01-01
        • 1970-01-01
        • 2013-07-15
        相关资源
        最近更新 更多