相关疑难解决方法(0)

在块内访问C数组(可变数组计数)Objective-C

块很好但是编写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
}];
Run Code Online (Sandbox Code Playgroud)

在搜索了一段时间后找到了这个可能的解决方案,把它放在一个结构中:

__block struct {
    CGPoint points[100];
} pointStruct;

[myArray forEachElementWithBlock:^(int idx) {
    pointStruct.points[idx] = CGPointMake(10, 20);
}];
Run Code Online (Sandbox Code Playgroud)

这会起作用,但我有一点限制,我必须动态创建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;
Run Code Online (Sandbox Code Playgroud)

如何CGPoint在一个内部访问我的阵列 …

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

18
推荐指数
2
解决办法
7601
查看次数