static const Vs extern const

前端 未结 7 2099
傲寒
傲寒 2020-12-07 19:34

I have been using static const in my header files as so:

static NSString * const myString = @\"foo\";

But have read that this is not the \'

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 19:44

    Using static NSString* const myString = @"foo"; in a header file means that each translation unit gets a separate myString variable. I think the linker may consolidate these, but I wouldn't count on it. That means that code which compares a string it has received using if (someString == myString) ... might get false even if the caller passed myString in, if the caller were from a different translation unit. (Of course, code should use -isEqualToString: instead of ==, but with a properly-declared string constant the latter may be workable.)

提交回复
热议问题