Using scanf with NSStrings

后端 未结 8 1724
迷失自我
迷失自我 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:56

    scanf does not work with any object types. If you have a C string and want to create an NSString from it, use -[NSString initWithBytes:length:encoding:].

    0 讨论(0)
  • 2020-12-01 13:59

    Maybe this will work for you because it accepts string with spaces as well.

     NSLog(@"Enter The Name Of State");
     char name[20];
     gets(name);
     NSLog(@"%s",name);
    
    0 讨论(0)
提交回复
热议问题