/* |
Copyright (C) 2014 Apple Inc. All Rights Reserved. |
See LICENSE.txt for this sample’s licensing information |
|
Abstract: |
|
Managed object class for the Quake entity. |
|
*/ |
|
|
import Foundation |
import CoreData |
|
class Quake: NSManagedObject { |
// MARK: Types |
|
/// An enumeration to specify the property names of interest in the JSON data. There may be other properties in the dictionary passed to updateFromDictionary(), but they are ignored. |
|
private enum JSONQuakeProperty: String { |
case Code = "code" |
case Magnitude = "mag" |
case PlaceName = "place" |
case DetailURL = "detail" |
case Time = "time" |
case Location = "geometry" |
} |
|
private struct JSONQuakeCoordinatesKey { |
static let coordinates = "coordinates" |
} |
|
// MARK: Properties |
|
@NSManaged var magnitude: Float |
|
@NSManaged var placeName: String |
|
@NSManaged var time: NSDate |
|
@NSManaged var longitude: Float |
|
@NSManaged var latitude: Float |
|
@NSManaged var depth: Float |
|
@NSManaged var detailURL: String |
|
@NSManaged var code: String |
|
// MARK: Convenience Methods |
|
func updateFromDictionary(quakeDictionary: [String: AnyObject]) { |
for (key, value) in quakeDictionary { |
// Ignore the key / value pair if the value is NSNull. |
if value is NSNull { |
continue |
} |
|
if let property = JSONQuakeProperty(rawValue: key) { |
switch property { |
case .Code: |
code = value as String |
|
case .Magnitude: |
magnitude = value as Float |
|
case .PlaceName: |
placeName = value as String |
|
case .DetailURL: |
detailURL = value as String |
|
case .Time: |
let timeInterval: NSTimeInterval = (value as NSNumber).doubleValue / 1000.0 |
time = NSDate(timeIntervalSince1970: timeInterval) |
|
case .Location: |
let coordinates = value[JSONQuakeCoordinatesKey.coordinates]! as [Float] |
|
// The longitude, latitude, and depth values are stored in an array in JSON. |
// Access these values by index directly. |
longitude = coordinates[0] |
latitude = coordinates[1] |
depth = coordinates[2] |
} |
} |
} |
} |
} |