How can I have my app run my code every time the app is viewed and not just every time it is opened? Right now I have to exit it, and open it again to run the code.
My app pulls data via JSON and displays it in labels. All of my code is in viewDidLoad.
Is the answer in putting all of the code in viewDidAppear?
Thanks in advance
Here is the whole code:
//
// 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 {
#IBOutlet weak var labelSpeed: UILabel!
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "backgroundNotification:", name: UIApplicationWillEnterForegroundNotification, object: nil);
// Set logo in nav bar
navigationItem.titleView = UIImageView(image: UIImage(named: "logo"))
refresh();
}
func backgroundNotification(noftification:NSNotification){
refresh();
println("Refreshed after background")
}
func refresh() {
// 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
dispatch_async(dispatch_get_main_queue(),{
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:Float = item["deg"] as Float
var speed:Float = item["speed"] as Float
// Start saving JSON
var newItem = NSEntityDescription.insertNewObjectForEntityForName("WindData", inManagedObjectContext: context) as NSManagedObject
var speedValue:Float = speed as Float
var degreesValue:Float = degrees as Float
newItem.setValue(speedValue, forKey: "speed")
newItem.setValue(degreesValue, forKey: "degrees")
context.save(nil)
}
// Start fetching from CoreData
var request = NSFetchRequest(entityName: "WindData")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
if results!.count > 0 {
for result in results as [NSManagedObject] {
let degrees:Float = result.valueForKey("degrees")! as Float
let speed:Float = result.valueForKey("speed")! as Float
if speed > 6.0 {
self.labelSpeed.text = "Go kitesurf: \(speed) m/s \(degrees)"
}
else {
self.labelSpeed.text = "Stay in: \(speed) m/s \(degrees)"
}
}
}
else {
println("No data")
}
});
})
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You need to add UIApplicationWillEnterForegroundNotification and refresh your screen there.Put all code from viewDidLoad to helper function like refreh and call it when viewDidLoad or you come in foreground from background or whenever you want to refresh screen.
override func viewDidLoad() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "backgoundNofification:", name: UIApplicationWillEnterForegroundNotification, object: nil);
//put all code of your viewDidLoad to refresh
refresh();
}
func backgoundNofification(noftification:NSNotification){
refresh();
}
func refresh() {
//here is your all code to fetch data and all
}
Related
I am trying to serialize a GET request then make a movie object, then appending that movie object to a movies array which I will use to show info on the UI.
I am new and have struggled with this problem for some time now :(
If you look at the self.movies?.append(movie) shouldnt that work? I dont see any reasons as to when i try to get the first item i get fatal error index out of bounds which means I the Array is not filled yet.... Dont know what i am doing wrong :(
import UIKit
class ViewController: UIViewController {
var movies:[Movie]? = []
#IBOutlet weak var uiMovieTitle: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getMovieData()
print(self.movies?.count)
setUI()
}
#IBAction func yesBtn(_ sender: UIButton) {
print(movies?[5].title ?? String())
}
#IBAction func seenBtn(_ sender: UIButton) {
}
#IBAction func noBtn(_ sender: UIButton) {
}
#IBOutlet weak var moviePoster: UIImageView!
let urlString = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbfed4b9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=12"
func getMovieData(){
//Set up URL
let todoEndPoint: String = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbfed4b9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=12"
guard let url = URL(string: todoEndPoint) else {
print("Cant get URL")
return
}
let urlRequest = URLRequest(url: url)
//Setting up session
let config = URLSessionConfiguration.default
let session = URLSession.shared
//Task setup
let task = session.dataTask(with: urlRequest) { (data, URLResponse, error) in
//Checking for errors
guard error == nil else{
print("Error calling GET")
print(error)
return
}
//Checking if we got data
guard let responseData = data else{
print("Error: No data")
return
}
self.movies = [Movie]()
do{//If we got data, if not print error
guard let todo = try JSONSerialization.jsonObject(with: responseData, options:.mutableContainers) as? [String:AnyObject] else{
print("Error trying to convert data to JSON")
return
}//if data is Serializable, do this
if let movieResults = todo["results"] as? [[String: AnyObject]]{
//For each movieobject inside of movieresult try to make a movie object
for moviesFromJson in movieResults{
let movie = Movie()
//If all this works, set variables
if let title = moviesFromJson["title"] as? String, let movieRelease = moviesFromJson["release_date"] as? String, let posterPath = moviesFromJson["poster_path"] as? String, let movieId = moviesFromJson["id"] as? Int{
movie.title = title
movie.movieRelease = movieRelease
movie.posterPath = posterPath
movie.movieId = movieId
}
self.movies?.append(movie)
}
}
}//do end
catch{
print(error)
}
}
////Do Stuff
task.resume()
}
func setUI(){
//uiMovieTitle.text = self.movies![0].title
//print(self.movies?[0].title)
}
}
my Movie class:
import UIKit
class Movie: NSObject {
var title:String?
var movieRelease: String?
var posterPath:String?
var movieId:Int?
var movieGenre:[Int] = []
//public init(title:String, movieRelease:String, posterPath:String,movieId:Int) {
// self.movieId = movieId
//self.title = title
//self.movieRelease = movieRelease
//self.posterPath = posterPath
//self.movieGenre = [movieGenre]
//}
}
getMovieData calls the network asynchronously. Your viewDidLoad invokes this, then calls setUI() - but the networking is still ongoing when setUI is called.
Instead, call setUI when the networking is complete - after the self.movies?.append(movie) line. The UI code will need to happen on the main thread. So...
for moviesFromJson... // your existing code
...
self.movies?.append(movie)
}
// Refresh UI now movies have loaded.
DispatchQueue.main.async {
setUI()
}
import UIKit
class ViewController: UIViewController {
var movies:[Movie]? = []
#IBOutlet weak var uiMovieTitle: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getMovieDataCall(completionHandler: {data, error in self. getMovieDataCallBack(data: data, error: error)})
}
func getMovieDataCallBack(data: Data?, error: Error?) {
if error == nil {
let dictionary = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! Dictionary<String, AnyObject>
//do your appending here and then call setUI()
print("dictionaryMovie \(dictionary)")
} else {
showAlertView("", error?.localizedDescription)
}
}
func getMovieDataCall(completionHandler: #escaping (Data?, Error?) -> Void)){
//Set up URL
let todoEndPoint: String = "https://api.themoviedb.org/3/discover/movie?api_key=935f539acbfed4b9e5534ddeed3fb57e&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1&with_genres=12"
guard let url = URL(string: todoEndPoint) else {
print("Cant get URL")
return
}
let urlRequest = URLRequest(url: url)
//Setting up session
let config = URLSessionConfiguration.default
let session = URLSession.shared
//Task setup
let task = session.dataTask(with: urlRequest) { (data, URLResponse, error) in
if error != nil {
NSLog("GET-ERROR", "=\(error)");
completionHandler(nil, error)
} else {
let dataString = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
print(dataString!)
completionHandler(data, nil)
}
task.resume()
}
func setUI(){
}
I am trying to display a Json result (temperature and Humidity) on my view controller (respectively temperatureDisp and humidityDisp), but it does not seem to work.
class HomeVC: UIViewController {
#IBOutlet var usernameLabel: UILabel!
#IBOutlet var temperatureDisp: UILabel!
#IBOutlet var humidityDisp: UILabel!
#IBAction func logoutTapped(sender: AnyObject) {
let appDomain = NSBundle.mainBundle().bundleIdentifier
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain!)
self.performSegueWithIdentifier("goto_login", sender: self)
}
override func viewDidAppear(animated: Bool) {
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let isLoggedIn:Int = prefs.integerForKey("ISLOGGEDIN") as Int
if (isLoggedIn != 1) {
self.performSegueWithIdentifier("goto_login", sender: self)
} else {
self.usernameLabel.text = prefs.valueForKey("USERNAME") as! NSString as String
}
}
override func viewDidLoad() {
super.viewDidLoad()
var url2 : String = "http://admin:xxxxxxx#xxxxxx/xxxxx.fr/metrics2.php"
var request2 : NSMutableURLRequest = NSMutableURLRequest()
request2.URL = NSURL(string: url2)
request2.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request2, queue: NSOperationQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult : NSArray! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: error) as! NSArray
if (jsonResult != nil) {
println(jsonResult)
} else {
println("There is a problem")
}
var temperature = jsonResult[0].valueForKey("temperature") as! String
var humidity = jsonResult[0].valueForKey("humidite") as! String
println(temperature)
println(humidity)
self.humidityDisp.text = temperature
})
}
}}
That is how the variable jsonResult looks :
(
{
Id = 117;
date = "2015-04-06";
humidite = "45.3";
login = raspberrypi;
luminosite = "\U00e9teinte";
temperature = "18.4";
time = "16:25:21";
}
)
I'm not sure what you mean by "it does not seem to work", but from your code I can assume at least that you are not getting the values you expect from your JSON result, but more likely your app is crashing like crazy. If you want to write applications in Swift you absolutely must understand optionals and learn how to properly work with them. Read The Swift Programming Languageāand thoroughly. As your code is now, by force unwrapping using as! and using implicitly unwrapped types (those followed by !) you are ignoring the entire concept of optionals and opening yourself up to crashes.
So, assuming that there is no network or parsing errors, and assuming that the JSON string you're parsing has an array as its root object, the following should work. I've taken the liberty of typing and unwrapping your variables appropriately, as well as cleaning up some of the cruft.
class HomeVC: UIViewController {
#IBOutlet var usernameLabel: UILabel!
#IBOutlet var temperatureDisp: UILabel!
#IBOutlet var humidityDisp: UILabel!
#IBAction func logoutTapped(sender: AnyObject) {
if let appDomain = NSBundle.mainBundle().bundleIdentifier {
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
}
self.performSegueWithIdentifier("goto_login", sender: self)
}
override func viewDidAppear(animated: Bool) {
let prefs = NSUserDefaults.standardUserDefaults()
let isLoggedIn = prefs.boolForKey("ISLOGGEDIN")
if isLoggedIn {
self.performSegueWithIdentifier("goto_login", sender: self)
} else {
if let username = prefs.stringForKey("USERNAME") {
self.usernameLabel.text = username
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
var url2 = "http://admin:xxxxxxx#xxxxxx/xxxxx.fr/metrics2.php"
var request2 = NSMutableURLRequest()
request2.URL = NSURL(string: url2)
request2.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request2, queue: NSOperationQueue()) { (response: NSURLResponse!,data: NSData!,error: NSError!) in
var parseError:NSError?
if let data = data, let jsonResults = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &parseError) as? NSArray {
println( jsonResults )
if let result = jsonResults.firstObject as? [String : AnyObject] {
if let humidity = result["humidite"] as? String {
self.humidityDisp.text = humidity
}
if let temperature = result["temperature"] as? String {
self.temperatureDisp.text = temperature
}
}
} else {
println("There is a problem: \(parseError)" )
}
}
}
}
Problem
I am new to IOS development, and have been struggling with Core Data. I am trying to create a settings page with a switch. I need to remember if this switch is turned on or off. I created a core data application, and have managed to figure out how to save the value of the switch to the attribute. The code below does this, I just can't figure out how to get the saved value of the switch back as on or off. How would I do this? Thanks!
Picture of Core Data
Code:
import UIKit
import CoreData
class preferencesStuff: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Settings")
request.returnsObjectsAsFaults = false;
var results:NSArray = context.executeFetchRequest(request, error: nil)!
if(results.count > 0){
if results[results.count-1] as NSObject == 1 {
println("ON")
}
}else{
println("NO RESULTS")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var fractSwitchValue: UISwitch!
#IBAction func fractSwitch(sender: AnyObject) {
if fractSwitchValue.on == true {
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var newSetting = NSEntityDescription.insertNewObjectForEntityForName("Settings", inManagedObjectContext: context) as NSManagedObject
newSetting.setValue(true, forKey: "fractionOnOff")
context.save(nil)
//println(newSetting)
}
else {
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var newSetting = NSEntityDescription.insertNewObjectForEntityForName("Settings", inManagedObjectContext: context) as NSManagedObject
newSetting.setValue(false, forKey: "fractionOnOff")
context.save(nil)
//println(newSetting)
}
}
}
I just did your example with CoreData. I just happened to store an NSString instead of a Bool. It's not optimized, but it should get you going.
import UIKit
import CoreData
class ViewController: UIViewController {
var state: NSString = ""
#IBOutlet weak var fractSwitchValue: UISwitch!
#IBAction func fractSwitch(sender: AnyObject) {
if fractSwitchValue.on == true {
state = "On"
save(state)
}
else {
state = "Off"
save(state)
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
var appDel = UIApplication.sharedApplication().delegate as AppDelegate
var context = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Settings")
var error: NSError?
var results = context.executeFetchRequest(request, error: &error) as [NSManagedObject]?
if let fetchedResults = results {
state = fetchedResults[fetchedResults.count - 1].valueForKey("fractionOnOff") as String!
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
if state == "On" {
fractSwitchValue.setOn(true, animated: true)
} else {
fractSwitchValue.setOn(false, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func save(string: NSString) {
let appDel = UIApplication.sharedApplication().delegate as AppDelegate
let context = appDel.managedObjectContext!
let entity = NSEntityDescription.entityForName("Settings", inManagedObjectContext: context)
let setting = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: context)
setting.setValue(string, forKey: "fractionOnOff")
println(string)
var error: NSError?
if !context.save(&error) {
println("Could not save \(error), \(error?.userInfo)")
}
}
}
I would use NSUserDefaults for this case. It's much easier to use than Core Data when storing user preferences. For example, you would store the property using
NSUserDefaults.standardUserDefaults().setBool(value, forKey: "fractionPreference")
and retrieve it using
let value = NSUserDefaults.standardUserDefaults().boolForKey("fractionPreference")
The following Swift code has to run twice in order to display the JSON data in a label. On the first run the label simply remains blank. It seems to me that the issue could be something along the lines of the JSON part of the code runs last, but I can't figure out why. I have included the code below. I'm still a rookie, so be gentle :D
class ViewController: UIViewController {
#IBOutlet weak var labelDegrees: UILabel!
#IBOutlet weak var labelSpeed: UILabel!
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:Float = item["deg"] as Float
var speed:Float = item["speed"] as Float
// Start saving JSON
var newItem = NSEntityDescription.insertNewObjectForEntityForName("WindData", inManagedObjectContext: context) as NSManagedObject
var speedValue:Float = speed as Float
var degreesValue:Float = degrees as Float
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.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
if results!.count > 0 {
for result in results as [NSManagedObject] {
let degrees:Float = result.valueForKey("degrees")! as Float
let speed:Float = result.valueForKey("speed")! as Float
if speed > 6.0 {
self.labelDegrees.text = "Go kitesurf: \(speed) m/s"
}
else {
self.labelDegrees.text = "Stay in: \(speed) m/s"
}
}
}
else {
println("No data")
}
}
You are actually fetching the data on another thread with dataTaskWithURL.So you need to write // Start fetching from CoreData code in dataTaskWithURL.As dataTaskWithURL will not immediately executed so // Start fetching from CoreData code will get called first while dataTaskWithURL is fetching the data.So replece the code with below
class ViewController: UIViewController {
#IBOutlet weak var labelDegrees: UILabel!
#IBOutlet weak var labelSpeed: UILabel!
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
dispatch_async(dispatch_get_main_queue(),{
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:Float = item["deg"] as Float
var speed:Float = item["speed"] as Float
// Start saving JSON
var newItem = NSEntityDescription.insertNewObjectForEntityForName("WindData", inManagedObjectContext: context) as NSManagedObject
var speedValue:Float = speed as Float
var degreesValue:Float = degrees as Float
newItem.setValue(speedValue, forKey: "speed")
newItem.setValue(degreesValue, forKey: "degrees")
context.save(nil)
}
// Start fetching from CoreData
var request = NSFetchRequest(entityName: "WindData")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: nil)
if results!.count > 0 {
for result in results as [NSManagedObject] {
let degrees:Float = result.valueForKey("degrees")! as Float
let speed:Float = result.valueForKey("speed")! as Float
if speed > 6.0 {
self.labelDegrees.text = "Go kitesurf: \(speed) m/s"
}
else {
self.labelDegrees.text = "Stay in: \(speed) m/s"
}
}
}
else {
println("No data")
}
});
})
task.resume()
}
I am attempting to pull out and print two values from CoreData. One is speed, the other is degrees. However things don't go as planned. First up, here's part of the code:
// 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")
}
When I simply print results I get the values:
<NSManagedObject: 0x7f944a734e80> (entity: WindData; id: 0xd000000000740000 <x-coredata://762EB8C2-DDCF-43F5-8DFC-FAB9A29532E1/WindData/p29> ; data: {
degrees = 190;
speed = 8; })
println(result.speed) writes in the consol: 2.34181e-38
and println(result.degrees) won't allow me to compile and tells me that AnyObject does not have a member named degrees (although it clearly does)
Sorry for any rookie mistakes, this is my first time posting on Stackoverflow.
Here is the full code:
//
// 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.
}
}
You need to cast the AnyObject to your class which have properties degree etc.AnyObject does not know about your class properties you need to downcast the array [AnyObject] to your class array
for result in results! as [NSManagedObject] {
println(result)
println(result.valueForKey("speed")!)
println(result.valueForKey("degrees")!)
}