我正在使用http://developer.apple.com/library/ios/#samplecode/GKTapper/Listings/Classes_GameCenterManager_m.html ...具体来说,这段代码:

- (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];
            }];
        }
    }
}

在“// Something break ..”注释点,我想将我具有的标识符&percentComplete转换为GKAchievement实例,然后可以将其发送到以下代码中(来自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];
}

这可能吗?如果是这样,怎么做?

最佳答案

这些最下面的代码行看起来像您要的内容:

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

关于iphone - 如果我有一个成就的标识符和percentComplete,是否可以将其转换为(GKAchievement *)成就?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6364647/

10-13 03:59