如何在 Objective-C 中声明类级属性? [英] How do I declare class-level properties in Objective-C?
本文介绍了如何在 Objective-C 中声明类级属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
也许这很明显,但我不知道如何在 Objective-C 中声明类属性.
Maybe this is obvious, but I don't know how to declare class properties in Objective-C.
我需要为每个类缓存一个字典,并想知道如何将其放入类中.
I need to cache per-class a dictionary and wonder how put it in the class.
推荐答案
属性在 Objective-C 中有特定的含义,但我认为您的意思是等同于静态变量的东西?例如.所有类型的 Foo 只有一个实例?
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 中声明类函数,请使用 + 前缀而不是 -,因此您的实现将类似于:
To declare class functions in Objective-C you use the + prefix instead of - so your implementation would look something like:
// Foo.h
@interface Foo {
}
+ (NSDictionary *)dictionary;
// Foo.m
+ (NSDictionary *)dictionary {
static NSDictionary *fooDict = nil;
if (fooDict == nil) {
// create dict
}
return fooDict;
}
这篇关于如何在 Objective-C 中声明类级属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文