gpt4 book ai didi

ios - 在 Swift 中从 CoreData 获取值

转载 作者:搜寻专家 更新时间:2023-11-01 05:51:31 25 4
gpt4 key购买 nike

我正在尝试从 CoreData 中提取并打印两个值。一是速度,二是度数。然而事情并没有按计划进行。首先,这是代码的一部分:

  // Start fetching from CoreData

request = NSFetchRequest(entityName: "WindData")

request.returnsObjectsAsFaults = false

results = context.executeFetchRequest(request, error: nil)

if results!.count > 0 {

for result: AnyObject in results! {

println(result)
println(result.speed)
println(result.degrees)


}
}
else {
println("No data")
}

当我简单地打印结果时,我得到了值:

<NSManagedObject: 0x7f944a734e80> (entity: WindData; id: 0xd000000000740000 <x-coredata://762EB8C2-DDCF-43F5-8DFC-FAB9A29532E1/WindData/p29> ; data: {
degrees = 190;
speed = 8; })

println(result.speed) 在控制台中写入:2.34181e-38并且 println(result.degrees) 不允许我编译并告诉我 AnyObject 没有名为 degrees 的成员(尽管它显然有)

对于任何菜鸟错误,我深表歉意,这是我第一次在 Stackoverflow 上发帖。

完整代码如下:

    //
// ViewController.swift
// KitesurfioApp
//
// Created by Lasse Kristensen on 24/10/14.
// Copyright (c) 2014 Lasse Kristensen. All rights reserved.
//

import UIKit
import CoreData

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()


// Set logo in nav bar

navigationItem.titleView = UIImageView(image: UIImage(named: "logo"))



// Global save values

var appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

var context: NSManagedObjectContext = appDel.managedObjectContext!



// JSON Fetching

let urlPath = "http://api.openweathermap.org/data/2.5/weather?lat=55.564120&lon=12.568605"

let url = NSURL(string: urlPath)

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in

if (error != nil) {
println(error)
}
else {


// Delete old entries in CoreData

var request = NSFetchRequest(entityName: "WindData")

request.returnsObjectsAsFaults = false

var results = context.executeFetchRequest(request, error: nil)

for result in results! {

context.deleteObject(result as NSManagedObject)
context.save(nil)

}

// Start fetching JSON


let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary


var item = jsonResult["wind"] as NSDictionary

var degrees:Int = item["deg"] as NSInteger
var speed:Float = item["speed"] as Float


// Start saving JSON

var newItem = NSEntityDescription.insertNewObjectForEntityForName("WindData", inManagedObjectContext: context) as NSManagedObject

var speedValue:NSNumber = speed as NSNumber
var degreesValue:NSNumber = degrees as NSNumber

newItem.setValue(speedValue, forKey: "speed")
newItem.setValue(degreesValue, forKey: "degrees")

context.save(nil)



}

})

task.resume()

// Start fetching from CoreData

var request = NSFetchRequest(entityName: "WindData")

request = NSFetchRequest(entityName: "WindData")

request.returnsObjectsAsFaults = false

var results = context.executeFetchRequest(request, error: nil)

if results!.count > 0 {

for result: AnyObject in results! as [WindData] {

println(result)
println(result.speed)

}
}
else {
println("No data")
}


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

您需要将 AnyObject 转换为具有 degree 等属性的类。AnyObject 不知道您需要的类属性将数组 [AnyObject] 向下转换为您的类数组

for result in results! as [NSManagedObject] {

println(result)
println(result.valueForKey("speed")!)
println(result.valueForKey("degrees")!)
}

关于ios - 在 Swift 中从 CoreData 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26545621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com