我正在尝试对 NSArray 进行排序。 不幸的是,该数组似乎保持为空。 我从一个带有名为“gaussianPopulation”的正态分布数据的 NSMutableArray 开始,然后将其复制并排序到 NSArray“sortedData”中。
我在这里查看了类似的帖子:Sorting NSArray and returning NSArray?
但我仍然缺少一些东西......
人口.h:
#define ARC4RANDOM_MAX 0x100000000
#import <Foundation/Foundation.h>
@interface Population : NSObject
@property NSMutableArray *totalPopulation;
@property NSMutableArray *gaussianPopulation;
-(void)createPopulation; // test purpose to create uniform distribution. not used anymore.
-(void)createGaussianPopulation;
-(double)calculateAndersonDarling;
@end
和人口.m:
#import "Population.h"
#include <math.h>
@implementation Population
-(void)createPopulation
{
_totalPopulation = [[NSMutableArray alloc] init];
srandom((int)time(NULL));
NSNumber *randomNumber;
for (int i = 0 ; i < 10000 ; i++)
{
randomNumber = [[NSNumber alloc] initWithInt:random()];
[_totalPopulation addObject:randomNumber];
NSLog(@"%@", randomNumber);
}
}
-(void)createGaussianPopulation
{
for (int i = 0; i < 5000 ; i++)
{
double x1, x2, w, y1, y2;
do
{
x1 = 2.0 * ((double)arc4random() / ARC4RANDOM_MAX) - 1.0;
x2 = 2.0 * ((double)arc4random() / ARC4RANDOM_MAX) - 1.0;
w = x1 * x1 + x2 * x2;
} while (w >= 1.0);
w = sqrt((-2.0 * log(w))/w);
y1 = x1 * w;
y2 = x2 * w;
NSNumber *value1 = [NSNumber numberWithDouble:y1];
NSNumber *value2 = [NSNumber numberWithDouble:y2];
[self.gaussianPopulation addObject:value1];
[self.gaussianPopulation addObject:value2];
//NSLog(@"%@ %@", value1, value2);
NSString *pathToDesktop = [NSString stringWithFormat:@"/Users/%@/Desktop", NSUserName()];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/testfile.tst", pathToDesktop];
//create content
NSString *content = [NSString stringWithFormat:@"%@\n%@ \n", [value1 stringValue], [value2 stringValue]];
NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:fileName];
[myHandle seekToEndOfFile];
[myHandle writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
}
}
-(double)calculateAndersonDarling
{
NSLog((@"in method calculateAndersonDarling"));
NSLog(@"%i", [self.gaussianPopulation count]);
for (id eachObject in self.gaussianPopulation)
{
NSLog(@"%@", eachObject);
}
NSArray *sortedData = [NSArray arrayWithArray:[self.gaussianPopulation sortedArrayUsingSelector:@selector(compare:)]];
//NSArray *sortedData = [self.gaussianPopulation sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%i", [sortedData count]);
for (id eachObject in sortedData)
{
NSLog(@"%@", eachObject);
}
return 0.0; //return value to be done later
}
@end
正如你所看到的(注释行所在的位置)我尝试了不同的方法。 但排序数据数组似乎仍为空。 通过 NSLog 报告的大小为零,并且没有内容的输出。
如有任何帮助,我们将不胜感激。
以防万一:
#import <Foundation/Foundation.h>
#import "Population.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Population *myPopulation;
myPopulation = [[Population alloc] init];
[myPopulation createGaussianPopulation];
[myPopulation calculateAndersonDarling];
}
return 0;
}
NSLog 输出:
2014-09-12 16:46:44.647 Statistik[4158:303] Hello, World!
2014-09-12 16:46:45.154 Statistik[4158:303] in method calculateAndersonDarling
2014-09-12 16:46:45.154 Statistik[4158:303] 0
2014-09-12 16:46:45.155 Statistik[4158:303] 0
Program ended with exit code: 0
显然在calculateAndersonDarling方法中数组gaussianPopulation已经是空的,或者我对数组大小的计算是错误的? 但为什么它会丢失内容???
最佳答案
您忘记在向 gaussianPopulation 数组添加元素之前分配该数组。在 Objective-C 中,你可以调用 nil 对象的方法,它不会有任何效果(不会崩溃,不会警告)。这就是为什么有时很难发现这些错误。只需在方法开头初始化数组即可:
- (void)createGaussianPopulation
{
self.gaussianPopulation = [NSMutableArray array];
...
关于objective-c - 一步复制并排序 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25810059/