把结构体转换为NSValue类型 

作者: Jason_Wong | 来源:发表于2016-08-11 09:27 被阅读175次

//1.将常见的结构体包装成oc对象NSValue,然后存到NSArray中
CGPoint point1 = CGPointMake(2,0);
NSValue *value1 = [NSValue valueWithPoint:point1];
NSArray *arr1 =@[value1];
NSLog(@"%@",arr1);

//2.从NSValue类型中取得对应的结构体值
CGPoint point2 = [arr1[0] pointValue ]; NSLog(@"%f,%f",point2.x,point2.y);

//3.------把一个自定义的结构体转换为NSValue类型(仅作为了解)
typedefstruct{
inter;
intmonth;
intday;
} SteveDate;

//创建一个结构体
SteveDate date = {2010,10,20};

//把结构体转换为NSValue类型
NSValue *dateValue = [NSValue valueWithBytes:&date objCType:@encode(SteveDate)]; //将dateValue装到NSArray中 NSArray *array =@[dateValue]; //获取array中的NSValue对象 NSValue *value = array[0];
SteveDate newDate;

//调用NSValue的getValue方法拿到其中的数据
[value getValue:&newDate];
NSLog(@"year = %d, month = %d, day = %d",newDate.year, newDate.month, newDate.day);

如果上面的文章对您在以后的开发中能派上用场,请关注或点个喜欢吧,我会不定期的给您分享或推荐一些好的文章哦_

相关文章

网友评论

    本文标题:把结构体转换为NSValue类型 

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