Casting Objective-C and C help

Discussion in 'iOS Development' started by uabckgb, May 2, 2008.

  1. uabckgb

    uabckgb New Member

    Joined:
    Apr 26, 2008
    Messages:
    2
    Likes Received:
    0
    I'm making an application that requires some low level access to sockets and serial port in an ITouch/IPhone.. For the low leve part i'm using ANSI C but i have a problem when trying to cast from char* to an id(@)... lets say that in a C function i read a string (char[]) from the serial port (or a socket) and i want to display this in a UITextView componet (uitextviewObj setText:myCFuncitionThatReturnCharArray()]; .

    How can i make this cast ?
    Where can i find a manual/tutorial with this information (casting between C and Objective-C)?

    Thanks.
  2. mysticwhiskey

    mysticwhiskey New Member

    Joined:
    Mar 27, 2008
    Messages:
    3
    Likes Received:
    0
    You'll probably want to use the stringWithUTF8String class method of NSString:

    Code:
    [uitextviewObj setText:[NSString stringWithUTF8String:myCFunctionThatReturnsCharArray()]];
    
    Note that if your function returns a NULL pointer, this will fail so you might need to check for that:

    Code:
    char* myCString = myCFunctionThatReturnsCharArray();
    if (myCString == NULL)
        [uitextviewObj setText:@""];
    else
        [uitextviewObj setText:[NSString stringWithUTF8String:myCString]];
    
  3. uabckgb

    uabckgb New Member

    Joined:
    Apr 26, 2008
    Messages:
    2
    Likes Received:
    0
    Thanks a lot mysticwhiskey .. this took care of my problem.

Share This Page