objective c - How do you assign characterAtIndex to NSString variable? -


i've looked @ several posts answer question keep getting errors.

i have nsstring holds 2 characters...

mystring = @"abc"; 

all want assign first character in mystring nsstring variable...

nsstring *firstchar; firstchar = [mystring characteratindex:0]; 

i error when this:

incompatible integer pointer conversion assigning 'unichar *' (aka 'unsigned short *') 'unichar' (aka 'unsigned short')

what doing wrong?

nsstring's characteratindex: returns unichar, not string, can't assign string. need create string first:

nsstring *mystring = @"abc"; unichar firstchar = [mystring characteratindex:0]; nsstring *string = [nsstring stringwithcharacters:&firstchar length:1]; 

of course, darkdust says, can use substringwithrange: string subrange of string:

[mystring substringwithrange:nsmakerange(0, 1)]; // first character [mystring substringwithrange:nsmakerange(1, 2)]; // second , third characters 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -