【问题标题】:copy and sort an NSArray in one step一步复制和排序 NSArray
【发布时间】:2014-09-12 13:58:57
【问题描述】:

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

和 Population.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

如您所见(注释行所在的位置),我尝试了不同的方法。 但似乎 sortedData 数组仍然为空。 通过 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中的Array gaussianPopulation已经为空,还是我对数组大小的计算错误? 但是为什么它会丢失它的内容呢???

【问题讨论】:

  • 你能在gaussianPopulation中显示一些值吗?
  • 同样在排序行之前记录 self.gaussianPopulation 中的内容。还有你里面有什么样的物品?它们是 NSNumbers 还是其他类型的对象?
  • 还有 compare: 方法吗?
  • Kaan:很好的提示... gaussianPopulation 在这个方法中它也已经是空的了......所以没有什么可以复制......
  • 房产怎么样?它是如何合成的?您是否尝试过使用 [self.gaussianPopulation addObject:value1];而不是 [_gaussianPopulation addObject:value1]; ?

标签: objective-c cocoa sorting nsarray


【解决方案1】:

在向 gaussianPopulation 数组添加元素之前忘记分配它。在 Objective-C 中,您可以在 nil 对象上调用方法,它不会产生任何影响(没有崩溃,没有警告)。这就是为什么有时很难看到这些错误。只需在方法的开头初始化数组即可:

- (void)createGaussianPopulation
{
    self.gaussianPopulation = [NSMutableArray array];
    ...

【讨论】:

  • 非常感谢。你是绝对正确的。很高兴知道,我猜当我添加元素时,c++ 不会编译或至少崩溃。当然,使用 NSLog() 字符串而不是数组的内容,这不是正确的方法。
猜你喜欢
  • 1970-01-01
  • 2012-01-27
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多