Read file and encoding

Discussion in 'iOS Programming' started by gameplayerxp, Dec 16, 2010.

  1. gameplayerxp macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #1
    Hi guys,

    I'm going to show the contents of files in a UITextView with encoding - so it can show texts in different languages.

    But after reading a lot of iOS docs I still don't know which class is used to read file and if UITextView is enough to encode strings.

    Can anybody give me some advices? Thanks.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    In Cocoa, you don't usually need a class to read files. Instead, the class whose data is read from or written to a file has a method for doing that.

    Examples:
    NSData has dataWithContentsOfFile: and initWithContentsOfFile:.
    http://developer.apple.com/library/...Classes/NSData_Class/Reference/Reference.html

    NSString has stringWithContentsOfFile:encoding:error: and initWithContentsOfFile:encoding:error:
    http://developer.apple.com/library/...lasses/NSString_Class/Reference/NSString.html

    There are also many classes that take an NSData rather than a filename, and leave it to you to connect-the-dots between the class, the NSData, and files.

    And a UITextView doesn't encode a file into text in a string; that's what NSString does. An NSString contains Unicode characters, which can be in many different languages.
     
  3. gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Is this a file that you are creating or that the user is providing?

    If you are creating the file then make it UTF-8 and load it in that way with the appropriate NSString method.

    If the user is providing the file then this is more complicated.

    UITextView or UILabel will present unicode text in a single font. If you need multiple fonts or styles you probably need a UIWebView.
     
  5. gameplayerxp thread starter macrumors newbie

    gameplayerxp

    Joined:
    Dec 1, 2010
    Location:
    Sydney
    #5
    Good point. The files will be from end users. I'll try UIWebView. Thanks.
     

Share This Page