我对如何在 Objective C 中返回具有 NSString 返回类型但其中包含变量的值有一定的困惑。
这样可以吗?
- (NSString *)returnInfo{
return @"Userinfo:\nusername: %@\npassword: %@", self.username, self.password;
}
非常感谢您的回答... 谢谢...
最佳答案
应该是吧
- (NSString *)returnInfo{
return [NSString stringWithFormat:@"Userinfo:\nusername: %@\npassword: %@", self.username, self.password];
}
您的代码可能也会编译,但由于逗号运算符的工作方式,它会返回 password
。不过,这几乎肯定不是您的初衷。
关于objective-c - Objective-C : Methods with return type of NSString but with variables inside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8611852/