0

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 would like to add a NSString called "searchKeyword" to the end of every object in the array.

How do I do this?

Thanks!

2
  • Why do you store this values in an array? They are basically all the same. You could simply create a string like this with: [NSString stringWithFormat:@"http://example.com/index.php?number=%i&keyword=", intValue] Commented Sep 19, 2011 at 22:44
  • I'm a newbie. How would I do that? Commented Sep 19, 2011 at 22:45

6 Answers 6

5

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;
Sign up to request clarification or add additional context in comments.

4 Comments

He does not need a mutable array, as he does not append anything to the array. You only need NSMutableArray if you want to change the SIZE of the array not the VALUES in it.
@gebirgsbaerbel: But you can only change NSString values by replacing them with new values, so you need to build a new array containing those new values.
@gebirgsbaerbel As Jeremy said, dot is starting with an immutable array (NSArray) with immutable values (NSString). He can can change neither the composition of the array NOR its values, so he needs to build a new array using NSMutableArray, creating new values as he goes along.
What you say is true, I just wanted to make clear to him that this is not always necesarry only because you want to change values in an array. He seems to be very new to objective-c and I did not want him to have this misconception.
2

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

2 Comments

That's a neat snippet, tho I'd recommend to consider copying the elements of the original array if you need your own implementation and use enumerateObjectsUsingBlock:(void (^)(...))block otherwise.
@aldi: As I said — It might be overkill, if you just got one array that you need to fill. But on the long run it will help to hide a lot of boilerplate code.
1

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

1 Comment

Thanks for the good ideas on how to do this the right away. I'll use the Array for now, but will look in to this once I ship this early version!
1

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.