开发者

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

开发者 https://www.devze.com 2022-12-08 11:27 出处:网络
This is probably a naive question but, how do I get the length of the stringValue of an NSTextField? I tried

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.


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.


stringValue is NSString instance. You may use the next code:

NSUInteger len = [[textField stringValue] length];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号