0

I have a problem about printing an array. I am a java user, and i am new to objective-c.

in java code, i show u what i want

 for(myClass a: myVariable){
     System.out.println(a.toString);
 }

so how do i do this in objective-c, i have written like this:

- (void) updateTextField:(NSMutableArray *)array{
    for (Storage *obj in array)   // Storage class holds a name and a price.
        [check setText:@"%@",[obj description]];  // i did override the description method to print out the name and price.  and "Check" is my textField; 
}

it does not work. [check setText:@"%@",obj description]; got error "too many argument to method call if i take out the description;"

here is my Storage.m #import "Storage.h"

@implementation Storage


@synthesize name;
@synthesize price;

- (NSString *)description {
    return [NSString stringWithFormat: @"%@        %@", name, price];
}

@end
8
  • You should say either obj.description or [obj description]. The use of [] for method calls is very disorienting when coming from Java. Commented Jan 29, 2013 at 20:54
  • hey guys, i did try this [check setText:@"%@",[obj description]]; but it still show me an error " too many argument to method call, expected 1 but 2;" Commented Jan 29, 2013 at 20:56
  • You can't simply use @"%@" and get something formatted. It needs to be something like [NSString stringWithFormat:@"%@",[obj description]]. (You were the one who wanted to switch from Java.) Commented Jan 29, 2013 at 21:01
  • 1
    Note that the description method shouldn't be used in production code. Great for debugging/logging/learning, though. Commented Jan 29, 2013 at 21:02
  • (Or, for a full statement, [check setText:[NSString stringWithFormat:@"%@",[obj description]]];) Commented Jan 29, 2013 at 21:02

3 Answers 3

1

Based on your commented error on Ryan's post you could try this:

- (void) updateTextField:(NSMutableArray *)array{
    for (Storage *obj in array)
        [check setText:[NSString stringWithString:obj.description]];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user1933421 In strict Cocoa terms, that should be a lot of wasted processing — setText: implies you're setting a property so if your array is 1000 elements long then setting the value you don't want 999 times and then the actual value is a waste. If check is appending, you might consider a more compact form without the explicit loop like [[obj valueForKey:@"description"] componentsJoinedByString:@","]
1

The proper syntax for what you did would be [check setText:[NSString stringWithFormat: @"%@", [obj description]]];. But you can just use NSLog in a similar manner as Java's sysout:

for(Storage *obj in array)
    NSLog(@"%@", obj); //the description will be called implicitly, like toString()

Comments

1

Does -setText: take a format list or just an NSString?

That is, try:

- (void) updateTextField:(NSMutableArray *)array{
    for (Storage *obj in array)  
        [check setText:[obj description]];
}

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.