Convert NSString into char array

后端 未结 6 1634
别那么骄傲
别那么骄傲 2020-11-29 22:53

I have a variable of type char[] and I want to copy NSString value in it. How can I convert an NSString to a char array?

6条回答
  •  广开言路
    2020-11-29 23:16

    We need to play NSString as a character array for working on coding practices which is otherwise much simpler to do in plain C coding. Using characterAtIndex: and appendFormat: helps me. May be this will help.

    NSString *str = @"abcdef";
    NSMutableString *strResult = [NSMutableString string];
    
    for (NSUInteger i = 0; i < [str length]; i++) {
      char ch = [str characterAtIndex:i];
      [strResult appendFormat:@"%c", ch];
    }
    NSLog(@"%@", strResult);
    

提交回复
热议问题