Does Objective-C support class variables?

谁说胖子不能爱 提交于 2019-12-04 17:51:25

问题


I know it supports automatic variables, but what about class variables?


回答1:


The language does not support class variables. You implement class-specific state with global static variables in the compilation units of the implementation.

In the header (.h file):

@interface MyClass : NSObject
+(int)val;
@end

In the implementation (.m file):

static int val = 123;

@implementation MyClass
+(int)val {return val;}
@end

Usage:

if ([MyClass val] > 100) ...



回答2:


ObjC class variables are plain old static variables.

Foo.m:

static int foo = 0;

Alternatively, you can use a C++ anonymous namespace if you use ObjC++:

Foo.mm:

namespace {
    int foo = 0;
}

But there is another pattern if you want to benefit from the advantages of properties:

Foo.h:

@interface FooShared

@property ( atomic, readwrite, strong ) Foo* foo;

@end

@interface Foo

+ (FooShared*) shared;

@end

Foo.m:

@implementation FooShared
@end

static fooShared* = nil;

@implementation Foo

+ (FooShared*) shared
{
    if ( fooShared == nil ) fooShared = [FooShared new];

    return fooShared;
}

@end

somewhere.m:

Foo* foo …;
foo.shared.foo = …;

It may look a bit overkill, but it's a interesting solution. You use the same constructs and language features for both instance properties and "class" properties. Atomicity on demand, accessors when needed, debugging, breakpoints... Inheritance even.

Creative minds can find other ways to do all this I suppose. :) But you're pretty much covered with these options.




回答3:


@property (class, nonatomic, copy) NSString *someStringProperty;

But you have to provide getter and setter

From the Xcode 8 release notes: Objective-C now supports class properties, which interoperate with Swift type properties. They are declared as: @property (class) NSString *someStringProperty;. They are never synthesized. (23891898)




回答4:


Class properties were added back in Xcode 8.x. It's not much more than if you wrote your own setters and getters as class methods, and used statics for storage. In fact, that's all that it is, except for the compiler messages, see below.

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

https://developer.apple.com/library/archive/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html

@interface MyClass

@property (class) NSObject *someClassProperty;
@property (class, readonly) NSString *aReadClassProperty;

@end

If you don't add the required getters/setter, you'll see these errors:

Class property 'aReadClassProperty' requires method 'aReadClassProperty' to be defined - use @dynamic or provide a method implementation in this class implementation

Class property 'someClassProperty' requires method 'setSomeClassProperty:' to be defined - use @dynamic or provide a method implementation in this class implementation

Class property 'someClassProperty' requires method 'someClassProperty' to be defined - use @dynamic or provide a method implementation in this class implementation

in your implementation, you'll need:

@implementation MyClass

static NSObject *object;

+ (void)setSomeClassProperty:(NSObject *)someClassProperty {
    object = someClassProperty;
}

+ (NSObject *)someClassProperty {
    return object;
}

static NSString *string = @"My class string";

+ (NSString *)aReadClassProperty {
    return string;
}


来源:https://stackoverflow.com/questions/11187530/does-objective-c-support-class-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!