UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: EKEvent and ForEach

Forums > SwiftUI

So I have this Array of EKEvent,

var events: [EKEvent] { get }

Where I load the users calender event without any problem, the probem comes when I use my Foreach to try and acess the data:

ForEach(viewModel.events, id: \.eventIdentifier ) { event in
                HStack {
                    RoundedRectangle(cornerRadius: 25, style: .continuous)
                        .fill(Color(event.calendar.cgColor))
                        .frame(width: 4, height: 20)
                    VStack {
                        Text(event.title)

Using this I can acess some of the parameters inside events, like title, calender ect. But I get error if I try to acess startDate or endDate, saying there is no such parameter inside EKCalenderItem. If i use print(event) I get:

EKEvent <0x600001508780>
{
     EKEvent <0x600001508780>
{    title =        Daddade; 
     location =     (null); 
     calendar =     EKCalendar <0x6000031fb840> {title = Calendar; type = Local; allowsModify = YES; color = #1BADF8;}; 
     alarms =       (null); 
     URL =          ; 
     lastModified = 2021-04-04 21:18:28 +0000; 
     startTimeZone =    Europe/Stockholm (GMT+2) offset 7200 (Daylight); 
     endTimeZone =  Europe/Stockholm (GMT+2) offset 7200 (Daylight) 
}; 
     location =     (null); 
     structuredLocation =   (null); 
     startDate =    2021-04-04 15:45:00 +0000; 
     endDate =      2021-04-04 16:45:00 +0000; 
     allDay =       0; 
     floating =     0; 
     recurrence =   (null); 
     travelTime =   (null); 
     startLocation =    (null);
};

I suspect it has something to do with my foreach and the id, anyone got any deas how do acess, startDate?

2      

Hi,

I remember having some issues with EKEvent in ForEach, when trying to conform it to Identifiable. However accessing start and end dates should work fine. I have this example project on GitHub where I do just that :-)

2      

Huge thanks for that goldmine of information and inspiration!

Something is definitly wierd with EKEvent and for each, I made this:

var start: Date {
        (self.startDate)
    }

Inspired with how you handled calender color and suddenly it works.. so for sure something that is wrong there. Anyways, problem solved for me and I will reread some of your code to improve how I handle my own, huge thanks :)

2      

I am super glad it helped! :-)

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.