我正在学习Objective - C,我想用一个单词替换警告中的%@。这是我的代码:
NSString *myString = @"John";
UIAlertView *myAlert = [[UIAlertView alloc]initWithTitle:@"Welcome:%@", myString message:...
有人可以修复代码吗?谢谢!
答案 0 :(得分:0)
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:[@"Welcome " stringByAppendingString:myString] ...];
答案 1 :(得分:0)
通常当你的字符串有%@
时,意味着某种字符串会替换它
您需要使用stringWithFormat:
方法NSString *
查看Apple Documentation for NSString *
对你而言,你只需要做以下
NSString *myString = @"John";
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Welcome: %@", myString]
message:@"Some Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
您可以使{1}}中的%@
替换为您的字符串(stringWithFormat:
)