-1

I am building an iOS application using Swift in which, I have a UIWebView that invokes the ViewController to have it perform certtain actions.

So the ViewController performs a series of actions and one of them is to get determine what day of the week it its. Using the following code,

  let today = NSDate()
  let calendar =  NSCalendar(calendarIdentifier: NSGregorianCalendar)!
  var components:Int = calendar.component(.WeekdayCalendarUnit, fromDate: today)
  let oneDay = 60 * 60 * 24
  let yesterday = today.dateByAddingTimeInterval(-(Double(oneDay)))
  var yDayDay = calendar.component(.WeekdayCalendarUnit, fromDate: yesterday)
  println("\(yDayDay)")

Now, this line

var components:Int = calendar.component(.WeekdayCalendarUnit, fromDate: today)

Throws the the following error,

<NSInvalidArgumentException> -[_NSCopyOnWriteCalendarWrapper component:fromDate:]: unrecognized selector sent to instance 0x15e921d0

On a device running iOS 7.1. The code works perfectly fine on iOS8 and above. So I cannot understand exactly what is causing this error on iOS 7.1. I assume it also applies to iOS7.

The functionality that I am trying to achieve with that method is to determine the day of the week. So now I have something that works on iOS8, but not iOS7.1 and below, how do I achieve that on both OS's. I assume I am not going to get an answer and the way this will play out is, the question may get downvoted and closed. But I wanted to give it a go none the less.

cptdanko
  • 812
  • 2
  • 13
  • 22
  • The error says that _NSCopyOnWriteCalendarWrapper does not have a method named `component:fromDate:`, but you're (indirectly) trying to call that method on that class. The wrong object type has been passed somewhere, or something has not been properly retained. – Hot Licks Feb 02 '15 at 23:37
  • of course, there is a negative vote? once again, the SO, hostility is back. I am just waiting till the iOS devs on SO downvote and close this question. "Hot Licks", yes you are right, that is how I interpret this error as well. since I am a new iOS dev, I am not sure how to achieve what I want to achieve what I have in the method above such that it works on both iOS 7 and iOS8. – cptdanko Feb 02 '15 at 23:44
  • Well, see [this question](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error). It's Objective-C, not Swift, but the same basic principles apply. You will need to learn how to get the exception stack trace, and possibly need to learn how to do "zombie" detection. But you wanted to program in Swift, a language that is quite unstable, so I assume you "bought in" to this. – Hot Licks Feb 02 '15 at 23:48
  • @cptdanko Have you tried to do it as I suggested? – Leo Dabus Feb 03 '15 at 02:40
  • Thanks for the answer mate, but i wont be able to try it for the next few hours (5 hrs). All the iOS programming I do is outside my day job. Anyway, I did know how to use dayFormatter, so I was mainly interested in 1-7 Integer, so I will try your solution, I am sure it would work. Also any idea what changed between iOS 7 and iOS in regards to this? or the way in which I was trying to accomplish this (get the day of the week) was a completely new thing in iOS8? – cptdanko Feb 03 '15 at 02:56

1 Answers1

1

I will help you fix your code. First suggestion is to use NSCalendarUnit to subtract one day or week or month from any NSDate type of calculation.

let yesterday = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitDay, value: -1, toDate: NSDate(), options: nil)!

To find out the day of the week you will need to use NSDateFormatter:

let styler = NSDateFormatter()
styler.dateFormat = "EEEE"
let yesterDayOfWeek = styler.stringFromDate(yesterday)
println(yesterDayOfWeek)   // "Sunday"

If you need an Integer 1-7 representing the day weekDay you have to do as follow:

let yesterDayOfWeekInt =  NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitWeekday, fromDate: yesterday).weekday   // 1

if you need some reference to format your dates you can use this one:

Date Formats

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • the code does help me get the day of the week as an Int. I am still curious as to why my original code was not working on iOS 7? was it the case of the method I was trying to use not available for iOS7, like UIApplication.sharedApplication().registerUserNotificationSettings ? – cptdanko Feb 03 '15 at 12:21
  • https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instm/UIApplication/registerUserNotificationSettings: – Leo Dabus Feb 03 '15 at 17:16
  • registerUserNotificationSettings is only available in iOS 8.0 and later – Leo Dabus Feb 03 '15 at 17:17