I’m very new to Objective C and am having trouble with some very basic things.
In AppDelegate.m, I’m getting the following errors:
Use of undeclared identifier ‘
health‘
Use of undeclared identifier ‘attack‘
Code (respectively):
[Troll setValue:100 forKeyPath:health];
[Troll setValue:10 forKeyPath:attack];
I’m not really sure how to declare the identifiers.
AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSObject *Troll = [[NSNumber alloc]init];
[Troll setValue:100 forKeyPath:health];
[Troll setValue:10 forKeyPath:attack];
return YES;
}
@end
AppDelegate.h
#import `<UIKit/UIKit.h>`
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@interface Troll : NSObject {
NSNumber *health;
NSNumber *attack;
}
@end
Keys are strings, and not something else (like dangling syntactic garbage). Furthermore, ‘100’ and ’10’ are not objects. Even after this, you don’t want to set the properties of the class itself but of its instances. Try
instead.