简体   繁体   中英

How do I convert an array of ints to a NSString in Objective c?

First off, I'm pretty new to coding so bear with me if this is a dumb question. I don't know how to convert soemthing like int array[10] into a single NSString. I've been searching for solutions, but so far, all i've seen is converting from an array to an string array.

First thing I tried was something like

NSString *String=[NSString stringWithFormat @"%i",array];

But the thing is, i know that I can do something like

NSString *String[NSString stringWithFormat @"%i%i%i...",array[0],array[1],array[2],.....];

But i'm trying to avoid that since I will be doing this for more than 10 variables and I will be doing it quite a bit with seperately named variables

int i[] = {1,2,3,4,5,6,7};

int len = sizeof(i) / sizeof(int);

NSMutableString * str = [NSMutableString string];
for (int j = 0; j<len; j++) {
    [str appendFormat:@"%i ", i[j]];
}
    NSLog(@"%@",str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM