简体   繁体   English

CoreData Fetch从Double创建字符串

[英]CoreData Fetch creates String from Double

I am querying HealthKit and saving the entries to CoreData: Here is my Fetch Request: 我正在查询HealthKit并将条目保存到CoreData:这是我的提取请求:

func queryCoreDataData () -> NSArray {
var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!

var request = NSFetchRequest(entityName: "HKActiveEnergyBurned")
request.returnsObjectsAsFaults = false
var hkActiveEnergyBurned = context.executeFetchRequest(request, error: nil)! as [HKActiveEnergyBurned]
var activeEnergyBurnedData = (hkActiveEnergyBurned as NSArray).valueForKey("activeEnergyBurned_data") as NSArray  
println("CoreData Fetch activeEnergyBurnedData: \(activeEnergyBurnedData)")

return activeEnergyBurnedData
}

I print each entry to the console. 我将每个条目打印到控制台。 The console shows this: 控制台显示以下内容:

Saving to CoreData:
CD save activeEnergyBurned: 740.0
CD save activeEnergyBurned: 725.0
CD save activeEnergyBurned: 4.0
CD save activeEnergyBurned: 152.0
CD save activeEnergyBurned: 459.0

When I later Fetch the data some of the entries have become Strings! 当我稍后提取数据时,某些条目已变成字符串!

CoreData Fetch activeEnergyBurnedData: (
"739.9999999999999",
    725,
    4,
    152,
    "459.0000000000001"
)

Here is a picture of my CoreData Model 这是我的CoreData模型的图片

CoreData模型

and the NSManaged Object: 和NSManaged对象:

import Foundation
import CoreData

class HKActiveEnergyBurned: NSManagedObject {

    @NSManaged var activeEnergyBurned_data: NSNumber
    @NSManaged var activeEnergyBurned_date: NSDate

}

I tried to google this, but nothing comes up. 我试图用谷歌搜索,但是什么都没有出现。 Maybe I am asking the wrong question. 也许我问错了问题。 Does anyone know why this happen and how I can make sure that the values stay as double? 有谁知道为什么会这样,以及我如何确保这些值保持两倍? (You may notice that the NSManaged Object comes up as NSNumber, I am not sure why that happens either). (您可能会注意到NSManaged Object以NSNumber出现,我也不知道为什么会这样)。 Any help would be very much appreciated. 任何帮助将不胜感激。 Thank you ! 谢谢 !

The type of the activeEnergyBurned_data is NSNumber however when you print it to console the description method is called. activeEnergyBurned_data的类型为NSNumber但是当您将其打印为控制台时,将调用description方法。 This method is needed to provide textual representation of the object. 需要此方法来提供对象的文本表示。 So that is why you see numbers printed in quotes. 所以这就是为什么您看到数字都用引号引起来。 This method is inherited from NSObject protocol so that any object has a chance to provide textual representation for debugging or logging, etc. 此方法继承自NSObject协议,因此任何对象都有机会提供用于调试或记录等的文本表示形式。

Your numbers are printing surrounded by quotes when they contain a decimal point, but they are still stored as numbers. 当您的数字包含小数点时,会用引号引起来,但它们仍会存储为数字。 You can reproduce this trivially: 您可以轻松地重现此内容:

let array = [ 739.9, 725 ] as NSArray
println("\(array)")

Output: 输出:

(
    "739.9",
    725
)

String interpolation uses the description method (in this case), and this is just the way -[NSArray description] works. 字符串插值使用description方法(在这种情况下),这就是-[NSArray description]工作方式。 It's not a bug. 这不是错误。 If you don't like it, you'll have to convert the array to a string “manually” instead of relying on description . 如果您不喜欢它,则必须“手动”将数组转换为字符串,而不要依赖description

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM