Objective c 从UIAlertView文本字段连接字符串
我有一个带有文本字段(alertView)的UIAlertView。假设我在UIAlertView文本字段中键入“Asdf” 在第一行中,我从该字段中获取文本并从中创建Objective c 从UIAlertView文本字段连接字符串,objective-c,nsstring,Objective C,Nsstring,我有一个带有文本字段(alertView)的UIAlertView。假设我在UIAlertView文本字段中键入“Asdf” 在第一行中,我从该字段中获取文本并从中创建*myString NSString *myString = [alertView textFieldAtIndex:0].text; 在第二行,我在NSLog中显示这个字符串,并添加一个值 NSLog(@"My String is: %@", myString); 这将在NSLog中正确显示: 我的字符串是:Asdf 因此,
*myString
NSString *myString = [alertView textFieldAtIndex:0].text;
在第二行,我在NSLog中显示这个字符串,并添加一个值
NSLog(@"My String is: %@", myString);
这将在NSLog中正确显示:
我的字符串是:Asdf
因此,在第三行,我用这个加法+我的原始字符串(myString)创建了一个NSString。最后在第4行,我在NSLog中显示第二个字符串
NSString *mySecondString = (@"My String is: %@", myString);
NSLog(@"%@", mySecondString);
在第三行(mySecondString=…
),我收到一条警告说
表达式结果未使用
此文件的NSLog显示不正确,如下所示:
Asdf
我确信我遗漏了一些非常简单的东西,因此如果您发现了一些东西,将不胜感激。这不是在Objective-C中连接字符串的方式。
要正确连接两个
NSString
实例,有几个选项
选项1<代码>+stringWithFormat类方法
NSString *a = @"hello";
NSString *b = @"world";
NSString *c = [NSString stringWithFormat:@"%@%@", a, b];
选项2,-stringByAppendingString
实例方法
NSString *a = @"hello";
NSString *b = @"world";
NSString *c = [a stringByAppendingString:b];
在您的情况下,您可以使用以下方法
NSString *mySecondString = [NSString stringWithFormat:@"%@%@",@"My String is:", myString];