gpt4 book ai didi

iphone - 使用自定义对象对 NSMutableArray 进行排序 'overwrites' 一些对象

转载 作者:行者123 更新时间:2023-12-03 17:45:43 25 4
gpt4 key购买 nike

对于我正在制作的一个小型 iPhone 应用程序,我想对 NSMutableArray 进行排序。

我发现有两种方法可以做到这一点,但它们都会产生相同的结果。对数组进行排序将导致某些对象相互“覆盖”。

首先,这是我的代码:

AppDelegate.h

NSMutableArray* highScores;

在 AppDelegate.h 的某个地方,我还将此变量设为属性,以便我可以从不同的类访问它:

@property (retain, nonatomic) NSMutableArray* highScores;

当我的应用程序启动时,我从文件中读取高分并将其导入到我的 NSMutableArray 中。

AppDelegate.m

NSMutableData* data = [NSData dataWithContentsOfFile:highScoresPath];
NSKeyedUnarchiver* decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
self.highScores = [decoder decodeObjectForKey:@"highscoresArray"];

我在此 NSMutableArray 中存储的对象来自 HighScore 类型。

HighScore.h

@interface HighScore : NSObject {

int score;
int roundsPlayed;
int wrongAnswers;

NSString* name;

NSDate* datetime;
}

@property int score;
@property int roundsPlayed;
@property int wrongAnswers;
@property (nonatomic, copy) NSDate* datetime;

@property (nonatomic, copy) NSString* name;

- (id) init;
- (void) update:(int)roundScore:(BOOL) correct;

@end

HighScore.m

#import "HighScore.h"

@implementation HighScore

@synthesize score, roundsPlayed, wrongAnswers, name, datetime;

- (id) init
{
self.name = @"";
self.score = 0;
self.roundsPlayed = 0;
self.wrongAnswers = 0;

self.datetime = [NSDate date];

return self;
}

- (void) update:(int)roundScore:(BOOL) correct
{
self.score += roundScore;

if (!correct)
self.wrongAnswers++;

self.roundsPlayed++;
self.datetime = [NSDate date];
}

- (id) initWithCoder:(NSCoder *) decoder
{
self.name = [[decoder decodeObjectForKey:@"name"] retain];
self.score = [decoder decodeIntForKey:@"score"];
self.roundsPlayed = [decoder decodeIntForKey:@"roundsPlayed"];
self.wrongAnswers = [decoder decodeIntForKey:@"wrongAnswers"];
self.datetime = [[decoder decodeObjectForKey:@"datetime"] retain];

return self;
}

- (void) encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeInt:self.score forKey:@"score"];
[encoder encodeInt:self.roundsPlayed forKey:@"roundsPlayed"];
[encoder encodeInt:self.wrongAnswers forKey:@"wrongAnswers"];
[encoder encodeObject:self.datetime forKey:@"datetime"];
}

- (NSComparisonResult) compareHighscore:(HighScore*) h
{

return [[NSNumber numberWithInt:self.score] compare:[NSNumber numberWithInt:h.score]];
}

@end

现在,当我尝试使用以下代码对数组进行排序时:

NSArray *sortedArray;
sortedArray = [highScores sortedArrayUsingSelector:@selector(compareHighscore:)];

它以某种方式搞乱了我的高分数组,我得到了 X 大量具有相同分数和名称的高分。

我做错了什么?

最佳答案

我注意到在您的 initWithCoder: 方法中,您没有这样做:

if (self = [super initWithCoder:coder]) {
// Decode your stuff here
}

与常规的 init 方法相同。需要调用[super init]

此外,由于您将字符串属性定义为 copy 并且正在使用属性语法,因此无需保留它们。它们将由合成访问器为您保留。

否则,你的代码对我来说看起来不错。请记住:每个 init 方法必须始终调用 superinit... 方法。

关于iphone - 使用自定义对象对 NSMutableArray 进行排序 'overwrites' 一些对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com