objective-c - 在 block 内访问 C 数组(可变数组计数)Objective-C

标签 objective-c struct core-graphics objective-c-blocks arrays

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/

相关文章:

ios - AutoLayout 在 ios 8.3 上无法正常工作

iphone - Core Graphics 文本比 Core Text 快得多——我觉得我错过了什么

iphone - 如何从自定义 UITabBar 启动模态视图?

ios - 如何在 iOS 中的 soap 请求后刷新 tableview

使用 CUDA 复制结构内的指针数组

c++ - 将 C++ 结构填充为 2 的幂

c++ - 从 vector 的元素初始化结构

ios - 来自不同状态的颜色的 UIButton 背景图像

ios - 在 Apple 的 Core Graphics/ImageIO 框架中的 CGImageDestination 中设置 JPEG 压缩质量时没有效果

objective-c - CITemperatureAndTint(CIFilter)的输入参数