xcode 4.4 中的自动属性合成是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11752443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:05:36  来源:igfitidea点击:

How does the automatic property synthesize in xcode 4.4 work?

iosxcodeproperties

提问by Ryan

I'm new to Objective-C and XCode, but I was happy to see that XCode 4.4 automatically synthesizes my properties for me, now. I figure this means that I no longer have to type out @synthesize for my properties, and that I get to access them using self.propertyName = @"hi";, for example.

我是 Objective-C 和 XCode 的新手,但我很高兴看到 XCode 4.4 现在自动为我合成我的属性。我认为这意味着我不再需要为我的属性输入 @synthesize,并且我可以使用self.propertyName = @"hi";例如访问它们。

I'm trying to re-write some example code so that I can understand it better, but this code implements a custom getter method. In the example code, the property is manually synthesized, as @synthesize managedObjectContext = __managedObjectContext;. The custom getter looks like this:

我正在尝试重新编写一些示例代码,以便更好地理解它,但此代码实现了自定义 getter 方法。在示例代码中,属性是手动合成的,如@synthesize managedObjectContext = __managedObjectContext;. 自定义 getter 如下所示:

- (NSManagedObjectContext *)managedObjectContext {
    if (__managedObjectContext != nil) {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }

    return __managedObjectContext;
}

In this person's code, I see he's just using his manually synthesized accessor to both get and set. I figured in my code, I could just replace the __managedObjectContextwith self.managedObjectContext, but nope. If I do this, I get an error telling me that I am trying to assign to a readonlyproperty. This makes sense, because that property is defined as readonly, by this other coder.

在这个人的代码中,我看到他只是使用他手动合成的访问器来获取和设置。我想在我的代码,我可以只更换__managedObjectContextself.managedObjectContext,但没了。如果我这样做,我会收到一条错误消息,告诉我我正在尝试分配给一个readonly属性。这是有道理的,因为该属性被其他编码器定义为只读。

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

So, I figure something about how he's manually synthesizing his property means that if he uses that specified setter, it allows him to set a readonly property somehow.

所以,我认为他如何手动合成他的属性意味着如果他使用指定的 setter,它允许他以某种方式设置只读属性。

If I manually synthesize the property, like in the code I am referencing, everything goes back to working, but that's not making use of the new automatic synthesize. If I remove the readonly, I can set this property, as expected, but I feel like I'm not understanding why he has it as readonly, so I bet I'm breaking something there.

如果我手动合成属性,就像在我引用的代码中一样,一切都会恢复工作,但这并没有利用新的自动合成。如果我删除readonly,我可以按预期设置此属性,但我觉得我不明白为什么他将其设为只读,所以我敢打赌我在那里破坏了某些东西。

So, am I mis-using the new automatic synthesize? How do I set this using the setter, if the automatic synthesize is not creating it for me, because of readonly?

那么,我是否误用了新的自动合成?如果自动合成没有为我创建它,我如何使用 setter 设置它,因为它是只读的?

Thank you

谢谢

回答by Jody Hagins

When XCode auto-synthesizes, it simulates the following...

当 XCode 自动合成时,它会模拟以下...

@synthesize foo = _foo;

So, you can access the data appropriately with self.foo or object.foo.

因此,您可以使用 self.foo 或 object.foo 适当地访问数据。

However, in the implementation of accessor methods (and initializers and dealloc), you should use the iVar directly.

但是,在访问器方法(以及初始化程序和dealloc)的实现中,您应该直接使用iVar。

Note, those iVars have two underscores. And, they are being manipulated in the accessor method. Use _managedObjectContext, and you should be good to go.

请注意,这些 iVar 有两个下划线。并且,它们在访问器方法中被操作。使用 _managedObjectContext,你应该很高兴。