自动属性如何在xcode 4.4中合成? [英] How does the automatic property synthesize in xcode 4.4 work?

查看:129
本文介绍了自动属性如何在xcode 4.4中合成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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:

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

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

    return __managedObjectContext;
}

在这个人的代码中,我看到他只是将他的手动合成访问器用于两者得到并设定。我在我的代码中想到,我可以用 self.managedObjectContext 替换 __ managedObjectContext ,但是没有。如果我这样做,我会收到一个错误,告诉我我正在尝试分配给 readonly 属性。这是有道理的,因为该属性被其他编码器定义为只读。

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 __managedObjectContext with self.managedObjectContext, but nope. If I do this, I get an error telling me that I am trying to assign to a readonly property. This makes sense, because that property is defined as readonly, by this other coder.

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

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

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.

如果我手动合成属性,就像我引用的代码一样,一切都恢复正常,但这不是使用新的自动合成。如果我删除 readonly ,我可以像预期的那样设置这个属性,但我觉得我不明白他为什么只读它,所以我打赌我是打破那里的东西。

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我如何使用setter设置?

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?

谢谢

推荐答案

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

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

@synthesize foo = _foo;

因此,您可以使用self.foo或object.foo正确访问数据。

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

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

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

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

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

这篇关于自动属性如何在xcode 4.4中合成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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