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

查看:53
本文介绍了@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 类的子类中提供属性的底层存储和实现.在与 Core Data 模型中的属性或关系相对应的托管对象子类中的每个属性定义之前添加 @NSManaged 属性.就像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.

我已阅读有关此事的正式文档和其他各种 SO 问题:

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

@synthesize vs @dynamic,有什么区别?

@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.

进一步观察:

Parse SDK 中的 PFObject 依赖于 Key Value Coding 来访问其值.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 神奇地明白我声明的变量自动使用上述访问器来get设置.我想知道它是如何做到这一点的(如果我的理解是真的),以及它还能做什么.

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 现在说'编译器,不要检查这些属性,因为我有核心数据来处理实现 - 它会在运行时存在'

@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
这里有人甚至在没有 CD 的情况下使用了 @NSManaged,因为他想要@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天全站免登陆