更新类型Int32的CoreData属性 [英] Update CoreData attribute of type Int32
问题描述
我想获取核心数据属性的值,在获取此对象/值后,我想更新它。这是我的代码:
I'd like to get the value of an core data attribute and after getting this object/value I'd like to update it. This is my code:
var numberOfChanges:Int32?
numberOfChanges = theme.valueForKey("numberThemeChanged") as Int32
var newValueThemeChanges:Int32?
newValueThemeChanges = numberOfChanges+1
theme.setValue(newValueThemeChanges, forKey: "numberThemeChanged")
$ b b
我收到以下错误:
I'm getting the following errors:
-
类型Int32不符合协议'在此行代码中的
: numberOfChanges = theme.valueForKey(numberThemeChanged)as Int32
。 -
'Int32'不等于这行代码中的'AnyObject':
theme.setValue(newValueThemeChanges,forKey:numberThemeChanged)
Type 'Int32' does not conform to protocol 'AnyObject'
in this line of code:numberOfChanges = theme.valueForKey("numberThemeChanged") as Int32
.'Int32' is not identical to 'AnyObject'
in this line of code:theme.setValue(newValueThemeChanges, forKey: "numberThemeChanged")
你知道我怎么做/我的代码出了什么问题?
感谢您的回答!
Do you know how I can do it/what's wrong with my code? Thanks for your answer!
推荐答案
键值编码方法
func valueForKey(key: String) -> AnyObject?
func setValue(value: AnyObject?, forKey key: String)
类型 AnyObject
,对于整数属性,这些
是 NSNumber
的实例。
work with values of the type AnyObject
, for an integer attribute these
are instances of NSNumber
.
Int
和 NSNumber
之间有一个自动桥接,但不是
之间的 Int32
和 NSNumber
(这与
无关的事实,你定义的属性作为核心数据
模型检查器中的整数32)。
There is an automatic bridging between Int
and NSNumber
, but not
between Int32
and NSNumber
(and this has nothing to do with the
fact that you define the property as "Integer 32" in the Core Data
model inspector).
您有几个选项:
-
只需使用类型
Int
的变量:
var numChanges = theme.valueForKey("numberThemeChanged") as Int
numChanges++
theme.setValue(numChanges, forKey: "numberThemeChanged")
使用 Int32
,并从和转换为 NSNumber
显示:
Use Int32
and convert from and to NSNumber
explicitly:
var numberOfChanges = (theme.valueForKey("numberThemeChanged") as NSNumber).intValue // Int32
numberOfChanges++
theme.setValue(NSNumber(int: numberOfChanges), forKey: "numberThemeChanged")
使用Xcode - >编辑器 - >创建NSManagedObject子类...并选中为原始数据类型使用标量属性选项。
这将给你一个带有属性的托管对象子类。
Use Xcode -> Editor -> Create NSManagedObject subclass ... and check the "Use scalar properties for primitive data types" options. This will give you a managed object subclass with the property
@NSManaged var numberThemeChanged: Int32
,您可以访问该属性而不使用键值编码:
and you can access the property without Key-Value Coding:
var numberOfChanges = theme.numberThemeChanged
numberOfChanges++
theme.numberThemeChanged = numberOfChanges
li>
下面是一个完整的创建或更新示例:
Here is a complete "create-or-update" example:
var theme : Entity!
let request = NSFetchRequest(entityName: "Entity")
var error : NSError?
if let result = context.executeFetchRequest(request, error: &error) as [Entity]? {
if result.count > 0 {
// (At least) one object found, set `theme` to the first one:
theme = result.first!
} else {
// No object found, create a new one:
theme = NSEntityDescription.insertNewObjectForEntityForName("Entity", inManagedObjectContext: context) as Entity
// Set an initial value:
theme.setValue(0, forKey: "numberThemeChanged")
}
} else {
println("Fetch failed: \(error?.localizedDescription)")
}
// Get value and update value:
var numChanges = theme.valueForKey("numberThemeChanged") as Int
numChanges++
theme.setValue(numChanges, forKey: "numberThemeChanged")
// Save context:
if !context.save(&error) {
println("Save failed: \(error?.localizedDescription)")
}
println(numChanges)
这篇关于更新类型Int32的CoreData属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!