Accesing global variable giving linker error in objective C

前端 未结 2 1109
轻奢々
轻奢々 2021-01-23 13:32

I have declared a global variable like below

extern NSString *name;
@interface viewcontrollerOne{}

in implementation file i am accessing that g

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 14:15

    extern is tipically used to create contants. If you want to Create a global variable string, you can do it in the following way:

    .h

    + (void)setName:(NSString*)name_in;
    
    + (NSString*)name;
    

    .m

    NSString* gName;
    
    @implementation ...
    
    + (void)setName:(NSString*)name_in{
       gName = name_in;
    }
    
    + (NSString*)name{
      return gName;
    }
    

提交回复
热议问题