Using scanf with NSStrings

后端 未结 8 1772
迷失自我
迷失自我 2020-12-01 13:21

I want the user to input a string and then assign the input to an NSString. Right now my code looks like this:

NSString *word; 

scanf(\"%s\", &word);
         


        
8条回答
  •  甜味超标
    2020-12-01 13:50

    This is how I'd do it:

    char word [40];
    scanf("%s",word);
    
    NSString * userInput = [[NSString alloc] initWithCString: word encoding: NSUTF8StringEncoding];
    

提交回复
热议问题