Reputation: 21
I am amidst making an app and have used the #import "HEADER“ in a few of my apps as they are vital for the function and no problems have arisen. I have tried again to use a #import command the same way I would do, but I get a duplicate symbols error, the dupes being the three variables declared in header A.h being imported into B.m.
I HAVE TRIED:
Updating OSX
Restarting
Cleaning project
Use build architectures only -> YES
Having a cup of tea
Making sure I imported A.h not A.m
Help!
Upvotes: 2
Views: 187
Reputation: 389
You are not supposed to define a global variable in your header.
You have 2 options:
Option 1
Use static to declare a static variable. Then your variable is only accessible in the A class and you don't get the duplicate symbol error.
#import <UIKit/UIKit.h>
static NSString *characterName1 = @"";
static NSString *characterName2 = @"";
static int characterChoice;
@interface CharacterViewController : UIViewController {
Option 2 (If you like to use your global variable in multiple classes)
Use the extern keyword in the A.h to declare your variable and define it in the A.m. Then your global variable can be accessed in the B.m class without duplicate symbol.
//---------
// A.h
//---------
#import <UIKit/UIKit.h>
extern NSString *characterName1;
extern NSString *characterName2;
extern int characterChoice;
@interface CharacterViewController : UIViewController {
//-----------
//A.m
//-----------
#import "A.h"
NSString *characterName1 = @"";
NSString *characterName2 = @"";
@implementation CharacterViewController
Upvotes: 3