5

我对为什么收到错误“格式字符串未使用的数据参数”感到有些困惑

有没有其他人在 Xcode 4.5 for iOS6 中得到这个或修复这个?

- (IBAction)facebookPost:(id)sender
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
    mySLComposerSheet = [[SLComposeViewController alloc] init];
    mySLComposerSheet = [SLComposeViewController  composeViewControllerForServiceType:SLServiceTypeFacebook];

    [mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I'm listening to Boilerroom Recordings via the Boilerroom iPhone Application",mySLComposerSheet.serviceType]];

    [mySLComposerSheet addImage:[UIImage imageNamed:@"BOILERROOM_LOGO_250x250.png"]];
    [self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
    NSLog(@"dfsdf");
    switch (result) {
        case SLComposeViewControllerResultCancelled:
            break;
        case SLComposeViewControllerResultDone:
            break;
        default:
            break;
    }
}];

}
4

1 回答 1

11

您遇到的错误是不言自明的:当您使用stringWithFormat时,您应该在格式字符串中提供一些格式化占位符(例如%@作为对象的占位符、%d整数、%f作为浮点数的占位符等,就像所有类似 printf 的方法)。

但你不使用任何。因此,mySLComposerSheet.serviceType您在格式字符串之后放置的参数不会被格式字符串(无占位符)使用,并且在这里没有用。因此错误说“mySLComposerSheet.serviceType格式字符串不使用数据参数(即 )”。


所以解决方案取决于你打算做什么:

  • 如果你真的想serviceType在你的字符串中插入某个地方,只需在你的格式字符串中添加一个%@(作为serviceType一个NSString*,因此是一个对象)占位符,在你mySLComposerSheet.serviceType要插入的值的位置。例如:

    [mySLComposerSheet setInitialText:[NSString stringWithFormat:@"I'm listening to Boilerroom Recordings via the Boilerroom iPhone Application and want to share it using %@ !",mySLComposerSheet.serviceType]];
    
  • 但我想实际上你不想serviceType在你的 initialText 字符串的任何地方插入值(我想知道你为什么首先添加这个参数)。在这种情况下,您可以简单地删除调用的这个无用的附加参数stringWithFormat:。或者更好,因为那时你的stringWithFormat调用根本不会有任何格式占位符,无论如何%@完全没用stringWithFormat,所以直接使用字符串文字

    [mySLComposerSheet setInitialText:@"I'm listening to Boilerroom Recordings via the Boilerroom iPhone Application"];
    
于 2012-09-27T12:34:32.967 回答