block 很好,但是编写 C 数组呢?
鉴于这种简化的情况:
CGPoint points[10];
[myArray forEachElementWithBlock:^(int idx) {
points[idx] = CGPointMake(10, 20); // error here
// Cannot refer to declaration with an array type inside block
}];
搜索一段时间后找到了这个可能的解决方案,把它放在一个结构中:
__block struct {
CGPoint points[100];
} pointStruct;
[myArray forEachElementWithBlock:^(int idx) {
pointStruct.points[idx] = CGPointMake(10, 20);
}];
这可行,但有一点限制,我必须动态创建 c 数组:
int count = [str countOccurencesOfString:@";"];
__block struct {
CGPoint points[count]; // error here
// Fields must have a constant size: 'variable length array in structure' extension will never be supported
} pointStruct;
如何在 block
中访问我的CGPoint
数组?
或
有没有可能,或者我是否必须重写 block 方法才能获得完整的功能?
最佳答案
另一个对我有用的简单答案如下:
CGPoint points[10], *pointsPtr;
pointsPtr = points;
[myArray forEachElementWithBlock:^(int idx) {
pointsPtr[idx] = CGPointMake(10, 20);
}];
关于objective-c - 在 block 内访问 C 数组(可变数组计数)Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5455592/