Shaideru

All things iOS & Swift

Fetching attributes in Core Data

Screenshot 2015-02-28 16.54.54

Create an Array of AnyObject, you have to keep in mind that to access the index subscript you must have an array

var chartData: Array = []

Create an instance of AppDelegate and NSManagedObjectContext

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!

Create a fetch request an execute it

var fetchRequest = NSFetchRequest(entityName: “ActivityEntity”)
chartData = context.executeFetchRequest(fetchRequest, error: nil) as [ActivityEntity]

To access the attributes of the entity, create an instance of NSManagedObject and use valueForKeyPath(“Attribute’s name”)

var data: NSManagedObject = chartData[Int(index)] as NSManagedObject
var percentage = data.valueForKeyPath(“activityPercentage”) as Int

Leave A Comment

Your email address will not be published.