ios - Assigning charAtIndex to stringWithCharacters gives invalid cast warning and bad access error -
i'm trying grab firstname , lastname firstname+lastname.
int loop=0; nsmutablestring *firstname = [[nsmutablestring alloc]init]; nsmutablestring *fullname = [[nsmutablestring alloc]initwithstring:@"anahita+havewala"]; (loop = 0; ([fullname characteratindex:loop]!='+'); loop++) { [firstname appendstring:[nsstring stringwithcharacters:(const unichar *)[fullname characteratindex:loop] length:1]]; } nslog(@"%@",firstname);
i tried typecasting unichar const unichar* because characteratindex returns unichar stringwithcharacters accepts const unichar.
this causes cast smaller integer type warning , app crashes (bad access) when line encountered.
why string operations complicated in objective c?
try out:
nsmutablestring *firstname = [[nsmutablestring alloc] init]; nsmutablestring *fullname = [[nsmutablestring alloc] initwithstring:@"anahita+havewala"]; (nsuinteger loop = 0; ([fullname characteratindex:loop]!='+'); loop++) { unichar mychar = [fullname characteratindex:loop]; [firstname appendstring:[nsstring stringwithformat:@"%c", mychar]]; } nslog(@"%@", firstname);
Comments
Post a Comment