제가 작성한 작은 iPhone 응용 프로그램의 경우 NSMutableArray를 정렬하려고합니다.NSMutableArray를 사용자 정의 객체로 정렬하면 일부 객체를 덮어 씁니다.
두 가지 방법이 있지만 동일한 결과가 나타납니다. 배열을 정렬하면 일부 객체가 서로 겹쳐 쓰일 수 있습니다.
첫째, 여기 내 코드입니다 :
AppDelegate.h
NSMutableArray* highScores;
어딘가에 내려 내가 differen에서 액세스 할 수 있도록 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:)];
을 어떻게 든 망쳐 버린다. 내 highScores 배열, 나는 같은 점수와 이름으로 최고 점수의 amound를 얻을.
내가 뭘 잘못하고 있니?
실제로 이것이 sortedArrayUsingSelector를 호출하여 실제로 발생했는지 확인하십시오. 이전과 이후에 실제 highScores 배열을 보려고 했습니까? 중복 객체를 삽입 할 수있는 객체를 highScores 배열에 추가하는 코드에 어딘가 다른 것이 있습니까? 기억하십시오 - 집합이 아니므로 동일한 높은 점수 객체를 두 번 이상 삽입하면 배열에 여러 번 나타납니다 *하지만 점수는 배열에서 실제로 객체 참조이므로 항상 동일합니다. –
동의합니다. sgot의 바로 앞에있는 NSArray * sortedArray; 줄에 중단 점을 놓은 다음 gdb에 배열을 출력하십시오 :'po highScores' –