I’ve just starting out with obj-c and I created 2 files, a .h and a .m file. The .h file is..
#import <Foundation/Foundation.h>
@interface CardUnit : NSObject
{
@property (assign) NSString *name;
@property (assign) NSString *gold;
@end
and the .m file is
#import "CardUnit.h"
@implementation CardUnit
@synthesize gold = @"gold";
@synthesize name = _name;
@end
But it’s giving me an error on
@synthesize gold = @"gold";
Saying “expected ; after synthesize”
Why can’t I set that to a string?
@synthesizeis not used for giving variables a value, but is rather a shorthand for defining basic getters and setters for the variable. Thesyntax is used to say, “I want you to use the instance variable
_varas the internal variable for the propertyvar“.If you want to assign a default string to a property, put it in your
initmethod:Or you can set the default value in the getter (per @Mario’s comment bellow):