Objective-C: Initialize char with char at row index

unichar myChar = [myString characterAtIndex:0];
[myNSMutableArray addObject:myChar];

I am trying to insert the first char of a string into an array to create an array of characters. The first line does not give me an error. However, the second line contains the following error:warning: passing argument 1 of 'addObject:' makes pointer from integer without a cast

This also causes the application to crash with a "bad address" error. I thought this error was due to a memory allocation problem. Can someone shed some light on this.

+3
source share
2 answers

One option is to add a character to the array as a string:

unichar myChar = [myString characterAtIndex:0];
NSString * charString = [NSString stringWithFormat:@"%c", myChar];
[myNSMutableArray addObject:charString];

Please note that this is probably too large.

+4
source

. unichar - . NSNumber. A unichar unsigned short, :

[myNSMutableArray addObject:[NSNumber numberWithUnsignedShort:[myString characterAtIndex:0]]];
+5

Source: https://habr.com/ru/post/1748854/


All Articles