How to get the length of the stringValue NSTextField?

This is probably a naive question, but how can I get the length stringValue

for NSTextField

? I tried

int len = strlen((char *)[textField stringValue]);

      

where textField

is NSTextField

, but always returns 6 (pointer size?). Also, I'm sure there is a more Objective-C way to do what I want.

+2


source to share


2 answers


See NSString documentation

NSUInteger length = [[textField stringValue] length];

      

Crucial to understanding here is that 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.

+9


source


stringValue is an instance of NSString. You can use the following code:



NSUInteger len = [[textField stringValue] length];

      

+2


source







All Articles