objective-c - 一步复制并排序 NSArray

标签 objective-c cocoa sorting nsarray

我正在尝试对 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/

相关文章:

algorithm - `stability`在排序算法中有什么意义?

ios - 检测 tableView reloadData 完成的新解决方案?

ios - 使用 CocoaPods 添加 CFNetwork

javascript - PHP 排序数组在 json_encode 后在 JS 中未排序

cocoa - 正在寻找 3d 引擎。 (麦克、 cocoa )

iphone - 在 CATiledLayer 中预加载/预显示图 block ?

javascript - 功能性 HTML 表格(排序、过滤、移动和切换列)

objective-c - ARC 适用于所有对象?

iOS 项目 : Static/Dynamic code analysis and call graphs

objective-c - alloc 和 allocWithZone :? 有什么区别