2011-12-27 151 views
0

我知道這對於某人來說很容易。我需要組合對象來創建一個地址。我不知道該怎麼做。我想...附加字符串

NSString *agcstr = [infoDictionary objectForKey:@"physstr"]; 
NSString *agccity = [infoDictionary objectForKey:@"physcity"]; 
NSString *agcstate = [infoDictionary objectForKey:@"physstate"]; 
NSString *agczip = [infoDictionary objectForKey:@"physzip"]; 

dvController.agcaddress = [agcstr stringByAppendingString:@"\n"  stringByAppendingString:agccity stringByAppendingString:@", " stringByAppendingString:agczip]; 

這是完全錯誤的,但我總是至少嘗試的東西:)

+0

那麼你做的方式是絕對正確的,雖然它可能看起來很奇怪。 – 2011-12-27 06:38:25

回答

0

請嘗試以下方法:

NSString *agcstr = [infoDictionary objectForKey:@"physstr"]; 
NSString *agccity = [infoDictionary objectForKey:@"physcity"]; 
NSString *agcstate = [infoDictionary objectForKey:@"physstate"]; 
NSString *agczip = [infoDictionary objectForKey:@"physzip"]; 

dvController.agcaddress = [[[[agcstr stringByAppendingString:@"\n"] stringByAppendingString:agccity] stringByAppendingString:@", "] stringByAppendingString:agczip]; 
另外,
dvController.agcaddress = [NSString stringWithFormat:@"%@ \n %@, %@", agcstr, agccity, agczip]; 
+0

這是一個非常糟糕的主意。您正在創建並立即丟棄最後一行中的三個字符串對象,並且很難閱讀。 taskinoor對'stringWithFormat:'的建議要好得多。 – 2012-01-04 19:21:52

3

嘗試stringWithFormat

dvController.agcaddress = [NSString stringWithFormat:@"%@, %@, %@, %@", agcstr, agccity, agcstate, agczip];