Character arrays in Xcode

Discussion in 'Mac Programming' started by JimmyFeatures, Nov 14, 2005.

  1. macrumors newbie

    Joined:
    Oct 31, 2005
    #1
    I want to assign the value of a text field to a character array in Xcode. How would I go about doing this?

    Thanks.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What languge are you using? XCode supports Obj-C, C++, Java, AppleScript and probably lots more! Also what type is the character array? Is it char (i.e. a byte) or unichar (2 bytes I think).
     
  3. thread starter macrumors newbie

    Joined:
    Oct 31, 2005
    #3
    Basically, I want to assign

    char array[255]

    to a text input.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You still havn't said what language! That narrows it to C++ (Carbon) or Obj-C (Cocoa). When you say text input do you mean from the command line or from a text box on screen (in a window)? If it's from a text box in a window you need to very carefull with this as the string value from that text box will be 16-bit Unicode, whereas your array is for 8-bit chars.

    You can do do this in Cocoa with something like:
    Code:
    IBOutlet NSTextView *textView;  // Conect in IB
    
    ...
    
    char *array = [[textView stringValue] UTF8String];
    
    This assumes that the text in the text box is a UTF-8 string (i.e. ASCII). The array returned will be null terminated, not 255 bytes long.
     
  5. thread starter macrumors newbie

    Joined:
    Oct 31, 2005
    #5
    OK. To clear this up.

    I am using OBJ-C in cocoa, and would like to assign a text input, which is a text box inside a window (not command line) to a C/C++ style character array.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Then the above will work for UTF-8 strings. If you need it in an array that you have assigned you can just copy the characters across or use the provided NSString method. You need to know what encoding you are in.

    Perhaps you could read the documentation.
     

Share This Page