如果变量不是nil,我想从变量连接一些字符串。如果他们是零,我不想包括他们。下面的代码可以工作,但是很笨拙,并且随着其他变量的增加而成比例地增加。任何人都可以推荐一种更优雅的方式来做到这一点吗?
NSString *city = self.address.city== nil ? @"" : self.address.city;
NSString *state = self.address.state== nil ? @"" : self.address.state;
NSString *zip = self.address.zip== nil ? @"" : self.address.zip;
NSString *bestCity;
if (city.length>0&&state.length>0&&zip.length>0) {
bestCity = [NSString stringWithFormat: @"%@ %@ %@", city, state,zip];
}
else if (city.length>0&&state.length>0) {
bestName = [NSString stringWithFormat: @"%@ %@", city,state];
}
else if (city.length>0&&zip.length>0) {
bestCity = [NSString stringWithFormat: @"%@ %@", city,zip];
}
else if (city.length>0&&zip.length>0) {
bestCity = [NSString stringWithFormat: @"%@ %@", city,zip];
}
else if (city.length>0) {
bestCity = [NSString stringWithFormat: @"%@", city];
}
else if (state.length>0) {
bestCity = [NSString stringWithFormat: @"%@", state];
}
else if (zip.length>0) {
bestCity = [NSString stringWithFormat: @"%@", zip];
}
else {
bestCity = @"";
}
答案 0 :(得分:1)
${DESTDIR}
答案 1 :(得分:1)
我通常会这样做:
NSMutableArray *bestCityArr = [@[] mutableCopy]; // [[NSMutableArray alloc] init];
if (city.length > 0)
[bestCityArr addObject:city];
if (state.length > 0)
[bestCityArr addObject:state];
if (zip.length > 0)
[bestCityArr addObject:zip];
NSString *bestCity = [bestCityArr componentsJoinedByString:@" "];
答案 2 :(得分:0)
此代码可以为您提供帮助!
User-agent: *
Allow: /minsc/menu-leaf.png
Disallow: /minsc/
Crawl-delay: 10