VinsonG
VinsonG

Reputation: 25

Print out an array in objective-c

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

Upvotes: 0

Views: 201

Answers (3)

StilesCrisis
StilesCrisis

Reputation: 16290

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]];
}

Upvotes: 1

Firo
Firo

Reputation: 15566

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]];
}

Upvotes: 1

cscott530
cscott530

Reputation: 1708

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()

Upvotes: 1

Related Questions