TdoubleG TdoubleG - 2 months ago 12
iOS Question

Swift: Fetch CoreData as Array

I want to fetch all Saved Data in the sqlite table.

I'm currently doing this:

func GetAllData() -> NSArray
var error : NSError? = nil;
var request : NSFetchRequest = NSFetchRequest(entityName: "Locations");
let result : [AnyObject] = managedObjectContext!.executeFetchRequest(request, error:&error)!;
var elements : NSMutableArray = NSMutableArray();
for fetchedObject in result
return elements;

I have no problems to fetch Data in Objective-C but in swift I dont get it!

The saving of the data works fine. I have two rows "Name" and "Category". How can I show all saved data?


You should load all your Objects from CoreData into an Array/Dict of NSManaged Objects.

For Example:

var locations  = [Locations]() // Where Locations = your NSManaged Class

var fetchRequest = NSFetchRequest(entityName: "Locations")
locations = context.executeFetchRequest(fetchRequest, error: nil) as [Locations]

// Then you can use your propertys.

for location in Locations {