第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

獲取Objective-C中的對(duì)象屬性列表

獲取Objective-C中的對(duì)象屬性列表

慕標(biāo)5832272 2019-08-15 15:13:43
獲取Objective-C中的對(duì)象屬性列表如何在Objective-C中獲取給定對(duì)象屬性的列表(以NSArray或的形式NSDictionary)?想象一下下面的場(chǎng)景:我已經(jīng)定義了一個(gè)只擴(kuò)展的父類NSObject,它將a NSString,a BOOL和一個(gè)NSData對(duì)象作為屬性。然后我有幾個(gè)擴(kuò)展這個(gè)父類的類,每個(gè)類都添加了很多不同的屬性。有沒(méi)有什么方法可以在父類上實(shí)現(xiàn)一個(gè)遍歷整個(gè)對(duì)象的實(shí)例方法,然后返回NSArray每個(gè)(子)類屬性的一個(gè),因?yàn)镹SStrings它不在父類上,所以我以后可以使用這些NSString對(duì)于KVC?
查看完整描述

3 回答

?
拉丁的傳說(shuō)

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊

我自己設(shè)法得到了答案。通過(guò)使用Obj-C運(yùn)行時(shí)庫(kù),我可以按照我想要的方式訪問(wèn)屬性:

- (void)myMethod {
    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for(i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        const char *propName = property_getName(property);
        if(propName) {
            const char *propType = getPropertyType(property);
            NSString *propertyName = [NSString stringWithCString:propName
                                                                encoding:[NSString defaultCStringEncoding]];
            NSString *propertyType = [NSString stringWithCString:propType
                                                                encoding:[NSString defaultCStringEncoding]];
            ...
        }
    }
    free(properties);}

這要求我制作一個(gè)'getPropertyType'C函數(shù),它主要取自Apple代碼示例(現(xiàn)在不能記住確切的來(lái)源):

static const char *getPropertyType(objc_property_t property) {
    const char *attributes = property_getAttributes(property);
    char buffer[1 + strlen(attributes)];
    strcpy(buffer, attributes);
    char *state = buffer, *attribute;
    while ((attribute = strsep(&state, ",")) != NULL) {
        if (attribute[0] == 'T') {
            if (strlen(attribute) <= 4) {
                break;
            }
            return (const char *)[[NSData dataWithBytes:(attribute + 3) length:strlen(attribute) - 4] bytes];
        }
    }
    return "@";}


查看完整回答
反對(duì) 回復(fù) 2019-08-15
?
明月笑刀無(wú)情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

它實(shí)際上并不總是以0結(jié)束字符串。這可能會(huì)導(dǎo)致意外的結(jié)果,比如在嘗試將其轉(zhuǎn)換為UTF8時(shí)崩潰(我實(shí)際上有一個(gè)非常令人討厭的崩潰因?yàn)檫@個(gè)。很有趣的調(diào)試它^^)。我通過(guò)實(shí)際從屬性獲取NSString然后調(diào)用cStringUsingEncoding來(lái)修復(fù)它:這就像現(xiàn)在的魅力。(也適用于ARC,至少對(duì)我而言)

所以這是我現(xiàn)在的代碼版本:

// PropertyUtil.h#import @interface PropertyUtil : NSObject+ (NSDictionary *)classPropsFor:(Class)klass;@end// PropertyUtil.m#import "PropertyUtil.h"#import <objc/runtime.h>@implementation PropertyUtilstatic const char *getPropertyType(objc_property_t property) {
    const char *attributes = property_getAttributes(property);
    //printf("attributes=%s\n", attributes);
    char buffer[1 + strlen(attributes)];
    strcpy(buffer, attributes);
    char *state = buffer, *attribute;
    while ((attribute = strsep(&state, ",")) != NULL) {
        if (attribute[0] == 'T' && attribute[1] != '@') {
            // it's a C primitive type:
            /*
             if you want a list of what will be returned for these primitives, search online for
             "objective-c" "Property Attribute Description Examples"
             apple docs list plenty of examples of what you get for int "i", long "l", unsigned "I", struct, etc.
             */
            NSString *name = [[NSString alloc] initWithBytes:attribute + 1 length:strlen(attribute) - 1 encoding:NSASCIIStringEncoding];
            return (const char *)[name cStringUsingEncoding:NSASCIIStringEncoding];
        }
        else if (attribute[0] == 'T' && attribute[1] == '@' && strlen(attribute) == 2) {
            // it's an ObjC id type:
            return "id";
        }
        else if (attribute[0] == 'T' && attribute[1] == '@') {
            // it's another ObjC object type:
            NSString *name = [[NSString alloc] initWithBytes:attribute + 3 length:strlen(attribute) - 4 encoding:NSASCIIStringEncoding];
            return (const char *)[name cStringUsingEncoding:NSASCIIStringEncoding];
        }
    }
    return "";}+ (NSDictionary *)classPropsFor:(Class)klass{
    if (klass == NULL) {
        return nil;
    }

    NSMutableDictionary *results = [[NSMutableDictionary alloc] init];

    unsigned int outCount, i;
    objc_property_t *properties = class_copyPropertyList(klass, &outCount);
    for (i = 0; i < outCount; i++) {
        objc_property_t property = properties[i];
        const char *propName = property_getName(property);
        if(propName) {
            const char *propType = getPropertyType(property);
            NSString *propertyName = [NSString stringWithUTF8String:propName];
            NSString *propertyType = [NSString stringWithUTF8String:propType];
            [results setObject:propertyType forKey:propertyName];
        }
    }
    free(properties);

    // returning a copy here to make sure the dictionary is immutable
    return [NSDictionary dictionaryWithDictionary:results];}@end


查看完整回答
反對(duì) 回復(fù) 2019-08-15
  • 3 回答
  • 0 關(guān)注
  • 701 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)