Is there a way to log all the property values of an Objective-C instance

前端 未结 5 1653
庸人自扰
庸人自扰 2021-02-04 03:22

I was just wondering if there is a quick and easy way of printing out to the log all of the various values of the properties to my class for debugging purposes. Like I would lik

5条回答
  •  余生分开走
    2021-02-04 03:42

    yes, one way would be to ask for all properties and then use KVC for example:

    //properties
    unsigned int cProperties = 0;
    objc_property_t *props = class_copyPropertyList(self.class, &cProperties);
    for(int i = 0; i < cProperties; i++) {
        const char *name = property_getName(props[i]);
        NSLog(@"%@=%@", name, [self valueForKey:name];
    }
    

    an alternate way is to go through all the methods of a class, get the return type, invoke and print it

提交回复
热议问题