Objective-C: unichar vs. char

后端 未结 3 1622
执笔经年
执笔经年 2020-12-18 18:41

I\'m a little confused between a unichar and a char. Can I treat unichar\'s similar to char\'s?

For example, can I do this:

-(BOOL)isNewLine:(unicha         


        
相关标签:
3条回答
  • 2020-12-18 18:46

    Yes, unichar is internally unsigned short, so you can meaningfully compare it with a char (if the latter is ASCII, but that works fine for '\n').

    0 讨论(0)
  • 2020-12-18 18:57

    When comparing unichars, consider instead using:

    if ([[NSCharacterSet newlineCharacterSet] characterIsMember:unicharCharacter])
    {
        //any type of newline
    }
    else
    {
        //etc
    }
    

    Then you will catch all types of newlines and have code you can easily modify to match different character sets (like whitespaceAndNewlineCharacterSet).

    This is the big upside with unichars.

    0 讨论(0)
  • 2020-12-18 19:07

    Be careful checking for newline that you know the format of your line endings, as Unix (and modern Mac) use \n, but Windows uses \r\n and classic Mac uses \r.

    0 讨论(0)
提交回复
热议问题