How do I get the string length of the stringValue of an NSTextField?

cocoa, objective-c

Solution

See NSString documentation

NSUInteger length = [[textField stringValue] length];

The crucial thing to realize here is that an NSString is not a char*. To get a real C-style char*, you need to do something like:

const char* ptr = [[textField stringValue]
    cStringUsingEncoding:[NSString defaultCStringEncoding]];

Updated to use default encoding instead of assuming ASCII.

Problem

This is probably a naive question but, how do I get the length of the `stringValue` of an `NSTextField`? I tried ``` int len = strlen((char *)[textField stringValue]); ``` where `textField` is an `NSTextField` but it always returns 6 (size of a pointer?). Besides I am sure that there is a more Objective-C way to do what I am after.

Original source