数字、字符串和集合操作全解析
在编程中,对数字、字符串和集合的操作是非常基础且重要的部分。下面将详细介绍一些常见的数据结构及其操作方法。
1. 存储结构体到数组
在某些情况下,我们需要将结构体存储到数组中,但由于数组只能存储对象,所以需要先将结构体转换为对象。以下是一个将 CGPoint 结构体添加到可变数组 touchPoints 的示例代码:
CGPoint myPoint;
NSValue *pointObj;
NSMutableArray *touchPoints = [NSMutableArray array];
...
myPoint.x = 100; // set the point to (100, 200)
myPoint.y = 200;
...
pointObj = [NSValue valueWithPoint: myPoint]; // make it an object
[touchPoints addObject: pointObj];
若要从数组中获取最后一个点并将其转换回 CGPoint 结构体,可以使用以下代码:
myPoint = [[touchPoints lastObject] pointValue];
这里使用了 NSValue 的 valueWit
超级会员免费看
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



