美文网首页
结构体存放到数组

结构体存放到数组

作者: Juno_o | 来源:发表于2016-05-13 10:57 被阅读49次
    
    typedef struct {
        
        CGFloat r;
        CGFloat g;
        CGFloat b;
        
    }TestStruct;
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        NSMutableArray *array = [NSMutableArray array];
        //保存
        for (int 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);
        }
    }
    

    相关文章

      网友评论

          本文标题:结构体存放到数组

          本文链接:https://www.haomeiwen.com/subject/llaprttx.html