2013-03-29 51 views
0

我正在尝试将GameCenter整合到我的iPhone应用程序中。我想要做的是将NSString highScore上传到我的Game Center排行榜。我遇到了有关字符串兼容性的问题,我不确定要从这里执行什么操作。下面是我所说的空虚时,我想高分的NSString上传到GameCenter的游戏中心集成错误

-(void)submitScore { 
int x = [highScore floatValue]; 
score=&x; 
GKScore *myScoreValue = [[GKScore alloc] initWithCategory:@"grumpyEscapeHighScoresLeaderboard"]; 
myScoreValue.value = score; 

[myScoreValue reportScoreWithCompletionHandler:^(NSError *error){ 
    if(error != nil){ 
     NSLog(@"Score Submission Failed"); 
    } else { 
     NSLog(@"Score Submitted"); 
    } 

}]; 
} 

当提交,我得到了GameCenter的一个巨大的数字(803089816),即使高分的NSString的值是6。这里是错误信息:

Incompatible pointer to integer conversion assigning to 'int64_t' (aka 'long long') from 'int*' 

在这里我ViewController.h是我定义的分数为

int *score; 

我感到非常的新目标C,和一般的编码。对不起,如果这个问题似乎对别人很愚蠢。我一直在研究如何做到这一点,并且找不到任何答案。 Here是我从中获得代码并为我自己的项目修改它的教程。

+0

备注 - 如果你调用'floatValue',你应该把结果赋给一个'float'类型的变量,而不是'int'。如果你想分配一个'int',那么调用'intValue'而不是'floatValue'。 – rmaddy

回答

1

没有理由在这里使用int *而不是int你的分数值,同样没有理由将其存储到您的score实例变量,如果你只是在-submitScore方法使用它。

- (void)submitScore { 
    GKScore *myScoreValue = [[GKScore alloc] initWithCategory:@"grumpyEscapeHighScoresLeaderboard"]; 
    myScoreValue.value = [highScore integerValue]; 

    [myScoreValue reportScoreWithCompletionHandler:^(NSError *error){ 
     if(error != nil){ 
      NSLog(@"Score Submission Failed"); 
     } else { 
      NSLog(@"Score Submitted"); 
     } 

    }]; 
} 
+0

非常感谢。完美解决了我的问题! – user2201063