動態映射objective-c的對象方法修改null 指標

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   strong   資料   

背景:目前iOS工程較多將json資料轉換成一個對象來儲存。假設這個對象有一個Attributes為NSString類型叫myName,這個Attributes一直是nil,建立這對象後忘記對它賦值。現在要通過一個函數來檢測他出來,再將他賦值為@“”;


另外,我不知道有多少個Attributes,只要是NSString類型而且Attributes是nil就賦值為@“”。


代碼如下:

//

//  GMCommonHelper.h

#import <Foundation/Foundation.h>

#import <objc/runtime.h>



@interface GMCommonHelper : NSObject


+(void)fixNilData:(id)kClass;


@end


//

//  GMCommonHelper.m

#import <AdSupport/AdSupport.h>


#import “GMCommonHelper.h"

@implementation GMCommonHelper


+(void)fixNilData:(id)kClass

{

    unsigned int propertyCount = 0;

    objc_property_t *properties = class_copyPropertyList([kClass class], &propertyCount);

    

    for (unsigned int i = 0; i < propertyCount; ++i) {

        objc_property_t property = properties[i];

        

        const char * name = property_getName(property);

        const char * attrs = property_getAttributes(property);

        NSString * utf8Name = [NSString stringWithUTF8String:name];

        NSString * utf8Attrs = [NSString stringWithUTF8String:attrs];

        id propertVal = [kClass valueForKey:utf8Name];

        if (!propertVal && [utf8Attrs hasPrefix:@"[email protected]\"NSString\""]) {

            [kClass setValue:@"" forKey:utf8Name];

        }

    }

    free(properties);

}



//———————————————————————


- (void)releaseProperties { unsigned int c = 0; objc_property_t *properties = class_copyPropertyList([self class], &c); for(unsigned int i = 0; i < c; i++) { objc_property_t property = properties[i]; NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)]; NSString *propertyType = [NSString stringWithUTF8String:property_getAttributes(property)]; if([propertyType hasPrefix:@"[email protected]"] // is an object && [propertyType rangeOfString:@",R,"].location == NSNotFound // not readonly ) { [self setValue:nil forKey:propertyName]; NSLog(@"%@.%@ = %@", NSStringFromClass(cls), propertyName, [self valueForKey:propertyName]); } } free(properties);}

//———————————————————————



If you only care about classes you can use isKindOfClass:, but if you want to deal with scalars you are correct that you need to use property_getAttributes(), it returns a string that encodes the type information. Below is a basic function that demonstrates what you need to do. For examples of encoding strings, look here.

unsigned int outCount, i;objc_property_t *properties = class_copyPropertyList([object class], &outCount);for (i = 0; i < outCount; i++) {    objc_property_t property = properties[i];    char *property_name = property_getName(property);    char *property_type = property_getAttributes(property);    switch(property_type[1]) {      case ‘f‘ : //float        break;      case ‘s‘ : //short        break;      case ‘@‘ : //ObjC object        //Handle different clases in here        break;    }}

Obvviously you will need to add all the types and classes you need to handle to this, it uses the normal ObjC @encode type。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.