Howie Livingston
Howie Livingston

Reputation: 3

How to append NSMutable strings into a UILabel

This is my first question to Stack Overflow. I have been using this site for a while and have used it's resources to figure out answers to my programming questions but I'm afraid I can't find the answer I'm looking for this time.

I've created these five strings:

//List five items from the book and turn them into strings

//1 Josh the Trucker
NSString *stringJosh = @"Josh the Trucker";

//2 The Witch from the Remote Town
NSString *stringWitch = @"The Witch from the Remote Town";

//3 Accepting the curse rules "Willingly and Knowingly"
NSString *stringRules = @"Accepting the curse rules, --Willingly and Knowingly--";

//4 Josh's time left to live--Five Days Alive Permitted
NSString *stringFiveDays = @"Josh's time left to live--Five Days Alive Permitted";

//5 The Fire Demon Elelmental    
NSString *stringDemon = @"The Fire Demon Elelmental";

Then, I've put them in an array:

//Create an array of five items from the book
      NSArray *itemsArray = [[NSArray alloc] initWithObjects:
                            stringJosh,
                            stringWitch,
                            stringRules,
                            stringFiveDays,
                            stringDemon,
                            nil];

Then, I created this mutable string where I need to loop through the array and append the items to a UIlabel.

 NSMutableString *itemsString = [[NSMutableString alloc] initWithString:
                                  @"itemsArray"];

Here's the loop, which displays the items in the console log.

for (int i=0; i<5; i++)
{
    NSLog(@"Book Item %d=%@", i, itemsArray[i]);

}

My question is, how do I append these items into the UIlabel? These functions are in my appledelegate.

In my viewDidAppear function (flipsideViewController) I have:

label8.text =""----thats where the looped info needs to go.

How do I do this? I feel I need to put them together and append where the NSLog should be...but how do I transfer that info to the textlabel?

I hope I explained myself.

We haven't done ANY append examples, I guess this is where I need to get answers from the "wild"

This is the wildest coding environment I know so I'm hoping I can find some direction here.

Thanks for taking a look!

Upvotes: 0

Views: 990

Answers (3)

Vladimir
Vladimir

Reputation: 170829

Once you have all your strings that you want to concatenate in NSArray you can combine them with single call (with whatever separator you want):

NSString *combinedString = [itemsArray componentsJoinedByString:@" "];

If you need more complex logic you can use NSMutableString to create result you want while iterating array, i.e.:

NSMutableString *combinedString = [NSMutableString string];
[itemsArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
       [combinedString appendFormat:@"Book Item %d=%@ ", idx, obj];
    }];

Note also that it is better to iterate through collections using fast enumeration or block enumeration rather than using plain index-based for loop.

Upvotes: 4

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81858

NSMutableString *labelText = [NSMutableString string];
int i = 0;
for (NSString *item in itemsArray)
    [labelText appendFormat:@"Book Item %d=%@\n", i++, item];

label8.text = labelText;

Upvotes: 1

NHS
NHS

Reputation: 409

DO this

UILabel *mainlabel; mainlabel.text = [origText stringByAppendingString:get];

Add your text to mainlabel.. orig text is mutable string or else in forloop just append array object at index text to label.put above line of code in forloop

Upvotes: -1

Related Questions