开发者

Add NSString to the end of every object in an array

开发者 https://www.devze.com 2023-04-06 04:29 出处:网络
I have an array like this: self.youtubeVideos = [NSArray arrayWithObjects: @\"http://example.com/index.php?number=1&keyword=\",

I have an array like this:

self.youtubeVideos = [NSArray arrayWithObjects: 

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

                      nil];

I wou开发者_运维百科ld like to add a NSString called "searchKeyword" to the end of every object in the array.

How do I do this?

Thanks!


Make a mutable array, then step through and modify each element as you go along.

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

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

self.youtubeVideos = fixedUrls;


My block-based solution might look a bit overkill, but if you have several of this requirements, it could be helpful:

create a Category on 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

and use it like this:

#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);

or even

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"];}];

I incorporated this example into a sample project I wrote to teach myself block-based techniques to use functional-style programming with Objective-C. Coming from Python I always missed List Comprehension like

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

Block-based it looks like this:

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"];}];


You can append something to an existing NSString with

NSString* string1 = @"String1";
NSString* string2 = @"String2";
NSString* string3 = [string1 stringByAppendingString:string2];

Note that this will always create a new NSString as NSStrings cannot be changed once they have been created. For this you would need an NSMutableString.

However your strings seem so similar that storing them in an array does not seem logical. If the only difference between the values in your array is an integer you can easily create all of them whenever you want from a template String. For example this will use a templateString and in the next line replace the %i with the integer value 4.

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


You don't have to create a new array. You can replace the objects in the mutablearray with the appended string:

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


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...
}


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号