Question

i got this warning when i try to get a character at index

Initialization makes pointer from integer without cast

here's the code :

int random(arch4random()%(string.length));
NSString * char =[string charachterAtIndex:random];

i just wanna Thanks

Was it helpful?

Solution

from NSString

- (unichar)characterAtIndex:(NSUInteger)index

Is the method. It returns a unichar, not an NSString. You are declaring your variable is an NSString pointer but a unichar is stored as an integer behind the scenes.

Your line should be

unichar character = [string charachterAtIndex:random];

as a side note you can't call your variable char as its a reserved keyword in C.

OTHER TIPS

[NSString characterAtIndex] returns a character, not a string object. So you can't assign it to a NSString named char (or named anything else). You can say:

unichar ch = [string characterAtIndex:random];

Essentially the compiler is warning that by writing

NSString * name = [string characterAtIndex:random];

you're storing a pointer to a NSString * to the address referred by the character returned (unsigned long). If you need to have the resulting character as a NSString, you should use a method like

[NSString substringWithRange: NSRangeFromString([NSString stringWithFormat: @"%d-%d", random, 1])];

which returns a pointer to an NSString object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top