How to Print out(NSLog) the properties of a custom object added to a NSMutableArray

后端 未结 4 1020
孤独总比滥情好
孤独总比滥情好 2020-12-11 15:43

I have a custom object that I create with 3 properties defined in it. I create the object and assign the values to those properties. After that I put that object into an

4条回答
  •  半阙折子戏
    2020-12-11 16:25

    You have to use the description method inside your Person class

    -(NSString *)description{
    
        return @"FirstName: %@, LastName: %@, E-mail: %@", 
                            _firstName, _lastName, _email;
    }
    

    This way you can print always the object you have inside your NSArray but instead of the memory description you'll get returned the values you've defined before in your description method of the specific object.

    If you just want to do this with the element from the NSArray use placeholders:

    NSLog(@"FirstName: %@, LastName: %@, E-mail: %@", 
                           obj.firstname, obj.lastname, obj.email);
    

    There is not much difference between, but its more useful because you don't have to rewrite it once you have created your description method, you just have to print the object.

提交回复
热议问题