创建新实例的类方法 [英] Class methods which create new instances

查看:132
本文介绍了创建新实例的类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了标准的[[MyClass alloc] init]模式外,某些对象是通过静态方法(如MyClass *obj = [MyClass classWithString:@"blabla"]

Apart from the standard [[MyClass alloc] init] pattern, some objects are built from static methods like MyClass *obj = [MyClass classWithString:@"blabla"]

根据广泛的内存管理指南(包括Apple的指南),您仅负责释放alloc的对象.

According to widespread memory management guides (including Apple's), you're only responsible for releasing the objects that you alloc.

任何人都可以为我提供此类方法的模板吗?如何返回分配的对象(也许是[self alloc]; return self;)?您如何确定它将被发布?

Can anyone provide me with a template for such methods? How do you return the allocated object ([self alloc]; return self;, perhaps)? How do you make sure that it will be released?

推荐答案

它们是类方法,而不是静态方法 1 .创建自动释放对象的这种特定类型可以称为工厂方法"(以前也称为便利构造函数"),它们在

They are class methods, not static methods1. This specific type, creating autoreleased objects, can be referred to as "factory methods" (formerly also "convenience constructors"), and they are discussed in the Concepts in ObjC Guide. They go something like this:

+ (instancetype)whatsisWithThingummy: (Thingummy *)theThingummy {
    return [[self alloc] initWithThingummy:theThingummy];
}

其中Whatsis是您的课程,而Thingummy是您的课程使用的另一个课程.

Where Whatsis is your class, and Thingummy is another class which your class uses.

如果您不使用ARC进行编译,则惯例是在返回实例之前先autorelease实例.

If you're not compiling with ARC, the convention is to autorelease the instance before returning it.

instancetype关键字由Clang引入对于这些方法;结合self(这是类对象本身 2 在类方法中),它允许正确的子类行为:该方法生成接收消息的类的实例. 3 instancetype允许编译器执行比.

The instancetype keyword was introduced by Clang for these kinds of methods; combined with self (which is the class object itself2 in a class method) it allows correct subclass behavior: the method produces an instance of the class which received the message.3 instancetype allows the compiler to do more strict typechecking than id.

该框架子类中这种用法的说明:+[NSString stringWithFormat:]返回一个NSString实例,而+[NSMutableString stringWithFormat:]返回一个子类NSMutableString的实例,而无需NSMutableString显式重写该方法.

An illustration of this usage in subclasses from the framework: +[NSString stringWithFormat:] returns an NSString instance, whereas +[NSMutableString stringWithFormat:], returns an instance of the subclass NSMutableString, without NSMutableString being required to explicitly override the method.

如[基础知识] [1]文档所讨论的,这些工厂方法还有其他用途,例如访问单例,或在执行之前评估必要的内存分配(可能,但使用标准alloc/init对的方便较少.

As discussed by the [Fundamentals][1] doc, there are other uses for these factory methods, such as accessing a singleton, or appraisal of the necessary memory allocation before it's performed (possible, but less convenient, with a standard alloc/init pair).

1 Java或C ++中的静态方法",. ObjC中没有诸如静态方法之类的东西

1"Static methods" in Java or C++, "class methods" in Objective-C. There's no such thing as static methods in ObjC

2 而在实例方法中,self明智地是对实例的引用.

2Whereas in an instance method self is, sensibly, a reference to the instance.

3 以前,像通常的初始化方法(initWith...)一样,您将使用id作为返回类型.

3Previously, like the usual initialization methods (initWith...), you would have used id as the return type. Using a specific class name unnecessarily forces subclasses to override the method.

这篇关于创建新实例的类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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