将NSString转换为Int - 游戏中心

问题描述:

我正在开发一款使用游戏中心的应用程序,但我遇到问题。我希望玩家将他们的分数提交给排行榜,以便他们可以挑战朋友。这是NSString得分的代码。将NSString转换为Int - 游戏中心

-(IBAction)gasPedalPressed:(id)sender { 

double noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000; 


NSString *reactionTime= [[NSString alloc] initWithFormat:@"Good Job! You're reaction time is %1.0f Ms. Let's see if you can do better...", noSeconds]; 

NSString *time= [[NSString alloc] initWithFormat:@"%1.0f Ms", noSeconds]; 


if(greenLightOn == 0) 
    reactionTime = @"Slow down! You have to wait for the green light. Let's see if you can do better..."; 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reaction Time" message:reactionTime 
               delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
[alert show]; 

,这就是我想,遵守:

-(IBAction)submitscore:(id)sender { 
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"1234567890"]; 

scoreReporter.value = score.text;  
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) { 



    if (error !=nil) {; 
     NSLog(@"failed sub score"); 
    } else { 

     NSLog(@"submitted score"); 

    } 
} 


]; 

}

请帮助!

+0

不是' - [NSString intValue]'为你工作吗? – Costique

+0

@Costique我加了这个NSString * myString = [NSString stringWithString:@“time”]; scoreInt = [myString intValue];但它仍然不会工作 – RafeeJ

如果你有字符串:

NSSting *myString = [NSString stringWithString:@"2"]; 

你可以从字符串一个int值:

int i = [myString intValue]; 

[编辑] - 在回答您的评论:

由于您已经将noSeconds创建为double,所以没有必要将其转换为NSString,然后再将其转换回来。您可以简单地将noSeconds传递给您创建的GKScore实例。

为了让您的'submitScore'方法知道您的变量'noSeconds',您将需要将其创建为实例变量。 (或者你可以只把它传递作为方法的参数)

所以,在你的.h:

double noSeconds; 

@property (nonatomic, assign) double noSeconds; 

然后在您的m:

@synthesize noSeconds; 

-(IBAction)gasPedalPressed:(id)sender { 
    ... 
    noSeconds = (double) [self.startDate timeIntervalSinceNow] * -1000; 
    ... 
} 

-(IBAction)submitscore:(id)sender { 
    ... 
    GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:@"123"] autorelease]; 
    scoreReporter.value = noSeconds; 
    ... 
} 

而且它总是有益采取看一下适用的文档:GKScore Docs

+0

谢谢,但我将如何实现到我的代码?抱歉!我是一个完整的初学者! – RafeeJ

+0

@ RafeeSkull-manJenkins - 检查我更新的答案,希望它有帮助。 – AtkinsonCM

+0

评分依然不会提交!请你能给我发电子邮件,这样我可以回复我的项目你能看到我做错了什么吗?非常感谢你! – RafeeJ