0
我有一個對象具有一個人的地址屬性。爲了方便,我寫了一個方法來生成一個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;
}
這對我來說很笨拙。有一個更好的方法嗎?
對codereview.stackexchange.com可能是一個很好的問題 – 2011-02-17 03:18:54