我有一个具有人名地址属性的对象。为方便起见,我编写了一个方法来生成一个带有人的完整地址的NSString。我的实现是:
/**
Returns the full address in US format of the Addressable object.
*/
- (NSString *)fullAddress {
NSMutableString *ret = [NSMutableString string];
if (self.company) {
[ret appendFormat:@"%@\n", self.company];
}
if (self.firstName) {
[ret appendFormat:@"%@", self.firstName];
}
if (self.firstName && self.lastName) {
[ret appendString:@" "];
}
if (self.lastName) {
[ret appendFormat:@"%@", self.firstName];
}
if (self.firstName || self.lastName) {
[ret appendString:@"\n"];
}
if (self.address) {
[ret appendFormat:@"%@\n", self.address];
}
if (self.addressLine2 && ![self.addressLine2 isEqualToString:@""]) {
[ret appendFormat:@"%@\n", self.addressLine2];
}
if (self.addressLine3 && ![self.addressLine3 isEqualToString:@""]) {
[ret appendFormat:@"%@\n", self.addressLine3];
}
if (self.city) {
[ret appendString:self.city];
}
if (self.city && self.state) {
[ret appendString:@", "];
}
if (self.state) {
[ret appendString:self.state];
}
if (self.zip) {
[ret appendFormat:@" %@", self.zip];
}
return ret;
}
这对我来说很笨拙。有更好的方法吗?
答案 0 :(得分:0)
我不这么认为。
你可以遍历这些属性,但由于你没有附加一致的字符串,所以你不会为此节省任何麻烦。