开发者

If I have an Achievement's Identifier & percentComplete, is there a way to convert this to a (GKAchievement *)achievement?

I'm working with http://developer.apple.com/library/ios/#samplecode/GKTapper/Listings/Classes_GameCenterManager_m.html ... Specifically, this code:

- (void) submitAchievement: (NSString*) identifier percentComplete: (double) percentComplete
{
    //GameCenter check for duplicate achievements when the achievement is submitted, but if you only want to report 
    // new achievements to the user, then you need to check if it's been earned 
    // before you submit.  Otherwise you'll end up with a race condition between loadAchievementsWithCompletionHandler
    // and reportAchievementWithCompletionHandler.  To avoid this, we fetch the current achievement list once,
    // then cache it and keep it updated with any new achievements.
    if(self.earnedAchievementCache == NULL)
    {
        [GKAchievement loadAchievementsWithCompletionHandler: ^(NSArray *scores, NSError *error)
        {
            if(error == NULL)
            {
                NSMutableDictionary* tempCache= [NSMutableDictionary dictionaryWithCapacity: [scores count]];
                for (GKAchievement* score in scores)
                {
                    [tempCache setObject: score forKey: score.identifier];
                }
                self.earnedAchievementCache= tempCache;
                [self submitAchievement: identifier percentComplete: percentComplete];
            }
            else
            {
                //Something broke loading the achievement list.  Error out, and we'll try again the next time achievements submit.
                [self callDelegateOnMainThread: @selector(achievementSubmitted:error:) withArg: NULL error: error];
            }

        }];
    }
    else
    {
         //Search the list for the ID we're using...
        GKAchievement* achievement= [self.earnedAchievementCache objectForKey: identifier];
        if(achievement != NULL)
        {
            if((achievement.percentComplete >= 100.0) || (achievement.percentComplete >= percentComplete))
            {
                //Achievement has already been earned so we're done.
                achievement= NULL;
            }
            achievement.percentComplete= percentComplete;
        }
        else
        {
            achievement= [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
            achievement.percentComplete= percentComplete;
            //Add achievement to achievement cache...
            [self.earnedAchievementCache setObject: achievement forKey: achievement.identifier];
        }
        if(achievement!= NULL)
        {
            //Submit the Achievement...
            [achievement reportAchievementWithCompletionHandler: ^(NSError *error)
            {
                 [self callDelegateOnMainThread: @selector(achievementSubmitted:error:) withArg: achievement error: error];
            }];
        }
    }
}
开发者_如何学编程

At the "//Something broke.." comment point, I want to convert the identifier & percentComplete that I have to an GKAchievement instance that can be then sent off into this bit of code (from http://www.garagegames.com/community/forums/viewthread/122529/):

- (void)saveAchievementToDevice:(GKAchievement *)achievement
{

    NSString *savePath = getGameCenterSavePath();

    // If achievements already exist, append the new achievement.
    NSMutableArray *achievements = [[[NSMutableArray alloc] init] autorelease];
    NSMutableDictionary *dict;
    if([[NSFileManager defaultManager] fileExistsAtPath:savePath]){
        dict = [[[NSMutableDictionary alloc] initWithContentsOfFile:savePath] autorelease];

        NSData *data = [dict objectForKey:achievementsArchiveKey];
        if(data) {
            NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            achievements = [unarchiver decodeObjectForKey:achievementsArchiveKey];
            [unarchiver finishDecoding];
            [unarchiver release];
            [dict removeObjectForKey:achievementsArchiveKey]; // remove it so we can add it back again later
        }
    }else{
        dict = [[[NSMutableDictionary alloc] init] autorelease];
    }


    [achievements addObject:achievement];

    // The achievement has been added, now save the file again
    NSMutableData *data = [NSMutableData data]; 
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:achievements forKey:achievementsArchiveKey];
    [archiver finishEncoding];
    [dict setObject:data forKey:achievementsArchiveKey];
    [dict writeToFile:savePath atomically:YES];
    [archiver release]; 
}

Is this possible? If so, how is it done?


These lines farther down in the top chunk of code seem like what you are asking for:

achievement= [[[GKAchievement alloc] initWithIdentifier: identifier] autorelease];
achievement.percentComplete= percentComplete;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜