使用 [[NSString alloc]initWithString 在 NSString 上显示 int 变量

发布于 2024-12-22 07:42:18 字数 868 浏览 4 评论 0原文

我有一个名为“health”的 NSString,还有一个名为“healthInt”的 int,我在我的 Appdelegates 中执行此操作,因为其他文件必须访问它。我想要做的是将 NSString“health”指定为“healthInt”的值。这样,在另一个类中,我可以为 NSString“health”设置一个标签,并在该标签上显示“healthInt”。这是代码,抱歉代码粘贴不正确。

Appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
NSString * health;
int healthInt;
}
-(void)take2Damage;
-(void)tellHealth;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (nonatomic, retain) NSString * health;
@end

Appdelegate.m

@synthesize health;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{

health = [[NSString alloc]initWithString:@"%d", healthInt];

但是这样做会给我带来错误“方法调用的参数太多,预期 1 个,有 2 个”

我是 Objective C 的业余爱好者,所以尝试简单地解释一下:)

谢谢!

I have and NSString called "health" and i have an int called "healthInt" I m doing this in my Appdelegates because other files have to access it. What i want to do is have the NSString "health" be assigned the value of "healthInt". That way in another class i can set a label to the NSString "health" and have "healthInt" be displayed on that label. Here's the code sorry the code isn't pasting properly.

Appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>{
NSString * health;
int healthInt;
}
-(void)take2Damage;
-(void)tellHealth;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (nonatomic, retain) NSString * health;
@end

Appdelegate.m

@synthesize health;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{

health = [[NSString alloc]initWithString:@"%d", healthInt];

But doing this gives me the error "Too many arguments to method call, expected 1, have 2"

I'm an amateur to objective c so try to explain it simply :)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的温暖 2024-12-29 07:42:18

您需要 -initWithFormat: 而不是 -initWithString:

You want -initWithFormat: instead of -initWithString:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文