如何在Objective-C中声明类级属性? [英] How do I declare class-level properties in Objective-C?

查看:109
本文介绍了如何在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中声明类函数,你使用+前缀而不是 - 所以你的实现将看起来像: p>

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屋!

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