Simple questions

xaosai

Registered
I'm new to cocoa programming..still trying to get used to it. First, if I want to do int => char conversions and vice versa, do I use the C++ method or is there a different way in objective-c? Second, I'm building an application and I want to read in a character string from a user-editable NStextField. What is the NStextField method that allows me to do this? Thanks!
 
Typecasting in Objective-C is the same as C. You can use C++ classes and methods, but your implementation file (.m) needs to have its extension set to .mm for the compiler to compile it correctly (you also need all the standard #includes). I'd suggest to stay away from C++ in Objective-C unless you know what you're doing. So, to typecast in Objective-C it's the same (well you don't need to typecast between ints and chars):
Code:
int blah = 65;
char a = blah; // ASCII 'A'
NSLog(@"blah: %d; char: %c", blah, a);
What do you mean "character string"? NSTextField has a stringValue method to return an NSString of the contents in the text field. If you want to get a C-style string from that, you can use NSString's UTF8String method:
Code:
//myTextField is an NSTextField
NSString *textFieldValue = [myTextField stringValue];
const char *cstring = [textFieldValue UTF8String];
 
Back
Top