使用 [[NSString alloc]initWithString 在 NSString 上显示 int 变量
我有一个名为“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要
-initWithFormat:
而不是-initWithString:
。You want
-initWithFormat:
instead of-initWithString:
.