![](/img/trans.png)
[英]How do I get the string length of the stringValue of an NSTextField?
[英]How do I send the mutable string to the NSTextField properly?
因此,我已經調試了所有這些代碼,這似乎還不錯。 我做了一個可變的字符串,由於某種原因,我無法將其顯示在標簽上。 調試器說
“ 2010-04-22 22:50:26.126 Fibonacci [24836:10b] ***-[NSTextField setString:]:無法識別的選擇器已發送到實例0x130150”
這有什么問題? 當我只是將字符串發送到NSLog時,它就很好了。
這是我所有的代碼,任何幫助將不勝感激。 “ elementNum”是一個comboBox,“ display”是一個Label。 謝謝
#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
`
查看您收到的錯誤消息:
-[NSTextField setString:]: unrecognized selector sent to instance 0x130150"
這告訴你一些事情。 具體來說,該NSTextField
沒有-setString:
方法,嘗試調用它將會失敗。
這是您查看NSTextField
文檔的提示。 這樣做時,您將看到沒有設置字符串值的方法。 但是,文檔還顯示NSTextField
繼承自NSControl
,后者具有-setStringValue:
方法。
因此,您需要調用-setStringValue:
來設置NSTextField
的值。
[display setStringValue:numbers];
請注意,當前在您的代碼中,您正在泄漏numbers
字符串對象。 您使用-alloc
創建了它,因此您有責任發布它。
相反,您應該使用[NSMutableString stringWithString:@"1, 1,"]
創建它,這將返回一個自動釋放的對象,並在同一條消息中對其進行初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.