我是 ObjC 和 iPhone 的新手。
我下载了一个在多个视图之间共享数据的示例。基本方法是在基础 UIApplication 中创建一个数据模型对象并从中获取/设置数据。所以在init方法中我看到了以下代码:
- (id) init;
{
self.theAppDataObject = [[ExampleAppDataObject alloc] init];
[theAppDataObject release];
return [super init];
}
之后,使用委托我们可以访问这个对象。
id theDelegate = (id) [UIApplication sharedApplication].delegate;
ExampleAppDataObject* theDataObject;
theDataObject = (ExampleAppDataObject*) theDelegate.theAppDataObject;
所以,我的问题在第一个代码示例中。为什么我们需要为 theAppDataObject 对象分配内存,然后立即释放该对象?为什么我们稍后访问这个对象时不会得到 nil 呢?
10倍