0

I want to show all objects in mutable array on to textfield, label, something else except NSLog

   - (IBAction)purchasePressed:(id)sender {
NSMutableArray *addItem = [[NSMutableArray alloc] init];
[addItem addObject:@"Almond"];
[addItem addObject:@"Choc"];

"number" is my label (I'm not sure that all of Objects in MutableArray can be showed on textfield or not?) i can do it only with NSLog.

for (i = 0;i < [addItem count] ; i++ )
{
   NSLog(@"%@", addItem);
    NSString *test1=(@"%@", addItem);
    number.text=test1;
}
1
  • You might want to use the Objective-C for in loop. for (NSString *string in addItem) {} Commented Oct 6, 2013 at 16:14

2 Answers 2

2

Every time you set the text of a label you replace the previous text.

Try replacing your whole loop with something like:

number.text = [addItem componentsJoinedByString:@", "];

Which will create a single string from all of the strings in the array and add that to the label. You could do something similar in your loop if you want to.

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

3 Comments

Just curiosity, Wain: What if we had a huge collection of NSStrings, is there an equivalent version of enumerate array using block for this?
@Unheilig, to do what exactly? A custom concatenation of the strings? NSArray provides enumeration with a block, what you do with it is your choice...
@Wain Thank you so much, can I ask you more about how could I count all objects in mutableArray?
1

If you want a string with all the values concatenated:

NSString *mainString = [NSString alloc] init]; 

for (NSString *item in addItem) {

    mainString = [mainString stringByAppendingString:item];
}

number.text = mainString;

EDIT: Using NSMutableString

NSMutableString *mainString = [[NSMutableString alloc] init];

for (NSString *item in addItem) {

    [mainString appendString:item];
}

number.text = mainString;

3 Comments

Would you not use a mutable string?
I've got error in [mainString appendingString:item]; with no visible @interface for'NSMutableString' declares the selector'appending string:'
Sorry I've made ane typing error. Use appendString instead appendingString. I've edit my previous answer.

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.