TheGooooogle
TheGooooogle

Reputation: 263

Creating Swift Array from coredata

My entity in coredata is like this value1,value2,value3,value4

I am trying to display graph which takes as array like this

AASeriesElement()
                    .name("Range Chart")
                    .type(.columnrange)
                    .data([
                        [120, 80],
                        [130, 95],
                        [135, 100],
                        [125, 95]
                        ])
                    .toDic()!, 
                AASeriesElement()
                    .name("Line Chart")
                    .data([72, 80, 90, 72, 80])
                    .toDic()!

Where data for range chart is [[Value1,Value2],[Value1,Value2],[Value1,Value2]] so on//each row and data from line chart is [row1val3,row2val3,row3val3] and so on.

I am bit new to swift so confused how can i do it?

Current I am getting coredata rows as follows (in view load as function call)

let managedObjectContext=self.persistentContainer.viewContext

        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ItemName")

        // Add Sort Descriptor
        let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]
        do {
            myItems = try managedObjectContext.fetch(fetchRequest) as! [Item]
            print( myItems.count)
        } catch {
            print("Failed to retrieve record")
            print(error)
        }

Need to create those array out of it.

Upvotes: 0

Views: 71

Answers (1)

Joakim Danielson
Joakim Danielson

Reputation: 51973

Simply use map to convert your entity properties to an array

let data = myItems.map { [$0.value1, $0.value2] }

Upvotes: 0

Related Questions