Is there a better way to implement this?
I have an object that has properties for a person's address. For convenience, I have written a method to generate an NSString with the person's full address. My implementation is:
/**
 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:@"%@", s开发者_开发知识库elf.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;
}
This feels clumsy to me. Is there a better way to do this?
I don't think so.
You could loop through the properties, but since you're not appending a consistent string, you're not going to save yourself any trouble by doing so.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论