views:

126

answers:

1

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.

here'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


`
+2  A: 

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.

Rob Keniger
Thanks so much man. I'm new to computer programming and I've been trying to teach myself Cocoa. I looked up a couple things in the docs but I guess I should've looked up NSTextField. Thanks again.
Merle