ios - 将对象中的值从 1 更改为 true

标签 ios format-specifiers

我有一个对象,其中包含代表 true 或 false 的值 1 或 0。例如,让我们将我的对象称为 Stuff,stuff 看起来像这样。

@interface Stuff : NSObject {

NSString *_boolValue1;
NSString *_boolValue2;
}

当我用值填充实体时,例如:Stuff *stuff = [[Stuff alloc] init...] 如果我使用 NSLog 来显示存储在 boolValue1 和 boolValue2 中的值,它们可能会输出 1 或 0。我想使用 NSLog 输出类似 [stuff.boolValue1 == 1 ? @“真假”]。我缺少什么?我收到一个错误,格式指定类型“id”,但参数类型为“BOOL”...

NSString *myStr = [NSString stringWithFormat:@"%@", [stuff.boolValue1 isEqualToString:@"1" ? @"true" : @"false"]];

最佳答案

你的括号放错了地方,因为 isEqualToString 运算符返回 bool 值。

NSString *myStr = [NSString stringWithFormat:@"%@", [stuff.boolValue1 isEqualToString:@"1"] ? @"true" : @"false"];

或者,更简单、更典型的方法,使用下面评论中的建议:

NSString *myStr = [NSString stringWithFormat:@"%@", [stuff.boolValue1 boolValue] ? @"true" : @"false"];

关于ios - 将对象中的值从 1 更改为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12752541/

相关文章:

ios - 在 ObjectMapper 中调用 super 方法的问题

ios - 是否可以在 cocoa touch 中找到用户发送短信的号码?

c - 如何用 printf 打印 1 个字节?

c - 使用 fscanf 从文件中读取字符串后读取整数

c - 关于 C 中的转换说明符

c - C 中 scanf 函数的格式说明符中 %c 规范之前的空格

用于从桌面访问 iOS 应用程序的文档目录的 Windows API?

ios - 发送帖子请求 IOS 7

c - 指向结构体的指针的格式说明符

ios - 将图像保存到相册时出现 NSInvalidArgumentException