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

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

问题描述

我似乎无法找到一种方法来设置我创建的静态int,以便为保存到持久内存的每个对象分配唯一ID。下面给我一个'无设定方法'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上测试,可能,因为我不能随意指向支持它,但它很容易测试,如果没有则会编译时间错误)允许您使用点表示法,即它看起来像一个类属性,即使它缺少 @property

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天全站免登陆