cdub
cdub

Reputation: 25711

Getting data out of an NSManagedObject

I have a fetch to core data that returns a NSManagedObject like so

let results = try context.fetch(data);
let resultset = results as! [NSManagedObject];

I have a string array created like so:

var db: [String] = [];

My core data has a column called blogs.

How do I get that entire column into my db variable?

Blogs column is a string.

New to core data too.

Upvotes: 1

Views: 1084

Answers (1)

vadian
vadian

Reputation: 285082

Use the map function

let results = try context.fetch(data) as! [NSManagedObject]
db = results.map { $0.value(forKey: "blogs") as! String }

or – preferable – if your are using NSManagedObject subclass and generic entity

let results = try context.fetch(data)
db = results.map { $0.blogs }

And remove the semicolons in your code by the way...

Upvotes: 3

Related Questions