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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.