问题
-(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