1

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

Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
The Fonz
  • 207
  • 1
  • 4
  • 14

4 Answers4

9

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.

Egist Li
  • 624
  • 5
  • 9
6

Try this

for ( DataOrder *bug in [[ArrayBuying instance] tableau] ) 
{
    NSLog(@"Product:- %@ Price:-%@", bug.product, bug.price);
}
Kalpesh
  • 5,336
  • 26
  • 41
1

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

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

 NSLog(@"%@",[[[ArrayBuying instance] tableau] descriptionWithLocale:nil indent:1]);
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
1

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.

Ivica M.
  • 4,763
  • 1
  • 23
  • 16