2

I have one array that has many name in self.this is my code :

NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"figo",@"messi",@"totti",@"ronaldo",@"pirlo", nil];

I want append this names in one TextView that any name to be in one line... but when run this app at last I see last name is in UITextView and another names was remove!!! please guide me about it.

this is my complete code :

ViewController.h

@interface ViewController : UIViewController
{
    IBOutlet UITextView *text;
}
@property(nonatomic,strong) IBOutlet UITextView *text;

@end

ViewController.m

@implementation ViewController
@synthesize text;

- (void)viewDidLoad
{
    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"figo",@"messi",@"totti",@"ronaldo",@"pirlo", nil];
    [super viewDidLoad];
    for (int i=0; i < [array count]; i++) {
        NSString *a = [array objectAtIndex:i];
        NSLog(@"%@",a);
        [text setText:a];
    }
}
@end

I want this name append in UITextView and these names to be in one line....like this:

figo
messi
totti
ronaldo
pirlo 
1
  • 1
    Do you want to added in only one line or each in New line ?? Commented Aug 30, 2013 at 8:52

3 Answers 3

6

You can try instead

- (void)viewDidLoad
{
    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"figo",@"messi",@"totti",@"ronaldo",@"pirlo", nil];
    [super viewDidLoad];

    [text setText:[array componentsJoinedByString:@"\n"];
}

or if you want them all in a line, separated by whatever character, you can just replace @"\n" and use yours. @", " would be ok, but also @" "

Sign up to request clarification or add additional context in comments.

Comments

4
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"figo",@"messi",@"totti",@"ronaldo",@"pirlo", nil];
    for (NSString *str in array) {
        tvText.text = [NSString stringWithFormat:@"%@\n%@", tvText.text,str];
    }

1 Comment

To print in same line use following code.. tvText.text = [NSString stringWithFormat:@"%@%@", tvText.text,str];
1

you can also do this

 NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"figo",@"messi",@"totti",@"ronaldo",@"pirlo", nil];
    NSMutableString* theText = [NSMutableString new];
    for (NSString* line in array)
    {
    [theText appendFormat: @"%@\n", line];
    }


[text setText:theText];

just change the NSString to NSMutableString

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.