反射类属性方法

广告数据实体:
// AdModel.h
@interface AdModel: NSObject
{
}

@property (nonatomic, assign) int       row;
@property (nonatomic, retain) NSString *picture;

@end
// AdModel.m
@implementation AdModel

@synthesize row      = _row;
@synthesize picture  = _picture;

- (void)dealloc
{
    [_picture release];
    [super dealloc];
}

@end


用SQLiteHelper将数据库内容返回NSDictionary

然后通过反射类属性将数据保存到实体集合



// NSObject+Add

- (NSArray *)getPropertyListByClass: (Class)clazz
{
    u_int count; 
    
    objc_property_t *properties  = class_copyPropertyList(clazz, &count);
    
    NSMutableArray *propertyArray = [NSMutableArray arrayWithCapacity:count];
    
    for (int i = 0; i < count ; i++) 
    { 
        const char* propertyName = property_getName(properties[i]);
        [propertyArray addObject: [NSString  stringWithUTF8String: propertyName]];
    } 
    
    free(properties);
    
    return propertyArray;
}


#define AD_INFO_QUERY  \
@"SELECT * FROM ad ORDER BY row"

- (NSArray *)queryAdInfo
{    
    NSMutableArray *adEntityArray   = nil;
    NSUInteger     count            = [[_sqliteHelper countRowsAtTable: @"ad"] intValue];

    NSString     *conn   = [NSString stringWithFormat: AD_INFO_QUERY];
    SQLiteHelper *obj    = [_sqliteHelper getQuery: conn];

    if (obj != nil)
    {
        adEntityArray         = [NSMutableArray arrayWithCapacity: count];
        NSArray *propertyList = [self getPropertyListByClass: [EntityAdInfo class]];

        for (NSMutableDictionary *dic in obj)
        {
            EntityAdInfo *adEntity = [[EntityAdInfo alloc] init];
            
            for (NSString *key in propertyList)
            {
                 [adEntity setValue: [dic objectForKey: key] forKey: key];
            }

            [adEntityArray addObject: adEntity];
            [adEntity release];
        }
    }
    return adEntityArray;
}
阅读更多
个人分类: Objective-C代码片段
下一篇获取UIAppliction Delegate类指针
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭