At the end of the article "IOS Efficient Development-----Implement description method", I left a question that if you were to overwrite description methods and Debugdescription methods with each custom class, it would be a very large workload, Because there are many custom classes, and the properties in each custom class change with the need to modify these two methods, it becomes infinite, yongwuningri, or not, or you need to find a better way to use.
Next, we will use the dynamic Runtime runtime.
Let's assume that the properties in the class we're dealing with are all OC objects, and the idea is to implement the following:
Idea one: You can define a parent class, implement the description method and the Debugdescription method in the parent class, and then make each class its child class, with the following implementation:
pingkfather.h// learneffective2.0//// Created by PK on 15/5/20.// Copyright (c) 2015 ISS. All rights reserved.//#import <Foundation/Foundation.h> @interface pingkfather:nsobject@end
pingkfather.m//learneffective2.0////Created by PK on 15/5/20.//Copyright (c) 2015 ISS. All rights reserved.//#import "PingkFather.h" #import <objc/runtime.h> @implementation pingkfather-( Nsdictionary *) Descriptionmehtod: (ID) obj{nsmutabledictionary * dic = [nsmutabledictionary dictionary]; U_int count; Get all properties objc_property_t *properties = Class_copypropertylist ([obj class], &count); for (int i = 0; i<count; i++) {//Gets the name of the property const char * propertyname = Property_getname (Properties[i]); ID ObjValue; Convert a property to a bit string NSString * key = [NSString Stringwithutf8string:propertyname]; Value ObjValue = [obj Valueforkey:key]; Written in dictionary [dic Setobject:objvalue Forkey:key]; } nsdictionary * dictionary = [dic copy]; return dictionary;} -(NSString *) description{return [NSString stringwithformat:@ "%@", [self descriptionmehtod:self]];} -(NSString *) debugdescription{return [NsstrinG stringwithformat:@ "<%@:%p,%@>", [Self-class], self, [self descriptionmehtod:self ]];} @end
Then define a subclass that inherits from Pingkfather, with the following code:
pingkson.h// learneffective2.0//// Created by PK on 15/5/20.// Copyright (c) 2015 ISS. All rights reserved.//#import "PingkFather.h" @interface Pingkson:pingkfather@property (nonatomic,copy,readonly) NSString * selfname; @property (nonatomic,copy,readonly) nsstring * fathername; @property (nonatomic,copy,readonly) NSString * mothername;-(ID) initwithselfname: (NSString *) selfname andfathername: (NSString *) Fathername AndMotherName :(NSString *) mothername; @end
pingkson.m// learneffective2.0//// Created by PK on 15/5/20.// Copyright (c) 2015 ISS. All rights reserved.//#import "PingkSon.h" @implementation pingkson-(ID) initwithselfname: (NSString *) selfname Andfathername: (NSString *) fathername andmothername: (NSString *) Mothername { if (self = [super init]) { _ Selfname = selfname; _fathername = Fathername; _mothername = Mothername; } return self;} @end
Test:
This implementation is the best and simplest, and subclasses no longer have to implement any code.
Another way of thinking, using the introverted function, is to achieve the following:
Ns_inline nsdictionary * descriptionmehtod (id obj) { nsmutabledictionary * dic = [Nsmutabledictionary dictionary];< C1/>u_int count; Get all properties objc_property_t *properties = class_copypropertylist ([obj class], &count); for (int i = 0; i<count; i++) { //Gets the name of the property const char * propertyname = Property_getname (Properties[i]); ID ObjValue; Convert a property to a bit string nsstring * key = [NSString stringwithutf8string:propertyname]; Value objvalue = [obj Valueforkey:key]; Write dictionary [dic setobject:objvalue Forkey:key]; } Nsdictionary * dictionary = [dic copy]; return dictionary;}
But in this way, not only does each class introduce a header file containing this function, but each class also implements its own description method and Debugdescription method;
In comparison, it is easier to implement a method that inherits from a class, and when there are errors, there are very few changes, as long as you modify the inherited class name.
From the two implementation of the idea can be seen, the less code, the better maintenance!
IOS Efficient Development-----Implement description Method (cont.)