开发者

how to save array into a string in iphone?

I would like to know that how to save an array in a string in iPhone (Objective-c). as i have an array with data

Array[0] = 01 Array[1] = 02 Array[2] = 03 Array[3] = 04

and now i want to save it in a string like this

NSString *string = @"01, 02, 03, 04";

If you have some i开发者_开发百科dea then please help me i am new in this field.

thanx in advance.


[myArray componentsJoinedByString: @", "] should do it.


NSMutableString sb = [[NSMutableString alloc] init];
for (int i=0; i < yourArrayLimit; i++){
  [sb appendFormat:@"%d",Array[i]];
  if(i < yourArrayLimit - 1)
    [sb appendString:@","];
}

One way..I am sure there are better methods..


NSMutableString sb = [[NSMutableString alloc] init];

for (int i=0; i < [Array count]; i++)  {  

  if(i<[Array count]-1)  
{
[sb appendString:[Array objectATIndex:i]];
}
else
{

[sb appendString:[Array objectATIndex:i]];
[sb appendString:@","];
}
}

Happy coding......


This method will work ... for you

NSString* str = [NSString stringWithFormat:@"%@, %@, %@, %@", Array[0], Array[1], Array[2], Array[3]];


You should also consider Serializing your objects. Check out the documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜