@NSManaged是做什么的? [英] What does @NSManaged do?

查看:335
本文介绍了@NSManaged是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在各种场合遇到这个关键字。我知道它想做什么。但我真的想要更好地了解它。

I have encountered this keyword in various occasions. I kind of know what it's suppose to do. But I really want a better understanding of it.

我注意到 @NSManaged - 不是基于文档,而是通过重复使用:

What I noticed about @NSManaged - based not on documentation, but through repeated use:


  1. 它神奇地替代了键值编码。

  2. 它大致相当于Objective-C中的 @dynamic (我不太了解)

  3. 我需要它来从 Parse SDK 子类化 PFObject 。它通常使用KVC从/向后端读取/写入值。

  4. @NSManaged 前缀任何变量将关闭编译器当我不初始化初始化器。

  1. It magically replaces key value coding.
  2. It is roughly equivalent to @dynamic in Objective-C (which I don't know much about)
  3. I need it to subclass PFObject from the Parse SDK. It normally uses KVC to read/write values from/to the backend.
  4. Prefixing any variable with @NSManaged will shut the compiler up when I don't initialize within the initializer.

正式定义(在Core Data Apple Docs中):


Core Data提供了NSManagedObject类的子类中的属性的底层存储和实现。在受管对象子类中的每个属性定义之前添加@NSManaged属性,该属性对应于Core Data模型中的属性或关系。与Objective-C中的@dynamic属性一样,@NSManaged属性通知Swift编译器将在运行时提供属性的存储和实现。但是,与@dynamic不同,@NSManaged属性仅用于Core Data支持。

Core Data provides the underlying storage and implementation of properties in subclasses of the NSManagedObject class. Add the @NSManaged attribute before each property definition in your managed object subclass that corresponds to an attribute or relationship in your Core Data model. Like the @dynamic attribute in Objective-C, the @NSManaged attribute informs the Swift compiler that the storage and implementation of a property will be provided at runtime. However, unlike @dynamic, the @NSManaged attribute is available only for Core Data support.


具有 @NSManaged 的变量应免于编译时检查

Variables with @NSManaged shall be exempt from compile time checks for something.


$ b

I've read the formal documentation and various other SO questions regarding this matter:

@synthesize vs @动态,有什么区别?

什么是@dynamic使用的常见情况?

我本能地识别一些 >我应该使用它的场景。我部分知道它做什么。但我所寻求的是更清楚地了解它的作用。

I instinctively recognize some scenarios where I should use it. I partially know what it does. But what I seek is purer understanding of what it does.

进一步观察:

Further Observations:

A PFObject <在 Parse SDK 中的code>依赖键值编码来访问其值。 PFObject 提供以下访问器:

A PFObject in the Parse SDK relies on Key Value Coding to access its values. The PFObject provides the following accessors:

objectForKey:

let score = results.objectForKey("descriptionOfResult") 
//returns the descriptionOfResult value from the results object

setObject:forKey:

results.setObject("The results for a physics exam", forKey: "descriptionOfResult") 
//sets the value of descriptionOfResult 

根据我的理解, @NSManaged magically 了解我已声明的变量会自动使用上述访问器 get set

To my understanding, @NSManaged magically understands that the variable I've declared automatically uses the above accessors to get and set. I'd like to know how it does that (if what I understand is true), and whatever else it does.

推荐答案

我想知道它是如何做的(如果我明白是真的)是的,它有点像@dynamic - 技术上它可能是相同的甚至。语义上有一点区别:

Yes, it kinda really acts like @dynamic -- technically it might be identical even. Semantically there is a slight difference:

@dynamic说'编译器,不检查我的属性是否也实现。可能没有代码可以看到,但我保证它会在运行时工作

@dynamic says 'compiler, don't check if my properties are also implemented. There might be no code you can see but I guarantee it will work at runtime'

@NSManaged现在说'编译器,不要检查这些属性,因为我有Core Data要注意的实现 - 它将在运行时'

@NSManaged now says 'compiler, don't check those properties as I have Core Data to take care of the implementation - it will be there at runtime'

,所以你甚至可以说:@NSManaged是语法糖,是一个更狭窄的版本的动态:)

so you could even say: @NSManaged is syntactic sugar that is a more narrow version of dynamic :)

https: //github.com/KyoheiG3/DynamicBlurView/issues/2

这里有人甚至使用@NSManaged没有CD,因为他想要@dynamic行为

https://github.com/KyoheiG3/DynamicBlurView/issues/2
here someone even used @NSManaged without CD because he wanted the @dynamic behaviour

这篇关于@NSManaged是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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