Obj-C 属性、范围和辅助类
我一直在尝试了解 iOS 系统的一些细微差别,但有一项我不太清楚。我正在构建一个类来封装访问日历的方法,其中包括创建事件、事件存储单例和异步获取操作。
类变量和实例变量混合在一起,但我很难让它发挥作用。
我的问题是这样的。
我们来看看这个项目。
http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown< /a>
Play 类封装了有关播放的信息。
@interface Play : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSArray *characters;
@property (nonatomic, strong) NSString *genre;
@property (nonatomic, strong) NSDate *date;
@end
#import "Play.h"
@implementation Play
@synthesize title, characters, genre, date;
@end
如果我理解正确的话,由于合成,没有 dealloc 方法。
现在,没有定义其他方法。
在示例项目的详细信息控制器中,对象根本没有初始化,但可以通过引用访问属性,这让我感到困惑。如果我尝试在 Play 中编写方法,这些方法将无法更改属性中的值。例如,如果您尝试将 Data Controller 类与 Play 类合并,我注意到方法无法从属性中设置和获取值。
例如,如果我添加到 Play 类中:
(void)createSamplePlay
{
self.title = @"Play 1";
self.characters = [NSArray arrayWithObject:[NSString stringWithFormat:@"Character 1"]];
self.genre = @"Genre 1";
self.date = [NSDate date];
}
然后我调用Details Controller 中的方法,实际上没有任何改变。如果我将该方法移动到详细信息控制器内部,声明一个静态类型属性 Play *play
,然后调用方法 [self createSamplePlay];
并更改为 self
到 play
,那么当然,这些方法就可以工作了。
我不知道我的理解中是否存在根本性的误解,或者只是上面的代码是编写的,而我在实际且更长的项目中遇到了错误。
与此同时,我将继续阅读 developer.apple.com 上的 Obj-C PDF。
编辑:
抱歉,这是我最终意图的一个不好的例子。
假设我想编写一个方法来执行此操作:
-(void)fillInMissingGenre
{
if([self.title caseInsensitiveCompare:@"Romeo and Juliet"] == 0){
self.genre = @"Tragedy";
}
}
在示例代码中,Play
由数据控制器实例化,该控制器返回到Root< /strong> 控制器,然后通过详细控制器的 Play 属性传递到详细控制器。 Root 和 Detail 都应该能够调用 Play 的方法,除非前面的实体之一决定取消引用该对象。
我开始怀疑这是我的问题的原因,因为有时 Play 对象对所有内容都返回 nil,然后有时我会得到 Exec_Bad_Access。如果我学会了使用那个调试器就好了。 。 。
I'm been trying to understand some of the nuances of the iOS system and one item that isn't clear to me. I was building a class to encapsulate methods to access the calendar, which included creating the events, event store singleton, and an async fetch operation.
There was a mixture of class variables and instance variables but I had a hard time getting that to work.
My question is this.
Lets look at this project.
http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown
The Play class encapsulates info about a play.
@interface Play : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSArray *characters;
@property (nonatomic, strong) NSString *genre;
@property (nonatomic, strong) NSDate *date;
@end
#import "Play.h"
@implementation Play
@synthesize title, characters, genre, date;
@end
Because of the synthesize, there is no dealloc method, if I understand that correctly.
Now, there are no other methods defined.
In the sample project's Details Controller, the object isn't initialized at all, but the properties are accessible by reference, which confused me. If I try to write methods in the Play, the methods can't change the values in the properties. For example, if you tried to merge the Data Controller class with the Play class, I noticed that methods can't set and get values from the properties.
For example, if I add into the Play class:
(void)createSamplePlay
{
self.title = @"Play 1";
self.characters = [NSArray arrayWithObject:[NSString stringWithFormat:@"Character 1"]];
self.genre = @"Genre 1";
self.date = [NSDate date];
}
Then I call the method inside Details Controller, nothing actually gets changed. If I move the method to inside the Details Controller, declare a static typed property Play *play
, then calling the method [self createSamplePlay];
and changing to self
to play
, then of course, the methods works.
I didn't know if there was a fundamental misunderstanding in my understanding or it is just the code above is write and I got error in my actual and much longer project.
In the meantime, I'm going to keep reading the Obj-C PDFs from developer.apple.com.
edit:
Sorry that was a bad example of my final intent.
Let say I wanted to write a method that does this:
-(void)fillInMissingGenre
{
if([self.title caseInsensitiveCompare:@"Romeo and Juliet"] == 0){
self.genre = @"Tragedy";
}
}
In the sample code, the Play
is instanced by a Data Controller, which is returned to the Root Controller, then passed to a Detail Controller, through the Detail Controller's Play property. Both the Root and the Detail should be able to call Play's methods, unless one of the previous entities have decided to dereference the object.
I'm starting to suspect this is the cause of my problems, since sometimes the Play
object returns nil for everything and then sometimes I get a Exec_Bad_Access. If I only had learned to use that debugger. . .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是错误的。没有dealloc,因为此代码是为ARC(自动引用计数)编写的。它与
@synthesize
无关。除此之外,我认为您错过了类和对象之间的区别。这些属性在
Play
类上定义,但要实际使用它们(存储数据),您需要创建一个对象或 >类的实例。您可以这样做:现在您可以使用对象
myPlay
(或更准确地说,myPlay
是指向对象的指针)来执行类似的操作`myPlay.title = @"播放 1";如果不创建实例,它将不起作用:
因为在这里,
myPlayNoInstance
是一个不指向已分配对象的指针。That's wrong. There is no
dealloc
because this code is written for ARC (Automatic Reference Counting). It has nothing to do with@synthesize
.Other than that, I think you are missing the difference between a class and an object. The properties are defined on the
Play
class, but to actually use them (to store data), you need to create an object or instance of the class. You do that with:Now you can use the object
myPlay
(or more exact,myPlay
is a pointer to an object) to do stuff like `myPlay.title = @"Play 1";If you don't create the instance, it won't work:
Because here,
myPlayNoInstance
is a pointer that does not point to an allocated object.如果我正确理解您的意图,则
createSamplePlay
是一个工厂方法。如果这个假设是正确的,您应该在Play
类中声明它(注意+
- 它是类方法的指示符),并重写实现,如下所示 :你可以在你的代码中使用它:
不,dealloc 不存在是因为自动引用计数(
strong
修饰符的存在告诉您您处于 ARC 区域)。If I understand your intent correctly, the
createSamplePlay
is a factory method. If this assumption is correct, you should declare it in thePlay
class (notice the+
- it's an indicator of a class method), and rewrite the implementation like this:Later you can use it in your code:
No, dealloc is not there because of Automatic Reference Counting (the presence of
strong
modifier tells you that you're in the ARC land).