views:

63

answers:

1

Hello,

Again I'm trying to wrap my head around Objective-C and Cocoa. So I've posted some code below. What I'm trying to do is basically use the function "th_brk_line" by entering text into a UITextField then displaying it into a UITextView once it has been processed by "th_brk_line". I've posted the external function definition and the typedef for "thchar_t" as well for clarity,I hope. I guess what I'm trying to accomplish is understanding how this C function would be used in Objective-C as well as a working example with UITextField and UITextView. This is an exercise mainly for my understanding the concept with Objective-C and Cocoa.

Thanks Again!

typedef unsigned char thchar_t; 

extern int th_brk_line(const thchar_t *in, thchar_t *out, size_t n, const char *delim);


int main () {

char line [1024];
char bline [1024];

while (fgets (line, sizeof line, stdin)) {
    if (line [strlen (line) - 1] == '\n')
        line [strlen (line) - 1] = '\0';

    th_brk_line (line, bline, 1024, "|");
    printf ("% s \n'", bline);
}

return 0;

}

A: 

At a high level, you need to put your text field and your text view on the screen and some sort of button that you press to make the text in the field appear in the view having been processed. You can use the keyboard's return key to do this.

On the assumption that you have done the above, calling a C function from Objective-C is easy. It's exactly the same as calling it from C. The tricky bit is that your function expects a C string as input and gives you a C string as output. Look in to NSString -UTF8String and NSString +stringWithCString:encoding:

JeremyP
Thanks! You were correct "tricky!". Still a bit rough, but i'll hack/read/re-hack my way through it until i get it right.
fulltone