Why doesn't NSString getCharacters work in xcode?

本小妞迷上赌 提交于 2019-12-23 05:42:15

问题


-(void)myFunction {

        unichar myBuffer[5];
        NSRange range = {0, 3};
        NSString *string = [NSString alloc];
        string = @"123";
        [string getCharacters:myBuffer :range];
}

Gets warning

! NSString may not respond to '-getCharacters::'

then does a SIGABRT when I run it. Why????? Or better yet how can I get it to work?


回答1:


To use getCharacters:range:, write this line of code:

[string getCharacters:myBuffer range:range];

In Objective-C, method names are split up by arguments (a feature that derives from its Smalltalk heritage). So your original code was trying to send a message that's essentially named getCharacters: :, which isn't found. The corrected version sends the message getCharacters: range:.

The getCharacters:myBuffer construct says that the first argument to getCharacters:range: is myBuffer. Similarly, the range:range construct says the second argument is your NSRange named range.



来源:https://stackoverflow.com/questions/6881517/why-doesnt-nsstring-getcharacters-work-in-xcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!