Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 940
Michael Kimin Choo Avatar asked Dec 27 '22 03:12

Michael Kimin Choo


1 Answers

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);
like image 73
Grady Player Avatar answered Apr 19 '23 23:04

Grady Player