xcode ARC 中不允许从 NSInteger 到 NSString 的隐式转换.. 应该使用什么解决方法来处理整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8502774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:44:48  来源:igfitidea点击:

Implicit Conversion from NSInteger to NSString not allowed in ARC.. what workaround should be used to deal with Integers

xcodensstringios5xcode4.2nsinteger

提问by Pang Zi Yang

This is my code in my viewcontroller.m file

这是我在 viewcontroller.m 文件中的代码

- (void)viewDidLoad{
[super viewDidLoad];
[self.abilitygeneration setText:((TestAbility *)[self.testabilities objectAtIndex:0]).abilitygeneration]; }

It gives me an error implicit conversion of NSInteger aka int to NSString is disallowed with ARC. How do I enable ARC and have a workaround for this.Instead of setText what can I use?

它给了我一个错误,ARC 不允许将 NSInteger aka int 隐式转换为 NSString。我如何启用 ARC 并对此有一个解决方法。我可以使用什么来代替 setText?

abilitygeneration is set as NSInteger in TestAbility.h file.. thanks

能力生成在 TestAbility.h 文件中设置为 NSInteger ..谢谢

回答by sjs

So if I've understood correctly, setText:takes an NSStringand you have an NSInteger? If that's correct you can explicitly convert them as follows:

所以,如果我理解正确的话,setText:取 anNSString并且你有一个NSInteger? 如果这是正确的,您可以按如下方式显式转换它们:

[self.abilitygeneration setText:[NSString stringWithFormat:@"%d",((TestAbility *)[self.testabilities objectAtIndex:0]).abilitygeneration]];