我正在尝试编写一个类来在 Objective-c 中操作矩阵,最初我使用的是 NSArrays 和 NSNumbers,但是这导致我正在做的事情的代码非常慢(模拟双摆)所以我决定尝试重新将我的数组写为 c++ 数组,但是我遇到了我不知道如何解决的问题。
-[SMFiniteDifferenceHelper matrixHandler]: unrecognized selector sent to instance 0x100107cb0
即使我的方法在头文件中并在类中实现,运行此代码也会给我一个错误。但是,如果我只保留其中一个array[][]
值未设置,则代码运行良好......
休息
self.matrixHandler = [[SMMatrixHandler alloc] init];
double **array1 = (double **)malloc(3*2*sizeof(double)); // height
for (int i = 0; i < 3; ++i)
array1[i] = new double[2]; // width
array1[0][0] = 1.0;
array1[1][0] = 2.0;
array1[0][1] = 1.0;
array1[1][1] = 0.0;
array1[0][2] = 4.0;
array1[1][2] = 1.0;
NSLog(@"array 1 : \n%@",[self.matrixHandler stringForMatrix:array1 ofSize:CGSizeMake(2, 3)]);
工作正常
self.matrixHandler = [[SMMatrixHandler alloc] init];
double **array1 = (double **)malloc(3*2*sizeof(double)); // height
for (int i = 0; i < 3; ++i)
array1[i] = new double[2]; // width
array1[0][0] = 1.0;
array1[1][0] = 2.0;
array1[0][1] = 1.0;
array1[1][1] = 0.0;
// array1[0][2] = 4.0;
array1[1][2] = 1.0;
NSLog(@"array 1 : \n%@",[self.matrixHandler stringForMatrix:array1 ofSize:CGSizeMake(2, 3)]);
该方法stringForArray
如下所示:
- (NSString *)stringForMatrix:(double **)m ofSize:(CGSize)s {
NSString *string = @"";
for (int i = 0; i < s.height; i++) {
for (int j = 0; j < s.width; j++) {
string = [string stringByAppendingFormat:@"%f ",m[j][i]];
}
string = [string stringByAppendingString:@"\n"];
}
return string;
}