<span style= "font-family:arial, Helvetica, Sans-serif;" > Sometimes we may need to determine whether an object has this property, then we can use this method </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" > #pragma mark-Dynamically determines whether a class contains a property </span>
+ (BOOL) Getvariablewithclass: (Class) MyClass varName: (NSString *) name{ unsigned int outcount, I; Ivar *ivars = class_copyivarlist (MyClass, &outcount); for (i = 0; i < Outcount; i++) { Ivar = ivars[i]; NSString *keyname = [NSString Stringwithcstring:ivar_getname (property) encoding:nsutf8stringencoding]; KeyName = [KeyName stringbyreplacingoccurrencesofstring:@ "_" withstring:@ ""]; if ([KeyName isequaltostring:name]) { return YES; } } return NO;}
Yes contains, no is not included. Pulling out some common methods can make the program more concise.
Learn from each other and thank you for reading
iOS dynamically determines whether an object contains a property