为什么要使用类方法构造函数与alloc / init? [英] Why would you use a class method constructor versus alloc/init?

查看:198
本文介绍了为什么要使用类方法构造函数与alloc / init?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective C / Cocoa中的构造函数有两种选择:



1。类构造函数



  Product * product = [Product productWithIdentifier:@Chocolate]; 
//使用产品

2。 Alloc / init构造函数

  Product * product = [[Product alloc] initWithIdentifier:@Chocolate 
//使用产品
[产品版本];

我所做的


$ b b

  • 我倾向于使用类方法,因为它看起来更干净,你不需要发布。

  • 我看到很多alloc / init - 这样做有什么好处?



我的问题 b


  • 最好是哪一个?







    对于上下文,Product类将具有以下内容:

      +(Product *)productWithIdentifier:(NSString * )identifier_ {
    return [[[[self class] alloc] initWithIdentifier:identifier] autorelease];
    }

    - (Product *)initWithIndentifier:(NSString *)identifier_ {
    self = [super init]
    if(self){
    identifier = identifier_;
    }
    return self;
    }


    解决方案

    ,两者之间没有太大的区别。



    alloc / init 组合给你一个拥有参考。这意味着你必须发布以后。变量 classnameWithFoo 返回非所有的引用。您不能发布



    这遵循通常的Cocoa命名约定。除了以 alloc copy ,<$开头的方法之外,所有方法都返回非拥有(自动释放) c $ c> mutableCopy 和 new 。这些返回拥有引用,你必须发布



    使用哪一个主要是品味的问题。但是,如果你需要临时对象,你可以快速处置 alloc 变量导致稍少的方法调用( autorelease ),并且在一个循环中,它也减少了最大内存占用。然而,在大多数情况下,这种降低的成本是可以忽略的。


    There are two choices for constructors in Objective C/Cocoa:

    1. Class Constructor

    Product *product = [Product productWithIdentifier:@"Chocolate"];
    // Use product
    

    2. Alloc/init Constructor

    Product *product = [[Product alloc] initWithIdentifier:@"Chocolate"];
    // Use product
    [product release];
    

    What I do

    • I tend to use the class method just because it looks cleaner and you don't need a release.
    • I see a lot of alloc/init - what's the advantage to doing it this way?

    My Question

    • Which one is preferable? Or is it just a matter of taste?

    Code

    For context, the class Product would have the following:

    +(Product *)productWithIdentifier:(NSString *)identifier_ {
       return [[[[self class] alloc] initWithIdentifier:identifier] autorelease];
    }
    
    -(Product *)initWithIndentifier:(NSString *)identifier_ {
       self = [super init]
       if (self) {
           identifier = identifier_;
       }
       return self;
    }
    

    解决方案

    If you're using ARC, there's not that much of a difference between the two. If you're not using ARC, the difference is extremely important.

    The alloc/init combination gives you an owning reference. That means you must release it later on. The classnameWithFoo variant returns a non-owning reference. You may not release it.

    This follows the usual Cocoa naming conventions. All methods return non-owning (autoreleased) instances, except for the methods that start with alloc, copy, mutableCopy and new. These return owning references that you must release.

    Which one to use is mostly a matter of taste. However, if you need temporary objects that you can dispose quickly the alloc variant results in slightly fewer method calls (the autorelease) and in a loop, it also reduces the maximum memory footprint. However, in most cases this reduced cost is neglectable.

    这篇关于为什么要使用类方法构造函数与alloc / init?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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