在Objective C 中,我有一个对象,例如有Person
很多字段firstName
、、、、lastName
...等等。这些字段类型是并且其中任何一个都可以是。phoneNumber
address
city
NSString
nil
现在我想将我的字段值连接到另一个NSString
:
Person *p = ...
NSMutableString *s = [[NSMutableString alloc] init];
for (NSString *field in @[p.firstName, p.lastName, p.phoneNumber,
p.adress, p.city, ....more fields...]) {
if ([field length] > 0) {
[s appendFormat:@"%@\n", field];
}
}
问题是,只要其中一个字段是nil
. 我有一个例外:
[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object
from objects[0]'
我怎样才能简单地处理循环中的nil
值的情况?for