也许更简单不是恰当的词; terse 也可以。我很清楚 Objective-C 是一种冗长的语言;我只是想知道它是否必须像我在下面显示的解决方案中那样冗长。我对这门语言比较陌生。
这段代码根据生成每个字段包含一个标签的可变高度 UITableViewCell 计算记录中每种类型字段的数量。我的问题不是关于如何做到这一点——我已经想通了——我只是想知道:在 Objective-C 中,是否有一种使用 NSDictionary 对象的更简单或更简洁的方法来做到这一点?这是否像使用 NSDictionary 一样简洁?也欢迎使用其他集合类型对象的解决方案。
这是我的 -PartsRecord.countFields 方法。我稍微简化了代码。
-(NSDictionary*) countFields {
NSDictionary* dict = [[NSDictionary alloc] initWithObjectAndKeys:
@"s", [NSNumber numberWithUnsignedInt: [self.struts count]],
@"h", [NSNumber numberWithUnsignedInt: [self.headAssemblies count]],
@"l", (self.leftVent == nil) ? [NSNumber numberWithUnsignedInt: 0] :
[NSNumber numberWithUnsignedInt: 1],
@"r", (self.rightVent == nil) ? [NSNumber numberWithUnsignedInt: 0] :
[NSNumber numberWithUnsignedInt: 1],
nil ];
return dict;
}
以上代码中的任何错误都是转录错误,而不是代码中的实际错误。
TIA,
霍华德
最佳答案
@"l", [NSNumber numberWithUnsignedInt: self.leftVent ? 1 : 0],
@"r", [NSNumber numberWithUnsignedInt: self.rightVent ? 1 : 0],
会缩短两行
关于objective-c - 寻找更简单的 Objective-C 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/621312/