在 Objective C 中设置静态变量 [英] Setting static variables in Objective C

查看:26
本文介绍了在 Objective C 中设置静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到一种方法来设置我创建的静态 int 来为我保存到持久内存的每个对象分配唯一的 id.以下为我提供了一个用于分配给属性的无 setter 方法"setIdGen.

I can't seem to find a way to set the static int I have created to assign unique ids to every object I save to persistent memory. The following gives me a 'no setter method 'setIdGen' for assignment to property.

-(void)viewDidLoad
{
    PlayerMenuController.idGen = [[NSUserDefaults standardUserDefaults] floatForKey:@"idGen"];
}

除上述之外,我还尝试创建一个静态 setIdGen 方法,该方法会返回错误访问错误,并使用自己的 set 方法制作 NSIntegers.当我尝试使用 = 分配它时,我的静态 NSMutableArray 给出了相同的错误,但在使用 setArray 时工作正常.

As well as the above I've tried creating a static setIdGen method that would return bad access errors, and making NSIntegers with their own set methods. My static NSMutableArray gave the same errors when I tried to assign it using = but worked fine when using setArray.

idGen 方法:

+ (int) idGen
{
    /*static int idGen;
    if(!idGen)
    {
        idGen = 0;
        NSLog(@"idGen reset");
    }*/
    return self.idGen;
}

推荐答案

2017 年更新

Xcode 8 向 Objective-C 引入了类属性,来自发行说明:

Xcode 8 introduced class properties to Objective-C, from the release notes:

Objective-C 现在支持与 Swift 类型属性互操作的类属性.它们被声明为 @property (class) NSString *someStringProperty;,并且永远不会被合成.

Objective-C now supports class properties, which interoperate with Swift type properties. They are declared as @property (class) NSString *someStringProperty;, and are never synthesised.

这意味着我们下面的示例界面可以变成:

This means our sample interface below can become:

@interface PlayerMenuController : NSObject

@property (class) int idGen;

@end

但是您仍然必须自己实现方法,如下所示,因为类属性永远不会被合成.请注意,这也意味着如果您指定属性属性,例如 copy,您的方法必须实现语义.

However you must still implement the methods yourself, as shown below, as class properties are never synthesised. Note that this also means if you specify property attributes, such as copy, that your methods must implement the semantics.

原答案

看起来你正在尝试实现一个类属性,但在 Objective-C 中没有这样的东西 - 一个属性是一对实例方法.

It looks like you are trying to implement a class property, but there is not such thing in Objective-C - a property is a pair of instance methods.

但是,您可以伪造它...

However, you can fake it...

虽然 @property 声明对您不可用,但如果您声明遵循正确命名约定的类方法,那么您的编译器可能(在 Xcode 4.6.1 上测试),可能",因为我不能直接指出它被支持,但测试很简单,如果不支持,将编译时错误)允许您使用点表示法,即它看起来像一个类属性,即使它缺少 @属性.

While the @property declaration is not available to you, if you declare class methods which follow the right naming convention then your compiler may (tested on Xcode 4.6.1, "may" as I cannot offhand point to this being supported, but it's simple to test and will compile time error if not) allow you to use dot notation, i.e. it looks like a class property even if it lacks an @property.

示例界面:

@interface PlayerMenuController : NSObject

// a class "property"
+ (int) idGen;
+ (void) setIdGen:(int)value;

@end

实现:

@implementation PlayerMenuController

static int idGen = 0;

+ (int) idGen { return idGen; }
+ (void) setIdGen:(int)value { idGen = value; }

@end

并进行测试:

NSLog(@"initial value: %d", PlayerMenuController.idGen);
PlayerMenuController.idGen = 42;
NSLog(@"updated value: %d", PlayerMenuController.idGen);

制作:

initial value: 0
updated value: 42

所以我们有一个类财产"——它看起来、走路和嘎嘎叫都像一个财产;-)

So we have a "class property" - it looks, walks and quacks like a property ;-)

这篇关于在 Objective C 中设置静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆