简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM