0

I am attempting to use scanf to assign a value to an NSString, as per the answers to this question by Omar. This is the code, taken straight from progrmr's answer:

char word[40];

        int nChars = scanf("%39s", word);   // read up to 39 chars (leave room for NUL)
        NSString* word2 = [NSString stringWithBytes:word
                                             length:nChars
                                           encoding:NSUTF8StringEncoding];

However, I'm getting an error on the last line that makes absolutely no sense to me:

No known class method for selector 'stringWithBytes:length:encoding:'

What in the world could be causing this error?

And yes, I do have #import <Foundation/Foundation.h> at the top of the file.

Community
  • 1
  • 1
James Dunn
  • 8,064
  • 13
  • 53
  • 87

3 Answers3

4

NSString does not have a stringWithBytes:length:encoding: class method, but you can use

NSString* word2 = [[NSString alloc] initWithBytes:word
                                         length:nChars
                                       encoding:NSUTF8StringEncoding];

Note however, that scanf() returns the number of scanned items and not the number of scanned characters. So nChars will contain 1 and not the string length, so you should set nChars = strlen(word) instead.

A simpler alternative is (as also mentioned in one answer to the linked question)

NSString* word2 = [NSString stringWithUTF8String:word];
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
2

NSString does not respond to the selector stringWithBytes:length:encoding:. You probably wanted initWithBytes:length:encoding:.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 2
    The funny thing is that *you* recommended `+[NSString stringWithBytes:length:encoding:]` in the answer to the linked question :-) – Martin R Jan 06 '14 at 18:33
  • @MartinR: Yikes, Past Me strikes again! Also weird is that prgrmr made the same mistake. I'm afraid my brain leakage is catching. Thanks for pointing that that, though. – Chuck Jan 06 '14 at 18:35
  • I'm getting the same error with initWithBytes. [EDIT] Nevermind, it works when I change NSString to [NSString alloc]. – James Dunn Jan 06 '14 at 18:55
  • I just could not resist when noticing it, and I do hope that you did not take offence at my comment. – Martin R Jan 06 '14 at 19:02
  • @MartinR: No offense at all. It was a great catch. I laughed and corrected it. – Chuck Jan 06 '14 at 19:06
0

Story in short: you might want to consider a const char C-string suitable initializer for your NSString object. Also, allocate memory before sending any initializer message to the NSString object. I would expect something like:

char word[40];
int nChars = scanf("%39s", word);

NSString *word2 = [[NSString alloc] initWithCString:word encoding:NSASCIIStringEncoding];

Note that initWithCString per design only supports properly null '\0' terminated 8-bit character arrays. For unterminated bytes arrays you have initWithBytes:length:encoding: instead.

For Unicode characters you could consider initWithCharactersNoCopy:length:freeWhenDone:.

Anthony
  • 101
  • 4