我在进行某种串联时遇到了麻烦;
我收到了通过“[dico objectForKey:@"alertSerie"]”(例如)获得的 4 个不同数据。 数据必须用在屏幕上引入数据的字符串来显示。 我必须将这些数据放入一个 UITextField 中。
示例:
[dico objectForKey:@"alertfriend"] = Beth
[dico objectForKey:@"alertmom"] = Lise
[dico objectForKey:@"alertgirlf"] = Angela
text for Beth will be "This is my friend :"
text for Lise will be "This is my mom :"
text for Angela will be "This is my girlfriend :"
在 UITextField 中,我想显示:
This is my friend :Beth
This is my mom : Lise
This is my girlfriend : Angela
到目前为止一切正常,代码如下所示:
field.text = [NSString stringWithFormat:@"NThis is my friend : %@\nTHis is my mom : %@\nThis is my girlfriend : %@\n",[dico objectForKey:@"alertfriend"], [dico objectForKey:@"alertmom"], [dico objectForKey:@"alertgfriend"] ];
现在,一个人可能没有 friend 、没有妈妈、没有女朋友……
我该怎么办???
我尝试了很多可能性,但我们无法连接对象这一事实对我来说并不常见,这在这种情况下给我带来了问题......
非常感谢您的关注!!!
编辑:
我尝试了该代码,但有以下错误; ] 或者其他什么...
if ([dico objectForKey:@"alertSerie"]) {
NSString *serie = [NSString stringWithFormat:@"Numéro de série : %@\n", [dico objectForKey:@"alertSerie"] ];}
else {
NSString *serie = [NSString stringWithFormat:@" "] ];}
if ([dico objectForKey:@"alertDate"]) {
NSString *date = [NSString stringWithFormat:@"Date de mise en service : %@\n", [dico objectForKey:@"alertDate"] ];}
else {
NSString *date = [[NSString stringWithFormat:@" "] ];}
if ([dico objectForKey:@"alertCli"]) {
NSString *cli = [NSString stringWithFormat:@"Nom du client associé : %@\n", [dico objectForKey:@"alertCli"] ];}
else {
NSString *cli = [NSString stringWithFormat:@" "] ];}
最佳答案
你需要检查这个人是否有 friend 、女朋友和妈妈。这意味着您必须使用条件。这可以通过使用一些简单的 if 语句来完成。
NSMutableString *text = [[NSString alloc] init];
if([dico objectForKey:@"alertmom"] != nil)
{
[text appendFormat:@"This is my Mom: %@ \n", [dico objectForKey:@"alertmom"]];
}
if([dico objectForKey:@"alertfriend"]!= nil)
{
[text appendFormat:@"This is my friend: %@ \n", [dico objectForKey:@"alertfriend"]];
}
if([dico objectForKey:@"alertgirlf"] != nil)
{
[text appendFormat:@"This is my girlfriend: %@ \n", [dico objectForKey:@"alertgirlf"]];
}
field.text = text;
关于iphone - 尝试执行某种串联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6873918/