自动释放会释放我的非对象 c 数组吗?我想知道,因为也许只有对象知道它们的引用计数?这是我的代码:
-(int *)getCombination{
int xIndex = arc4random() % [self._num1 count] + 1;
int yIndex = arc4random() % [self._num2 count] + 1;
int *combination;
combination[0] = [[self._num1 objectAtIndex:xIndex]intValue];
combination[1] = [[self._num2 objectAtIndex:yIndex]intValue];
return combination;
}
这是我的 main() 函数:
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([YYAAppDelegate class]));
}
}
那么自动释放是否仅适用于对象,还是会从 getCombination 中释放我的 c 数组?
编辑:由于答案是否定的,自动释放不适用于 c 数组/指针,我使用了以下使用 NSArrays 的代码:
#import <Foundation/Foundation.h>
@interface Multiplication : NSObject
@property (strong, nonatomic) NSMutableArray *_combinations;
-(id)initArrays;
-(NSArray *)getCombination;
@end
#import "Multiplication.h"
@implementation Multiplication
@synthesize _combinations;
-(void)initializeArray{
self._combinations = [[NSMutableArray alloc]init];
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
NSNumber *x = [NSNumber numberWithInt:i];
NSNumber *y = [NSNumber numberWithInt:j];
[self._combinations addObject:[NSArray arrayWithObjects:x, y, [NSNumber numberWithInt:([x intValue] * [y intValue])], nil]];
}
}
}
-(NSArray *)getCombination{
if ([self._combinations count] == 0) {
[self initializeArray];
}
int index = arc4random() % [self._combinations count];
NSArray *arr = [self._combinations objectAtIndex:index];
[self._combinations removeObjectAtIndex:index];
return arr;
}
-(id)initArrays{
self = [super init];
if (self) {
[self initializeArray];
}
return self;
}
@end
顺便说一句,此功能应该提供一种方法,用于随机显示 10X10 乘法表中的每个组合,并在所有组合都显示且次数相等时重新启动。