English 中文(简体)
编制一个高分清单
原标题:Creating a highscore list

i 在你试图尽可能分得点的场合,我正在尝试制定一份高分清单。 我在这样做时,如果将分数定为2、2、3、4等,就算出你知道的分数。 但是,使用单体字体的计算方法是:

display score code: Code:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingPathComponent:@"highscore.save"];

NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init];

myDict = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSString *nssScore1 = [myDict objectForKey:@"Score1"];
NSString *nssName1 = [myDict objectForKey:@"Name1"];
NSString *nssScore2 = [myDict objectForKey:@"Score2"];
NSString *nssName2 = [myDict objectForKey:@"Name2"];
NSString *nssScore3 = [myDict objectForKey:@"Score3"];
NSString *nssName3 = [myDict objectForKey:@"Name3"];
NSString *nssScore4 = [myDict objectForKey:@"Score4"];
NSString *nssName4 = [myDict objectForKey:@"Name4"];
NSString *nssScore5 = [myDict objectForKey:@"Score5"];
NSString *nssName5 = [myDict objectForKey:@"Name5"];
NSString *nssScore6 = [myDict objectForKey:@"Score6"];
NSString *nssName6 = [myDict objectForKey:@"Name6"];
NSString *nssScore7 = [myDict objectForKey:@"Score7"];
NSString *nssName7 = [myDict objectForKey:@"Name7"];
NSString *nssScore8 = [myDict objectForKey:@"Score8"];
NSString *nssName8 = [myDict objectForKey:@"Name8"];
NSString *nssScore9 = [myDict objectForKey:@"Score9"];
NSString *nssName9 = [myDict objectForKey:@"Name9"];
NSString *nssScore10 = [myDict objectForKey:@"Score10"];
NSString *nssName10 = [myDict objectForKey:@"Name10"];
highestscore = [nssScore1 intValue];
score1i = [nssScore1 intValue];
score2i = [nssScore2 intValue];
score3i = [nssScore3 intValue];
score4i = [nssScore4 intValue];
score5i = [nssScore5 intValue];
score6i = [nssScore6 intValue];
score7i = [nssScore7 intValue];
score8i = [nssScore8 intValue];
score9i = [nssScore9 intValue];
score10i = [nssScore10 intValue];

if(score1i > 0){
    NSString *str1 = [NSString stringWithFormat:@"1. %@ | %i points", nssName1, score1i];
    score1.text = str1;
} else {
    score1.text = @"No Scores available yet";
}
if(score2i > 0){
    NSString *str2 = [NSString stringWithFormat:@"2. %@ | %i points", nssName2, score2i];
    score2.text = str2;
} else {
    [score2 setHidden:1];
}
if(score3i > 0){
    NSString *str3 = [NSString stringWithFormat:@"3. %@ | %i points", nssName3, score3i];
    score3.text = str3;
} else {
    [score3 setHidden:1];
}
if(score4i > 0){
    NSString *str4 = [NSString stringWithFormat:@"4. %@ | %i points", nssName4, score4i];
    score4.text = str4;
} else {
    [score4 setHidden:1];
}
if(score5i > 0){
    NSString *str5 = [NSString stringWithFormat:@"5. %@ | %i points", nssName5, score5i];
    score5.text = str5;
} else {
    [score5 setHidden:1];
}
if(score6i > 0){
    NSString *str6 = [NSString stringWithFormat:@"6. %@ | %i points", nssName6, score6i];
    score6.text = str6;
} else {
    [score6 setHidden:1];
}
if(score7i > 0){
    NSString *str7 = [NSString stringWithFormat:@"7. %@ | %i points", nssName7, score7i];
    score7.text = str7;
} else {
    [score7 setHidden:1];
}
if(score8i > 0){
    NSString *str8 = [NSString stringWithFormat:@"8. %@ | %i points", nssName8, score8i];
    score8.text = str8;
} else {
    [score8 setHidden:1];
}
if(score9i > 0){
    NSString *str9 = [NSString stringWithFormat:@"9. %@ | %i points", nssName9, score9i];
    score9.text = str9;
} else {
    [score9 setHidden:1];
}
if(score10i > 0){
    NSString *str10 = [NSString stringWithFormat:@"10. %@ | %i points", nssName10, score10i];
    score10.text = str10;
} else {
    [score10 setHidden:1];
}

除记分法外:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingPathComponent:@"highscore.save"];

NSMutableDictionary* myDict1 = [[NSMutableDictionary alloc] init];

myDict1 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSString *nssScore1 = [myDict1 objectForKey:@"Score1"];
NSString *nssName1 = [myDict1 objectForKey:@"Name1"];
NSString *nssScore2 = [myDict1 objectForKey:@"Score2"];
NSString *nssName2 = [myDict1 objectForKey:@"Name2"];
NSString *nssScore3 = [myDict1 objectForKey:@"Score3"];
NSString *nssName3 = [myDict1 objectForKey:@"Name3"];
NSString *nssScore4 = [myDict1 objectForKey:@"Score4"];
NSString *nssName4 = [myDict1 objectForKey:@"Name4"];
NSString *nssScore5 = [myDict1 objectForKey:@"Score5"];
NSString *nssName5 = [myDict1 objectForKey:@"Name5"];
NSString *nssScore6 = [myDict1 objectForKey:@"Score6"];
NSString *nssName6 = [myDict1 objectForKey:@"Name6"];
NSString *nssScore7 = [myDict1 objectForKey:@"Score7"];
NSString *nssName7 = [myDict1 objectForKey:@"Name7"];
NSString *nssScore8 = [myDict1 objectForKey:@"Score8"];
NSString *nssName8 = [myDict1 objectForKey:@"Name8"];
NSString *nssScore9 = [myDict1 objectForKey:@"Score9"];
NSString *nssName9 = [myDict1 objectForKey:@"Name9"];

self.String = textname.text;
NSString *nameString = String;
NSString *nssHighScore = [NSString stringWithFormat:@"%i", highestscore];
NSString *nssIdentifier = [NSString stringWithFormat:@"%@", nameString];

NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init];

[myDict setObject:nssIdentifier forKey:@"Name1"];
[myDict setObject:nssHighScore forKey:@"Score1"];
[myDict setObject:nssName1 forKey:@"Name2"];
[myDict setObject:nssScore1 forKey:@"Score2"];
if(nssScore2 > 0){
[myDict setObject:nssName2 forKey:@"Name3"];
[myDict setObject:nssScore2 forKey:@"Score3"];
}
if(nssScore3 > 0){
[myDict setObject:nssName3 forKey:@"Name4"];
[myDict setObject:nssScore3 forKey:@"Score4"];
}
if(nssScore4 > 0){
[myDict setObject:nssName4 forKey:@"Name5"];
[myDict setObject:nssScore4 forKey:@"Score5"];
}
if(nssScore5 > 0){
[myDict setObject:nssName5 forKey:@"Name6"];
[myDict setObject:nssScore5 forKey:@"Score6"];
}
if(nssScore6 > 0){
[myDict setObject:nssName6 forKey:@"Name7"];
[myDict setObject:nssScore6 forKey:@"Score7"];
}
if(nssScore7 > 0){
[myDict setObject:nssName7 forKey:@"Name8"];
[myDict setObject:nssScore7 forKey:@"Score8"];
}
if(nssScore8 > 0){
[myDict setObject:nssName8 forKey:@"Name9"];
[myDict setObject:nssScore8 forKey:@"Score9"];
}
if(nssScore9 > 0){
[myDict setObject:nssName9 forKey:@"Name10"];
[myDict setObject:nssScore9 forKey:@"Score10"];
}

NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath1 = [paths1 objectAtIndex:0];
NSString *path1 = [documentPath1 stringByAppendingPathComponent:@"highscore.save"];

[NSKeyedArchiver archiveRootObject:myDict toFile:path1];

如今,已经拯救了1号高分,取代了原来的第1号,但前一个数字刚刚消失,另一个(3、4、5等)没有显示。 你们能够发现错误或遗漏的东西? 感谢!

问题回答

首先。 你们正在这样做是错误的!

Just create a high-score manager class using an array! Here is an example:

<>高分>。

@interface HighScore : NSObject<NSCoding>

+(id) highScoreWithName:(NSString *) name score:(int) score;

-(id) initWithName:(NSString *) name score:(int) score;

@property (assign) int score;
@property (copy)   NSString *name;

@end

www.un.org/spanish/ecosoc 高分:

@implementation HighScore

@synthesize score = _score, name = _name;

+(id) highScoreWithName:(NSString *)name score:(int)score
{
    return [[self alloc] initWithName:name score:score];
}

-(id) initWithName:(NSString *)name score:(int)score
{
    if ((self = [super init]))
    {
        self.name = name;
        self.score = score;
    }

    return self;
}

-(id) initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super init]))
    {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.score = [aDecoder decodeIntForKey:@"score"];
    }

    return self;
}

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

-(NSString *) description
{
    return [NSString stringWithFormat:@"%@ - %@: %i", [super description], self.name, self.score];
}

@end

HighScoreManager.h

@interface HighScoreManager : NSObject {
    NSMutableArray *_scores;
}

+(HighScoreManager *) instance;

-(void) addHighScore:(HighScore *) score;
-(void) clearHighScores;

-(HighScore *) bestScore;
-(NSArray *) sortedScores;

@end

HighScoreManager.m

@implementation HighScoreManager

+(HighScoreManager *) instance
{
    static HighScoreManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[HighScoreManager alloc] init];
    });

    return manager;
}

-(id) init
{
    if ((self = [super init]))
    {
        // create from the file
        _scores = [NSKeyedUnarchiver unarchiveObjectWithFile:@"highscore.sav"];

        if (_scores == nil)
            _scores = [NSMutableArray new];
    }

    return self;
}

-(void) _synchronize
{
    // write to the file
    [NSKeyedArchiver archiveRootObject:_scores toFile:@"highscore.sav"];
}

-(void) addHighScore:(HighScore *)score
{    
    // add the score to the end of the array
    [_scores addObject:score];

    // sort on insert
    [_scores sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        HighScore *hs1 = obj1;
        HighScore *hs2 = obj2;

        return hs1.score - hs2.score;
    }];

    //  then synchronize
    [self _synchronize];
}

-(void) clearHighScores
{
    // remove all of the objects
    [_scores removeAllObjects];

    // then synchronize
    [self _synchronize];
}

-(HighScore *) bestScore
{
    // simply return the last object in the array
    return [_scores lastObject];
}

-(NSArray *) sortedScores
{
    // note this returns them from lowest to highest scores.
    // also notice we make a copy of the array. This way we 
    // can still add scores without compromising the reciever 
    // of this function.
    return [NSArray arrayWithArray:_scores]; 
}

@end

然后,在你走到一起的时候,你只是补充了高分点清单,并且照顾到你内部的所有困难。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签