如何在Objective-C中声明类级属性?

时间:2021-02-26 20:57:09

Maybe this is obvious, but I don't know how to declare class properties in Objective-C.

也许这是显而易见的,但是我不知道如何在Objective-C中声明类属性。

I need to cache per-class a dictionary and wonder how put it in the class.

我需要缓存每个类a字典,并想知道如何将它放入类中。

9 个解决方案

#1


180  

properties have a specific meaning in Objective-C, but I think you mean something that's equivalent to a static variable? E.g. only one instance for all types of Foo?

属性在Objective-C中有特定的含义,但我想你的意思是等价于静态变量?对于所有类型的Foo,只有一个实例?

To declare class functions in Objective-C you use the + prefix instead of - so your implementation would look something like:

要在Objective-C中声明类函数,需要使用+前缀,而不是-,因此实现应该如下:

// Foo.h
@interface Foo {
}

+ (NSDictionary *)dictionary;

// Foo.m
+ (NSDictionary *)dictionary {
  static NSDictionary *fooDict = nil;
  if (fooDict == nil) {
    // create dict
  }
  return fooDict;
}

#2


100  

I'm using this solution:

我使用这个解决方案:

@interface Model
+ (int) value;
+ (void) setValue:(int)val;
@end

@implementation Model
static int value;
+ (int) value
{ @synchronized(self) { return value; } }
+ (void) setValue:(int)val
{ @synchronized(self) { value = val; } }
@end

And i found it extremely useful as a replacement of Singleton pattern.

我发现它作为单例模式的替换非常有用。

To use it, simply access your data with dot notation:

要使用它,只需用点符号访问您的数据:

Model.value = 1;
NSLog(@"%d = value", Model.value);

#3


62  

If you're looking for the class-level equivalent of @property, then the answer is "there's no such thing". But remember, @property is only syntactic sugar, anyway; it just creates appropriately-named object methods.

如果你正在寻找与@property等价的类级别,那么答案是“没有这样的东西”。但是记住,@property只是语法上的糖;它只创建命名为适当的对象方法。

You want to create class methods that access static variables which, as others have said, have only a slightly different syntax.

您希望创建访问静态变量的类方法,正如其他人所说,这些静态变量的语法略有不同。

#4


56  

As seen in WWDC 2016/XCode 8 (what's new in LLVM session @5:05). Class properties can be declared as follows

正如在WWDC 2016/XCode 8中所看到的(在LLVM会话@5:05中有什么新内容)。类属性可以声明如下

@interface MyType : NSObject
@property (class) NSString *someString;
@end

NSLog(@"format string %@", MyType.someString);

Note that class properties are never synthesized

注意,从来没有合成类属性

@implementation
static NSString * _someString
+ (NSString *)someString { return _someString; }
+ (void)setSomeString:(NSString *)newString { _someString = newString; }
@end

#5


19  

Here's a thread safe way of doing it:

这里有一个线程安全的方法:

// Foo.h
@interface Foo {
}

+(NSDictionary*) dictionary;

// Foo.m
+(NSDictionary*) dictionary
{
  static NSDictionary* fooDict = nil;

  static dispatch_once_t oncePredicate;

  dispatch_once(&oncePredicate, ^{
        // create dict
    });

  return fooDict;
}

These edits ensure that fooDict is only created once.

这些编辑确保fooDict仅仅创建一次。

From Apple documentation: "dispatch_once - Executes a block object once and only once for the lifetime of an application."

来自Apple文档:“dispatch_once—在应用程序的生命周期中只执行一个块对象一次。”

#6


8  

As of Xcode 8 Objective-C now supports class properties:

现在,Xcode 8 Objective-C支持类属性:

@interface MyClass : NSObject
@property (class, nonatomic, assign, readonly) NSUUID* identifier;
@end

Since class properties are never synthesised you need to write your own implementation.

由于类属性从未被合成,所以您需要编写自己的实现。

@implementation MyClass
static NSUUID*_identifier = nil;

+ (NSUUID *)identifier {
  if (_identifier == nil) {
    _identifier = [[NSUUID alloc] init];
  }
  return _identifier;
}
@end

You access the class properties using normal dot syntax on the class name:

使用类名上的普通点语法访问类属性:

MyClass.identifier;

#7


7  

Properties have values only in objects, not classes.

属性只在对象中有值,而不是类。

If you need to store something for all objects of a class, you have to use a global variable. You can hide it by declaring it static in the implementation file.

如果需要为一个类的所有对象存储一些东西,则必须使用全局变量。可以通过在实现文件中声明它为静态来隐藏它。

You may also consider using specific relations between your objects: you attribute a role of master to a specific object of your class and link others objects to this master. The master will hold the dictionary as a simple property. I think of a tree like the one used for the view hierarchy in Cocoa applications.

您还可以考虑在对象之间使用特定的关系:将master的角色属性为类的特定对象,并将其他对象链接到此主对象。主人将保持字典作为一个简单的属性。我想到了一棵树,就像在Cocoa应用程序中用于视图层次结构的树。

Another option is to create an object of a dedicated class that is composed of both your 'class' dictionary and a set of all the objects related to this dictionary. This is something like NSAutoreleasePool in Cocoa.

另一个选项是创建专用类的对象,该对象由“类”字典和与该字典相关的所有对象的集合组成。这有点像可可里的NSAutoreleasePool。

#8


3  

If you have many class level properties then a singleton pattern might be in order. Something like this:

如果您有许多类级别的属性,那么单例模式可能是有序的。是这样的:

// Foo.h
@interface Foo

+ (Foo *)singleton;

@property 1 ...
@property 2 ...
@property 3 ...

@end

And

// Foo.m

#import "Foo.h"

@implementation Foo

static Foo *_singleton = nil;

+ (Foo *)singleton {
    if (_singleton == nil) _singleton = [[Foo alloc] init];

    return _singleton;
}

@synthesize property1;
@synthesize property2;
@synthesise property3;

@end

Now access your class-level properties like this:

现在访问类级属性如下:

[Foo singleton].property1 = value;
value = [Foo singleton].property2;

#9


-3  

[Try this solution it's simple] You can create a static variable in a Swift class then call it from any Objective-C class.

您可以在Swift类中创建一个静态变量,然后从任何Objective-C类中调用它。

#1


180  

properties have a specific meaning in Objective-C, but I think you mean something that's equivalent to a static variable? E.g. only one instance for all types of Foo?

属性在Objective-C中有特定的含义,但我想你的意思是等价于静态变量?对于所有类型的Foo,只有一个实例?

To declare class functions in Objective-C you use the + prefix instead of - so your implementation would look something like:

要在Objective-C中声明类函数,需要使用+前缀,而不是-,因此实现应该如下:

// Foo.h
@interface Foo {
}

+ (NSDictionary *)dictionary;

// Foo.m
+ (NSDictionary *)dictionary {
  static NSDictionary *fooDict = nil;
  if (fooDict == nil) {
    // create dict
  }
  return fooDict;
}

#2


100  

I'm using this solution:

我使用这个解决方案:

@interface Model
+ (int) value;
+ (void) setValue:(int)val;
@end

@implementation Model
static int value;
+ (int) value
{ @synchronized(self) { return value; } }
+ (void) setValue:(int)val
{ @synchronized(self) { value = val; } }
@end

And i found it extremely useful as a replacement of Singleton pattern.

我发现它作为单例模式的替换非常有用。

To use it, simply access your data with dot notation:

要使用它,只需用点符号访问您的数据:

Model.value = 1;
NSLog(@"%d = value", Model.value);

#3


62  

If you're looking for the class-level equivalent of @property, then the answer is "there's no such thing". But remember, @property is only syntactic sugar, anyway; it just creates appropriately-named object methods.

如果你正在寻找与@property等价的类级别,那么答案是“没有这样的东西”。但是记住,@property只是语法上的糖;它只创建命名为适当的对象方法。

You want to create class methods that access static variables which, as others have said, have only a slightly different syntax.

您希望创建访问静态变量的类方法,正如其他人所说,这些静态变量的语法略有不同。

#4


56  

As seen in WWDC 2016/XCode 8 (what's new in LLVM session @5:05). Class properties can be declared as follows

正如在WWDC 2016/XCode 8中所看到的(在LLVM会话@5:05中有什么新内容)。类属性可以声明如下

@interface MyType : NSObject
@property (class) NSString *someString;
@end

NSLog(@"format string %@", MyType.someString);

Note that class properties are never synthesized

注意,从来没有合成类属性

@implementation
static NSString * _someString
+ (NSString *)someString { return _someString; }
+ (void)setSomeString:(NSString *)newString { _someString = newString; }
@end

#5


19  

Here's a thread safe way of doing it:

这里有一个线程安全的方法:

// Foo.h
@interface Foo {
}

+(NSDictionary*) dictionary;

// Foo.m
+(NSDictionary*) dictionary
{
  static NSDictionary* fooDict = nil;

  static dispatch_once_t oncePredicate;

  dispatch_once(&oncePredicate, ^{
        // create dict
    });

  return fooDict;
}

These edits ensure that fooDict is only created once.

这些编辑确保fooDict仅仅创建一次。

From Apple documentation: "dispatch_once - Executes a block object once and only once for the lifetime of an application."

来自Apple文档:“dispatch_once—在应用程序的生命周期中只执行一个块对象一次。”

#6


8  

As of Xcode 8 Objective-C now supports class properties:

现在,Xcode 8 Objective-C支持类属性:

@interface MyClass : NSObject
@property (class, nonatomic, assign, readonly) NSUUID* identifier;
@end

Since class properties are never synthesised you need to write your own implementation.

由于类属性从未被合成,所以您需要编写自己的实现。

@implementation MyClass
static NSUUID*_identifier = nil;

+ (NSUUID *)identifier {
  if (_identifier == nil) {
    _identifier = [[NSUUID alloc] init];
  }
  return _identifier;
}
@end

You access the class properties using normal dot syntax on the class name:

使用类名上的普通点语法访问类属性:

MyClass.identifier;

#7


7  

Properties have values only in objects, not classes.

属性只在对象中有值,而不是类。

If you need to store something for all objects of a class, you have to use a global variable. You can hide it by declaring it static in the implementation file.

如果需要为一个类的所有对象存储一些东西,则必须使用全局变量。可以通过在实现文件中声明它为静态来隐藏它。

You may also consider using specific relations between your objects: you attribute a role of master to a specific object of your class and link others objects to this master. The master will hold the dictionary as a simple property. I think of a tree like the one used for the view hierarchy in Cocoa applications.

您还可以考虑在对象之间使用特定的关系:将master的角色属性为类的特定对象,并将其他对象链接到此主对象。主人将保持字典作为一个简单的属性。我想到了一棵树,就像在Cocoa应用程序中用于视图层次结构的树。

Another option is to create an object of a dedicated class that is composed of both your 'class' dictionary and a set of all the objects related to this dictionary. This is something like NSAutoreleasePool in Cocoa.

另一个选项是创建专用类的对象,该对象由“类”字典和与该字典相关的所有对象的集合组成。这有点像可可里的NSAutoreleasePool。

#8


3  

If you have many class level properties then a singleton pattern might be in order. Something like this:

如果您有许多类级别的属性,那么单例模式可能是有序的。是这样的:

// Foo.h
@interface Foo

+ (Foo *)singleton;

@property 1 ...
@property 2 ...
@property 3 ...

@end

And

// Foo.m

#import "Foo.h"

@implementation Foo

static Foo *_singleton = nil;

+ (Foo *)singleton {
    if (_singleton == nil) _singleton = [[Foo alloc] init];

    return _singleton;
}

@synthesize property1;
@synthesize property2;
@synthesise property3;

@end

Now access your class-level properties like this:

现在访问类级属性如下:

[Foo singleton].property1 = value;
value = [Foo singleton].property2;

#9


-3  

[Try this solution it's simple] You can create a static variable in a Swift class then call it from any Objective-C class.

您可以在Swift类中创建一个静态变量,然后从任何Objective-C类中调用它。