0

I'm trying to do an action in my app when it is a spcific date. Here is an "example"


if it is June 5, 2017 {

} else {

}


Obviously, the bold section won't work in Swift. That's the part that I'm trying to figure out. How would I accomplish this?

iFunnyVlogger
  • 437
  • 1
  • 6
  • 17
  • why don't you go for a trigger like alarm manager etc? – Asim Khan May 14 '17 at 00:22
  • @Technacron How would I do this? Thanks for the help, I'm still learning swift – iFunnyVlogger May 14 '17 at 00:24
  • Have you seen this post: [link] (http://stackoverflow.com/questions/39513258/get-current-date-in-swift-3) check out the post by Jorge it shows how to access the day, month, and year. You could then compare those three variables to the desired date – Max K May 14 '17 at 00:27

2 Answers2

2

Based off of Get current date in Swift 3?

let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)

let year =  components.year
let month = components.month
let day = components.day

if(month == 6 && day == 5 && year == 2017) { //June 5 2017
  //Code
}else{
  //Code
}

Make sure to use lets for performance since you don't need to change those variables.

Community
  • 1
  • 1
Max K
  • 656
  • 1
  • 6
  • 22
  • 1
    Thanks! But, one more question lol. How do I remove a table view cell by doing a if statement like this: if ____ {remove static cell } else { keep cell in tableview} – iFunnyVlogger May 14 '17 at 01:27
  • @iFunnyVlogger Sorry, I don't know Swift. Perhaps explore the Swift documentation. – Max K May 14 '17 at 14:50
  • you need to do checking for the array data. if the date false... then remove the data... then reload tableview. – Muhammad Asyraf Dec 09 '20 at 09:02
  • @iFunnyVlogger if u wish to remove the row while the table is load.. really not suggested. because this could cause runtime error if the user happen to click the row that is currently deleted. i suggest to remove the data instead and reload the table view... but if you wish to remove the row try use tableview.deleteRows check this link https://stackoverflow.com/questions/40156274/deleting-a-row-from-a-uitableview-in-swift-3 – Muhammad Asyraf Dec 09 '20 at 09:06
0

I am assuming that you are trying to launch some of your code at a specific date. For this you may be assisted by UILocalNotification class. You just need to create a date alarm for this, and launch your code when it fires. I hope this thread would help you.

This approach works even when the user is not using your app at the specific datetime. If you are sure that user would be using your app so you can go with this answer

Community
  • 1
  • 1
Asim Khan
  • 572
  • 6
  • 34