如何在Objective-C的NSTextField中将char和int结合起来?

如何在Objective-C的NSTextField中将char和int结合起来?

问题描述:

例如,我有以下的代码,其中lblPercentNSTextField如何在Objective-C的NSTextField中将char和int结合起来?

double Progress = progress(Points); 
[lblPercent setIntValue:(Progress)]; 

我将其设置为整数值,所以它扔出来的小数,由于某种原因,NSProgressIndicator强迫我使用双。无论如何,在进度条旁边的标签中,我希望它能看到数字x%和旁边的百分号。

我试过标准的连接技术,但没有骰子。

尝试

[lblPercent setText:[NSString stringWithFormat:@"%d%%",[Progress intValue]]]; 
+0

这给了我糟糕的接收器类型双。 – 2013-04-23 15:26:55

+0

[lblPercent setText:[NSString stringWithFormat:@“%f %%”,Progress]] ?? – alex 2013-04-23 15:44:01

+0

好的进展!我得到关于**的错误没有可见的@interface为NSTextField在setText中声明** – 2013-04-23 15:55:46

NSMutableString *value = lblPercent.text; 
[value appendString:@"%"]; 
[lblPercent setText:value]; 
+0

这给了我在NSTextField *类型的对象中找不到的文本,也没有可见的@interface为NSTextfield声明选择器setText – 2013-04-23 15:27:49

+0

@JonathanWeinraub:已经编辑了你的问题,使其更清楚地表明你在iOS中使用Cocoa和'NSTextField',而不是'UITextField'。 (这个答案假设'lblPercent'是一个'UITextField',并且不能与'NSTextField'一起使用)。 – NSGod 2013-04-23 16:29:03

+0

@JonathanWeinraub在您编辑之前,我假设您使用UILabel,因为您将它命名为lblPercent。 – 2013-04-23 17:40:19

您可以使用unicode characters得到percent sign

double value;

myLabel.text = [NSString stringWithFormat:@"%d\u0025", value ]

u0025'percent sign'

NSInteger percentageProgress = (NSInteger) (Progress * 100); 
[lblPercent setText:[NSString stringWithFormat:@"%d%%", percentageProgress]]; 

Unicode字符,您应该使用NSNumberFormatter with the percent style

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init]; 
[formatter setNumberStyle: NSNumberFormatterPercentStyle]; 
// Any other format settings you want 
NSString* formattedNumber = [formatter stringFromNumber: [NSNumber numberWithDouble: progress]]; 
+0

这给了我一个相当奇怪的数字,不是因为某种原因返回的变量。 – 2013-04-23 20:50:45

+0

@JonathanWeinraub使用这种方法来设置字段,你应该在你的测试字段中使用'setStringValue:',nt'setIntValue:' – JeremyP 2013-04-24 14:45:29

+0

对不起,花了这么长的时间回到你身边,我有期末考试。无论如何,我现在明白你的意思了。但是,我将其改为您的建议,我得到的价值是600%,所以百分比符号就在那里。但是,我正在返回的数据应该是6%而不是600%。到目前为止,我们正在取得最好的进展 - 我认为比我在考试中取得的进步要好: -/ – 2013-05-11 06:22:32

NSString *string = [NSString stringWithFormat:@"%.0f%@",Progress, @"%"]; 
[lblPercent setStringValue:string]; 

这似乎已经为我工作做的我做了它的方式......