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

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

6

Try this

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

Comments

1

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

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

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

Comments

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.

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.