objective-c - 将 C 数组传递给 Objective-C 方法

标签 objective-c c arrays

我想用尽可能少的代码创建一个整数数组,并将该数组传递给一个 Objective-C 方法。

我尝试了以下方法。 sequence 以数组形式开始并传递给 setLights: 但是当在方法中查看 sequence 时(通过断点)它不再数组。

*编辑:我不想使用 NSArray,因为整数的 NSArray 太冗长了:

使用 NSArray:

NSArray *sequence = [[NSArray alloc] initWithObjects: [NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4],[NSNumber numberWithInt:5],nil];

使用 C 数组:

int sequence[6] = {0,1,2,3,4,5};

我做错了什么?

- (IBAction)testLights:(id)sender {
    int sequence[6] = {0,1,2,3,4,5};
    //int *sequence[0][1][2][3][4][5]; //also tried this
    [self setLights:sequence];
}


- (void)setLights:(int *)sequence {

    UIImageView *light=[lgtArray objectAtIndex: sequence[0]];
    light.alpha = 0;
    [UIView animateWithDuration:0.3f
                     animations:^{
                         light.alpha = 1;
                     }completion:nil
     ];


}

enter image description here enter image description here

最佳答案

使用此语法传递数组:

- (void)setLights:(int[] )sequence

关于objective-c - 将 C 数组传递给 Objective-C 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23420826/

相关文章:

ios - 无法在框架项目的 Objective-C 代码中导入 Swift 类

Objective-C 重用 NSString 内存泄漏

python - 核心转储 : Extract all the global variables , 核心转储中的数据结构和子结构

c - 如何获取与 TCP 套接字关联的接口(interface)名称/索引?

c - 随机数 65- 90[a-z] 或 97-122[A-Z]

objective-c - 是否有用于将行插入/编辑到Core Data DB中的编辑器?

iphone - 自定义UITableViewCell在[super dealloc]上崩溃

c - C 中的 JSON 响应格式

c++ - 从函数返回静态固定大小的数组

java - 将 int 数组转换为 int