表达结果在iOS UIAlertView中未使用

时间:2012-08-22 14:32:39

标签: ios string uialertview

我仍然是iOS的新手,我有点卡住创建一个结合常规文本和变量的警报视图。我在initWithTitle行上收到“表达式结果未使用”警告,我不知道如何修复它。

name = @"foo";

//now create the alert
UIAlertView *myAlert = [[UIAlertView alloc]
                        initWithTitle: (@"Hello %@", name)
                        message: @"You're looking mighty fine today"
                        delegate: nil
                        cancelButtonTitle: @"I'm awesome"
                        otherButtonTitles: nil];

//show the alert
[myAlert show];

现在,有了警告,所有内容都会编译,但我的提醒标题只是“foo”而不是“hello foo”。

如果删除括号,我会在下一行收到语法错误。

1 个答案:

答案 0 :(得分:5)

按如下方式创建标题:

UIAlertView *myAlert = [[UIAlertView alloc]
                    initWithTitle: [NSString stringWithFormat:@"Hello %@",name]
                    message: @"You're looking mighty fine today"
                    delegate: nil
                    cancelButtonTitle: @"I'm awesome"
                    otherButtonTitles: nil];