user2677746
user2677746

Reputation:

Get all inside NSDictionary into a UITextView

I got a NSDictionary and I want to print it into a UITextView.

I tried using this code, but when I try to get it into a UITextView it gets only one line of text, when I NSLog it it works perfectly.

for(int i=0; i<[array count];i++) {
    dictionary = [array objectAtIndex:i];
    //that shows only one line from the dictionary in the textview
    textview.text = [dictionary objectForKey:@"text"];

    //that shows all the dictionary with key "text" in the log
    NSLog(@"%@", [dictionary objectForKey:@"text"]);
}

is there a way to solve this problem?

Upvotes: 2

Views: 1208

Answers (2)

Shubhank
Shubhank

Reputation: 21805

you should append it to the existing text

textview.text = @"";

  for(int i=0; i<[array count];i++)
{
        dictionary= [array objectAtIndex:i];
        //that shows only one line from the dictionary in the textview
        textview.text = [textview.text stringByAppendingFormat:@"%@\n",[diction objectForKey:@"text"]]];

        //that shows all the dictionary with key "text" in the log
        NSLog(@"%@",[diction objectForKey:@"text"]);
}

Upvotes: 1

Paulo Fierro
Paulo Fierro

Reputation: 603

The problem is that textview.text is overwriting the previous contents of textview.text.

What you need to do is append the string to the existing value of textview:

textview.text = [textview.text stringByAppendingString:[diction objectForKey:@"text"]];

But this will get all the values side by side, so you may want to add a space in between the values:

NSString *appendedText = [NSString stringWithFormat:@"%@ %@", textview.text, [diction objectForKey:@"text"]]; textview.text = appendedText;

Finally, you can use modern Objective-C literals to access your dictionary objects like array[i] and diction[@"text"] instead of using objectAtIndex and objectForKey to type a little less.

Upvotes: 0

Related Questions