Gajendrasinh Chauhan
Gajendrasinh Chauhan

Reputation: 3397

How to convert NSMutableArray value to NSString?

I have NSMutableArray named *total and UILabel as *lblTotal. I got result value from looping. Now I want to set this value as UILabel in viewDidLoad. How can I fix this?

Here is code I am using.

for(int counter = 0; counter < [node childCount]; counter++) {
            if ([[[node childAtIndex:counter] name] isEqualToString:@"Total"]){

                NSString *str = [[node childAtIndex:counter] stringValue];

                [total addObject:str];
                NSLog(@"Grand total is %@", total);
            }
        }

Simply I want to set result value in NSString lbltotal.text = total.

Upvotes: 1

Views: 7390

Answers (2)

icodebuster
icodebuster

Reputation: 8964

NSArray  *array1 = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSString *joinedString = [array1 componentsJoinedByString:@","];

componentsJoinedByString: will join the components in the array by the specified string and return a string representation of the array.

To set the value on UILabel

UILabel *label = [[UILabel alloc] init];
[label setText:joinedString];

Upvotes: 7

jobima
jobima

Reputation: 5920

To convert an NSMutableArray to NSString it's like this:

NSMutableArray * myArray = [[NSMutableArray alloc] initWithObjects:@"one", @"two", @"three", nil];
NSString * myString = [myArray componentsJoinedByString:@", "];
UILabel * myLabel = [[UILabel alloc] init];
[myLabel setText:myString];

I hope it helps ;-)

Upvotes: 2

Related Questions