【问题标题】:how to declare static NSString?如何声明静态 NSString?
【发布时间】:2009-10-08 04:19:28
【问题描述】:

如果我在接口中声明,如何声明静态 NSString 作为静态 NSString *str;我将属性设置为 @property (nonatomic, retain) static NSString *sportsName;

它给出错误?任何教程

【问题讨论】:

    标签: iphone


    【解决方案1】:

    如果你所说的静态是指它对类而不是类的实例进行操作,你可以这样做:

    //In the header file:
    + (NSString *)myStaticString;
    
    //In the implementation file:
    + (NSString *)myStaticString { return @"Hello world!"; }
    

    如果静态是指全局,那么只需在类范围之外声明它即可。

    @property 声明仅适用于实例(非类)方法。它们不能用于声明静态变量的访问器。

    【讨论】:

      【解决方案2】:

      这是另一种方式:

      NSString *const someString = @"TheStringHere";
      

      【讨论】:

        【解决方案3】:

        这样就可以了 ;-)

        - (void)foo {
            static NSString *bar = @"My String";
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-01-06
          • 1970-01-01
          • 2013-10-14
          • 2011-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多