【问题标题】:Error when defining NSString global constants [duplicate]定义 NSString 全局常量时出错 [重复]
【发布时间】:2012-10-01 06:49:04
【问题描述】:

可能重复:
Interface type cannot be statically allocated?

这是我的代码,就像其他人一样:

TZNotifications.h
extern NSString *const TZNotificationKeywordAuthentication;

TZNotifications.m
NSString *const TZNotificationKeywordAuthentication = @"NOTIFICATION_KEYWORD_AUTHENTICATION";

但是我得到两个这样的代码错误:

【问题讨论】:

  • 嗯,在创建仅包含该内容的文件并在我的程序中使用它们时,对我来说工作得很好。你能显示周围的代码吗?似乎其他一些语法错误会使编译器窒息。
  • 我只是想知道,在这种情况下您不能将#define TZNotificationKeywordAuthentication @"NOTIFICATION_KEYWORD_AUTHENTICATION" 用于常量字符串的主要原因是什么...?
  • @holex Built in NSString contants in Cocoa 似乎是这样定义的,可能是因为这允许您使用 == 来比较它们(它们都是同一个对象)而你没有需要知道常量是 NSString 还是 enum/int。

标签: objective-c xcode nsstring


【解决方案1】:

替换 'const' 和类型声明:

TZNotifications.h

extern const NSString *TZNotificationKeywordAuthentication;

TZNotifications.m

const NSString *TZNotificationKeywordAuthentication = @"NOTIFICATION_KEYWORD_AUTHENTICATION";

【讨论】:

    【解决方案2】:

    谢谢你们。结果我愚蠢地在我的一个头文件的末尾添加了一个无意义的字符串,导致了这个奇怪的错误。对不起……

    【讨论】:

      猜你喜欢
      • 2012-08-09
      • 2016-05-01
      • 2012-06-13
      • 1970-01-01
      • 2021-03-11
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多