Referencing a static NSString * const from another class

前端 未结 3 510
闹比i
闹比i 2020-12-10 01:27

In class A I have this:

static NSString * const kMyConstant = @\"my constant string\";

How can I reference this from class B?

3条回答
  •  心在旅途
    2020-12-10 02:08

    If it's static, you can't (that's what the static keyword is for).

    If you simply declare it as a global variable, however, you can do something like this:

    // ClassA.m
    
    NSString *const str = @"Foo";
    
    // ClassB.m
    
    extern NSString *const str;
    
    NSLog(@"str is: %@", str);
    

提交回复
热议问题