Snowman
Snowman

Reputation: 32071

Format specifies type 'unsigned short' but the argument has type 'int'

I have a method that scans a string, converting any new lines to <br> for HTML. The line in question is:

NSCharacterSet *newLineCharacters = [NSCharacterSet characterSetWithCharactersInString:
          [NSString stringWithFormat:@"\n\r%C%C%C%C", 0x0085, 0x000C, 0x2028, 0x2029]];

Xcode gives me a warning here:

Format specifies type 'unsigned short' but the argument has type 'int'

It's recommending that I change all %C to %d, however that turns out to break the function. What is the correct way to do this, and why is Xcode recommending the wrong thing?

Upvotes: 2

Views: 3151

Answers (2)

nielsbot
nielsbot

Reputation: 16031

One option is to cast your arguments to unsigned short: i.e. (unsigned short)0x0085 etc

But if you're looking for newlines, you should just use the newline character set. This is "Unicode compliant":[ NSCharacterSet newlineCharacterSet ]

edit

Revisiting this question: If you are trying to separate an NSString/CFString by line breaks, you should probably use -[ NSString getLineStart:end:contentsEnd:forRange:].

Upvotes: 7

Sixten Otto
Sixten Otto

Reputation: 14816

Using the canned character sets as @nielsbot suggests is definitely the way to go when there's one that matches your app's needs.

But as far as writing a string literal with Unicode codepoints in it, you don't need -stringWithFormat:, you can just use Unicode escapes:

NSCharacterSet *aCharSet = [NSCharacterSet
  characterSetWithCharactersInString:@"\u2704\u2710\u2764"];

Upvotes: 2

Related Questions