iOS开发IOS理论知识

IOS中的结构体保存到数组与读取

2017-03-21  本文已影响504人  leonardni
typedef struct
{
    CGFloat r;
    CGFloat g;
    CGFloat b;
}TestStruct;
 NSMutableArray *array = [NSMutableArray array];
//保存
    for (unsigned i = 0 ; i<5; i++) {
        TestStruct s;
        s.b = i;
        s.g = 1.0;
        s.r = 2.0f;
        NSValue *value = nil;
        value = [NSValue valueWithBytes:&s objCType:@encode(TestStruct)];
        [array addObject:value];
    }

//读取
    for (NSValue *value in array) {
        TestStruct s ;
        [value getValue:&s];
        NSLog(@"%f",s.b);
    }
上一篇下一篇

猜你喜欢

热点阅读