objective c - concatenate NSString with loop -
i have code below, creates loop , loop inserting values into variable:
int i; nsstring *idts; for(i=0;i<11;i++){ idts = [nsstring stringwithformat:@"%d<-+->",i]; } nslog(@"order -> %@",idts); i wonder how concatenate values of variable "idts" inside loop, because console shows me following message:
10<-+->
while, should appear follows:
1<-+->2<-+->3<-+->4<-+->5<-+->6<-+->7<-+->8<-+->9<-+->10
someone can me?
should this:
nsstring *idts = @""; for(int i=0;i<11;i++){ idts = [nsstring stringwithformat:@"%@%d<-+->", idts, i]; } otherwise, not adding current string, you're overwriting variable.
update
based on @anon's comment, here's way exact output question:
nsstring *idts = @""; for(int i=0;i<11;i++){ idts = [nsstring stringwithformat:@"%@%d%@", idts, i, (i != 10) ? @"<-+->" : @""]; } and @rmaddy's - mutable string more efficient
nsmutablestring *idts = [nsmutablestring string]; for(int i=0;i<11;i++){ [idts appendformat:@"%d%@", i, (i != 10) ? @"<-+->" : @""]; }
Comments
Post a Comment