Swift: 为什么从 NSManagedObject 继承会破坏我的属性?
Swift: why does inheriting from NSManagedObject ruin my properties?
我是一个完全 Swift/IOS 的新手,关于 CoreData 的一些东西我根本无法理解。
我有一个 class,其中包含一些由指定初始化程序初始化的非可选属性。然后,如果我将 class 设置为从 NSManagedObject 继承,然后突然出现错误
Stored property X requires an initial value or should be @NSManaged.
为什么 Swift 突然认为我的属性没有初始化,即使它们显然是?
此外,我读到@NSManaged "tells the compiler that the storage and implementation of the properties will be handled by CoreData",但这到底是什么意思?
任何答案将不胜感激..
实际上我昨天刚读到这篇文章。
Yes, it kinda really acts like @dynamic -- technically it might be
identical even. Semantically there is a slight difference:
@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 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'
so you could even say: @NSManaged is syntactic sugar that is a more
narrow version of dynamic :)
取自此
swift 的一大推动是使语言极其安全,在这种情况下,检查属性是否在编译时实现。如果我理解正确,CoreData 并不完全符合这些编译时检查,因此添加 @NSManaged
让编译器知道变量将被处理。
来自 Apple:
You use the @NSManaged attribute to inform the Swift compiler that
Core Data provides the storage and implementation of a declaration at
runtime.
我是一个完全 Swift/IOS 的新手,关于 CoreData 的一些东西我根本无法理解。
我有一个 class,其中包含一些由指定初始化程序初始化的非可选属性。然后,如果我将 class 设置为从 NSManagedObject 继承,然后突然出现错误
Stored property X requires an initial value or should be @NSManaged.
为什么 Swift 突然认为我的属性没有初始化,即使它们显然是?
此外,我读到@NSManaged "tells the compiler that the storage and implementation of the properties will be handled by CoreData",但这到底是什么意思?
任何答案将不胜感激..
实际上我昨天刚读到这篇文章。
Yes, it kinda really acts like @dynamic -- technically it might be identical even. Semantically there is a slight difference:
@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 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'
so you could even say: @NSManaged is syntactic sugar that is a more narrow version of dynamic :)
取自此
swift 的一大推动是使语言极其安全,在这种情况下,检查属性是否在编译时实现。如果我理解正确,CoreData 并不完全符合这些编译时检查,因此添加 @NSManaged
让编译器知道变量将被处理。
来自 Apple:
You use the @NSManaged attribute to inform the Swift compiler that Core Data provides the storage and implementation of a declaration at runtime.