一些初学者Objective-C / iPhone问题 [英] Some beginner Objective-C/iPhone questions

查看:116
本文介绍了一些初学者Objective-C / iPhone问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始(过去几天读了很多)。这里有一些我已经堆积的问题,希望有人可以回答。

I'm just starting out (reading up a lot for the past couple of days). Here's some questions that I have stacked up, hopefully someone can answer them.

1。(self!= nil)签入初始化代码。为什么这样?为了防止意外访问那些包含在其中的仅运行一次代码?这个意外访问从何而来?进行此类检查表明我无法控制正在发生的事情。

1. the (self != nil) check in initializer code. Why do it? To prevent accidental access to some "run-only-once" code that's wrapped up in there? Where from could this accidental access come from? Doing such checks suggest that I don't have control over what's going on.


- (id)init {
    self = [super init]
    if (self != nil) {
    // Code..
    }
    return self;
}

2。它是怎么回事你不必释放静态方法返回的任何东西? (或者这就是我的想法)

2. How is it that you don't have to free up anything that static methods return? (or this is the idea I've got)

3。 str = @怎么样?你好!不同于

3. How is str = @"Hi there!" different from

str = [[NSString alloc] initWithString:@"Hi there!"];

据我所知,你必须使用第二种方法释放str,但不是先用?如果是这样,第一个什么时候发布?哪一个更可取(不注意打字长度)?

As I understand, you have to release str in aquired with second method, but not with first? If so, when does the first one get released? Which one is preferable (not minding the typing length)?

4. 什么是autorelease,如果iphone没有垃圾收集?我注意到在main.m中创建了一个名为自动释放池的东西。是[myObject autorelease];一种将myObject添加到最近的包装自动释放池的方法,它将释放它?基本上,一些魔法,以避免自己释放它?为什么要使用它?

4. What is autorelease, if iphone has no garbage collection? I've noticed something called "an autorelease pool" being created in main.m. Is [myObject autorelease]; a way of adding myObject to the nearest wrapping "autorelease pool", which will release it? Basically, some magic to avoid releasing it yourself? Why use it?

好吧,现在就是这样。谢谢你的答案!

Well, thats it for now. Thanks for any answers!

推荐答案


  1. 在Objective-C中,可以返回一个来自 -init 的自我以外的实例。例如,类执行此操作以强制执行单例实例,或者在类集群的情况下执行此操作。例如, NSNumber 返回一个子类,具体取决于传递给其初始值设定项的值的类型。因此,当您调用 [[NSNumber alloc] initWithLong:long_value] 时, NSNumber - initWithLong:初始化程序在 NSNumber + alloc 之后调用,但是<的子类 NSNumber 可能会返回给oringial来电者。因此模式

  1. In Objective-C, it's possible to return an instance other than self from -init. Classes do this, for example, to enforce a singleton instance, or in the case of class clusters. NSNumber, for example, returns a subclass depending on the type of value passed to its initializer. So when you call [[NSNumber alloc] initWithLong:long_value], NSNumber's -initWithLong: initializer is called after NSNumber's +alloc, but a subclass of NSNumber may be returned to the oringial caller. Thus the pattern

self = [super init];

self = [super init];

重新分配 self [super init] 的值,以便 self 指向<的实际实例<返回code> [super init] 。如果 + alloc 或超级 init 方法失败,则 [super init]的结果可能 nil 。为避免在初始化失败的情况下产生副作用,模式随后变为

which reassigns self to the value of [super init] so that self points to the actual instance that [super init] returned. If +alloc or the super's init method fails, the result of [super init] may be nil. To avoid, side effects in the case of a failed initialization, the pattern then becomes


- (id) init {
  if(self = [super init]) {
    // do initialization of instance variables etc.
  }

  return self;
}


请注意,您必须返回<$ c来自 init 方法的$ c> self (或 nil 或其他实例)。你应该将自己分配给 [super init] ,你可以在做之前检查 nil 更多工作。

Note that you must return self (or nil or an other instance) from the init method. You should assign self to [super init] and you may check for nil before doing more work.

您可能必须释放staic方法的返回值。您应该阅读Cocoa内存管理指南。规则通常很简单:如果您调用的方法在其签名中具有new,alloc或copy,则结果属于调用者,调用者必须调用 -release 在该实例上或者会有内存泄漏。当然你应该在其他任何东西(即不是来自alloc,new或copy方法)上调用 -retain 你要保留引用然后完成该实例后,请致电 -release -autorelease

You may have to release the return value of a staic method. You should read the Cocoa memory management guide. The rule is generally quite simple: If the method you call has "new", "alloc", or "copy" in its signature, the result belongs to the caller and the caller must call -release on that instance or there will be a memory leak. Of course you should call -retain on anything else (i.e. not from an "alloc","new" or "copy" method) you want to keep a reference to and then call -release or -autorelease when you are done with that instance.

str = @Hi there!,假设 str 被声明为 NSString * str; 指定字符串常量的地址 @你好!到 str 变量的值。您不需要保留或释放字符串常量。 str = [[NSString alloc] initWithString:@Hi there!]; 分配一个新的字符串实例。 str 的值将是此实例的地址。每次调用 str = [[NSString alloc] initWithString:@Hi there!]; 将再次分配一个新实例。所以在 str2 = [[NSString alloc] initWithString:@Hi there!]; str!= str2 ,在 str2 = @Hi There!之后, str == str2 。请参阅答案为好吧。

str = @"Hi there!", assuming str was declared as NSString *str; assigns the address of the string constant @"Hi there!" to the value of thestrvariable. You do not need to retain or release string constants.str = [[NSString alloc] initWithString:@"Hi there!"];allocates a new string instance. The value ofstrwill be the address of this instance. Each call ofstr = [[NSString alloc] initWithString:@"Hi there!"];again will allocate a new instance. So afterstr2 = [[NSString alloc] initWithString:@"Hi there!"];,str != str2, while afterstr2 = @"Hi There!", str==str2. See this answer as well.

-autorelease 将接收器添加到当前的 NSAutoreleasPool 。当池耗尽时(通常在当前运行循环迭代结束时,或者手动耗尽池时),池将在池中的所有实例上调用 -release 。如果此 -release 将保留计数降为0,则释放该对象(并调用 -dealloc ),就像使用任何其他 -release 。在iPhone上使用自动释放池通常是不受欢迎的,因为它可能会导致您在运行循环迭代结束时耗尽之前在池中累积许多未使用的实例。如果你可以使用 -release 而不是 -autorelease ,你通常应该这样做。再次,请参阅Cocoa内存管理指南以获取更多信息。

-autorelease adds the receiver to the current NSAutoreleasPool. When the pool is drained (usually at the end of the current run loop iteration, or when the pool is manually drained), the pool calls -release on all instances in the pool. If this -release drops the retain count to 0, the object is deallocated (and -dealloc called) just as with any other -release. Using an autorelease pool is generally frowned upon on the iPhone because it may cause you to accumulate many unused instances in the pool before it is drained at the end of the run loop iteration. If you can use -release instead of -autorelease, you generally should. Again, see the Cocoa memory management guide for more info.

这篇关于一些初学者Objective-C / iPhone问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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