The Fonz
The Fonz

Reputation: 207

How to display an array of object with NSLog

what is the solution to display an array of object with "NSLog".

I can display it with my TableView with :

- (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView
                                 dequeueReusableCellWithIdentifier:@"MyBasicCell2"];
        DataOrder *bug = [[[ArrayBuying instance] tableau] objectAtIndex:indexPath.row];
      static NSString *CellIdentifier = @"MyBasicCell2";
        CustomCellFinalView *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


        if (!Cell) {
            Cell = [[CustomCellFinalView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        Cell.NomProduit.text = bug.product;
        Cell.Quantite.text = [NSString stringWithFormat:@"%.2f €", bug.price];
        Cell.Prix.text = [NSString stringWithFormat:@"X %d  ", bug.quantite];

        return Cell;
        return cell;    
}

When I try in my ViewDidLoad: method this

NSLog(@"%@",[[ArrayBuying instance] tableau]);

I obtain in my target output:

(
    "<DataOrder: 0x15d5d980>",
    "<DataOrder: 0x15de1aa0>"
)

Thank you very much for your futur help

Upvotes: 1

Views: 13494

Answers (4)

Ivica M.
Ivica M.

Reputation: 4823

NSLog calls object's description method in order to print it. If you don't implement this method for your class (DataOrder in this case), it will call NSObject's default implementation which only prints the object's address.

Upvotes: 1

Egist Li
Egist Li

Reputation: 634

You can implement - (NSString *)description method in class DataOrder.

NSLog will show the return value of description method of a instance if the description is available.

Check out the document of NSObject protocol https://developer.apple.com/library/mac/documentation/cocoa/reference/foundation/Protocols/NSObject_Protocol/Reference/NSObject.html

With description method implemented, you can print object easier afterward.

Upvotes: 9

Parag Bafna
Parag Bafna

Reputation: 22930

Write - (NSString*) description method in DataOrder class.

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

 NSLog(@"%@",[[[ArrayBuying instance] tableau] descriptionWithLocale:nil indent:1]);

Upvotes: 1

Kalpesh
Kalpesh

Reputation: 5334

Try this

for ( DataOrder *bug in [[ArrayBuying instance] tableau] ) 
{
    NSLog(@"Product:- %@ Price:-%@", bug.product, bug.price);
}

Upvotes: 6

Related Questions