如何在objective-c [duplicate]中声明C数组属性

时间:2023-01-15 14:54:07

This question already has an answer here:

这个问题在这里已有答案:

How to declare a C array of integers as a property on an objective-c class?

如何将一个C数组的整数声明为objective-c类的属性?

1 个解决方案

#1


6  

@property (nonatomic, assign) int *array;
...
// somewhere in your code 
    int *gg = malloc(10 * sizeof(int));
    gg[1] = 1;
    gg[0] = 2;
    self.array = gg;

UPDATE:

This is heap based array now to make sure it will not be deallocated.
But don't forget to free it in dealloc free(self.array)

这是基于堆的数组,现在确保它不会被释放。但不要忘记在dealloc free(self.array)中释放它

#1


6  

@property (nonatomic, assign) int *array;
...
// somewhere in your code 
    int *gg = malloc(10 * sizeof(int));
    gg[1] = 1;
    gg[0] = 2;
    self.array = gg;

UPDATE:

This is heap based array now to make sure it will not be deallocated.
But don't forget to free it in dealloc free(self.array)

这是基于堆的数组,现在确保它不会被释放。但不要忘记在dealloc free(self.array)中释放它