Runtime 簡單了解

copy 類中的 property

/** 
 * Describes the properties declared by a class.
 * 
 * @param cls The class you want to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If \e outCount is \c NULL, the length is not returned.        
 * 
 * @return An array of pointers of type \c objc_property_t describing the properties 
 *  declared by the class. Any properties declared by superclasses are not included. 
 *  The array contains \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
 * 
 *  If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0.
 */
OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);

返回 property 的 name

/** 
 * Returns the name of a property.
 * 
 * @param property The property you want to inquire about.
 * 
 * @return A C string containing the property's name.
 */
OBJC_EXPORT const char *property_getName(objc_property_t property) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);

實戰(zhàn) 通過類名得到類中的屬性名

// 通過類名得到類中的屬性名
+ (NSArray *)getPropertyNameArrayWithClassName:(NSString *)className {
    NSMutableArray *array = [NSMutableArray array];
    unsigned int count;
    objc_property_t *props = class_copyPropertyList(NSClassFromString(className), &count);
    
    for (int i = 0; i < count; i ++) {
        objc_property_t property = props[i];
        const char *name = property_getName(property);
        
        NSString *propertyName = [NSString stringWithUTF8String:name];
        [array addObject:propertyName];
    }
    
    free(props);
    
    return array;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • 最近沒什么任務(wù),想了解一下Swift中Runtime的一些知識,所以網(wǎng)上找了不少相關(guān)文章看了一下,算是了解了一些S...
    哈南閱讀 1,335評論 2 4
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,739評論 18 399
  • 隨意信息 ·觀點信息 ·過程信息 ·具體信息 ·抽象信息
    Bebevino閱讀 50評論 0 0
  • 剛開始到這個新的城市,來到這個新的單位,說實話是懼怕的,但是沒有辦法,人不能總是沒有突破,總是習(xí)慣滿足吧!有時候適...
    張銘小鑠閱讀 373評論 2 2