First, the use of the introduction
Project sometimes, you will encounter some special processing, want to get the name of this instance in code according to the reference of an instance. For example, when dealing with the coordinates of the view, we configure the coordinates of the UIView to the plist file, we can set a key, and then use this key to get the coordinates in the configuration file, and so on. Is there a simpler way, or I just want to simply use the variable name of the instance variable as key. Here's a simple way to get an instance variable name from a reference to an instance variable.
Reprint please retain original link: http://my.oschina.net/taptale/blog/110626
Ii. Citation of documents
In the first step, we need to introduce the header files we need to refer to the following code in the classes we need to use
?
| 1 |
#import <objc/runtime.h> |
Three, operating principle
We can look at the detailed runtime usage and API from Apple's official development documentation, and the official does not directly provide a way to get instance variable names based on instance references, so we need to implement them ourselves.
In the official API we can find the following methods
(1) Describes the instance variables declared by a class.
Ivar * Class_copyivarlist (class cls, unsigned int *outcount)
(2) Reads The value of a instance variable in an object.
ID Object_getivar (ID object, Ivar Ivar)
(3) Returns The name of an instance variable.
const char * ivar_getname (Ivar Ivar)
Based on the API above, we can get the Ivar of all variables based on the owner of the variable, iterate over all the Ivar, and do the following for each iteration
- Based on the API in (2), we can get a reference to the instance variable that corresponds to the Ivar in the current iteration
- Compare the obtained instance variable with the address of the instance variable passed over
- If the address is the same, the current Ivar is the Ivar of the instance variable passed over, you can get the name of the variable by (3) and return
Four, the code
(1) According to the above principle we can get the first version of the Code, as follows:
?
| 12345678910111213141516 |
- (NSString *)nameWithInstance:(id)instance{ unsigned int numIvars = 0; NSString *key=nil; Ivar * ivars = class_copyIvarList([self.target class], &numIvars); for(int i = 0; i < numIvars; i++) { Ivar thisIvar = ivars[i]; if ((object_getIvar(self.target, thisIvar) == instance)) { key = [NSString stringWithUTF8String:ivar_getName(thisIvar)]; break; } } free(ivars); return key;} |
(2) in the test found to reach the above if statement, the program sometimes will crash, after detailed testing found that each iteration to non-objective-c objects, such as the basic data type, BOOL, int, float will be error.
The reason is that in Object_getivar this method, when encountering non-objective-c objects, and directly crash, and then view the official explanation
the value of the instance variable specified by Ivar, or nil if object is nil.
Does not explicitly give crash when encountering non-objects, and does not return nil
We need to make a correction, and when we encounter non-objective-c, we need to skip execution. The final code is as follows:
?
| 123456789101112131415161718192021 |
- (NSString *)nameWithInstance:(id)instance{ unsigned int numIvars = 0; NSString *key=nil; Ivar * ivars = class_copyIvarList([self.target class], &numIvars); for(int i = 0; i < numIvars; i++) { Ivar thisIvar = ivars[i]; const char *type = ivar_getTypeEncoding(thisIvar); NSString *stringType = [NSString stringWithCString:type encoding:NSUTF8StringEncoding]; if (![stringType hasPrefix:@"@"]) { continue; } if ((object_getIvar(self.target, thisIvar) == instance)) { key = [NSString stringWithUTF8String:ivar_getName(thisIvar)]; break; } } free(ivars); return key;}
|
iOS Advanced Tutorial 2: Reflection Gets the variable name based on the reference to the variable