How do I send the mutable string to the NSTextField properly?
So I have all this code that I have debugged and it seems to be fine. I made a mutable string and for some reason I can not get it to be displayed on my label. the debugger says
"2010-04-22 22:50:26.126 Fibonacci[24836:10b] *** -[NSTextField setString:]: unrecognized selector sent to instance 0x130150"
What is wrong with this? When I just send the string to NSLog, it comes out fine.
h开发者_开发技巧ere's all my code, any help would be appreciated. "elementNum" is a comboBox and "display" is a Label. Thanks
#import "Controller.h"
@implementation Controller
- (IBAction)computeNumber:(id)sender {
int x = 1;
int y = 1;
NSMutableString *numbers = [[NSMutableString alloc] init];
[numbers setString:@"1, 1,"];
int num = [[elementNum objectValueOfSelectedItem]intValue];
int count = 1;
while (count<=num) {
int z = y;
y+=x;
x=z;
[numbers appendString:[NSString stringWithFormat:@" %d,", y]];
count++;
}
[display setString:numbers];
NSLog(numbers);
}
@end
`
Look at the error message you're getting:
-[NSTextField setString:]: unrecognized selector sent to instance 0x130150"
This is telling you something. Specifically, that NSTextField
does not have a -setString:
method and trying to call it will fail.
This is your cue to look at the docs for NSTextField
. When you do so, you will see that there are no methods to set the string value. However, the docs also show you that NSTextField
inherits from NSControl
, which has a -setStringValue:
method.
So, you need to call -setStringValue:
to set the value of an NSTextField
.
[display setStringValue:numbers];
Note that in your code at present, you are leaking the numbers
string object. You created it using -alloc
, so you are responsible for releasing it.
Instead, you should create it using [NSMutableString stringWithString:@"1, 1,"]
, which will return an autoreleased object, as well as initializing it in the same message.
精彩评论