Jtapplecalendar: Error: Thread 1 EXC_BAD_ACCESS

Created on 7 Jul 2016  路  14Comments  路  Source: patchthecode/JTAppleCalendar

I keep getting an error at runtime in my ViewController class.

screen shot 2016-07-06 at 6 24 21 pm

My code looks like.

import JTAppleCalendar

 class ViewController: UIViewController  {
  @IBOutlet weak var calendarView: JTAppleCalendarView!
  let formatter = NSDateFormatter()
  let testCalendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)

@IBAction func changeToThreeRows(sender: UIButton) {
    let date = formatter.dateFromString("2016 04 11")
    calendarView.changeNumberOfRowsPerMonthTo(3, withFocusDate: date)
}

@IBAction func changeToSixRows(sender: UIButton) {
    let date = formatter.dateFromString("2016 04 11")
    calendarView.changeNumberOfRowsPerMonthTo(6, withFocusDate: date)
}


override func viewDidLoad() {
    super.viewDidLoad()
    formatter.dateFormat = "yyyy MM dd"
    testCalendar.timeZone = NSTimeZone(abbreviation: "GMT")!

    calendarView.dataSource = self
    calendarView.delegate = self

    calendarView.registerCellViewXib(fileName: "CellView") // Registering your cell is manditory

}

@IBAction func select10(sender: AnyObject?) {
    calendarView.allowsMultipleSelection = true
    var dates: [NSDate] = []

    dates.append(formatter.dateFromString("2016 02 03")!)
    dates.append(formatter.dateFromString("2016 02 05")!)
    dates.append(formatter.dateFromString("2016 02 07")!)
    dates.append(formatter.dateFromString("2020 02 16")!) // --> This date will never be selected as it is outsde bounds
    // --> This is what happens when you select an invalid date
    // --> It is simply not selected
    calendarView.selectDates(dates, triggerSelectionDelegate: false)
}

@IBAction func select11(sender: AnyObject?) {
    calendarView.allowsMultipleSelection = false
    let date = formatter.dateFromString("2016 02 11")
    self.calendarView.selectDates([date!], triggerSelectionDelegate: false)
}

@IBAction func scrollToDate(sender: AnyObject?) {
    let date = formatter.dateFromString("2016 03 11")
    calendarView.scrollToDate(date!)
}

@IBAction func printSelectedDates() {
    print("Selected dates --->")
    for date in calendarView.selectedDates {
        print(formatter.stringFromDate(date))
    }
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
}


func delayRunOnMainThread(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    calendarView.frame = calendarView.frame
}

}

extension ViewController: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate {
  func configureCalendar(calendar: JTAppleCalendarView) -> (startDate: NSDate, endDate: NSDate,     calendar: NSCalendar) {

    let firstDate = formatter.dateFromString("2016 01 05")
    let secondDate = NSDate()
    let aCalendar = NSCalendar.currentCalendar() // Properly configure your calendar to your time zone here

    return (startDate: firstDate!, endDate: secondDate, calendar: aCalendar)
  }

func calendar(calendar: JTAppleCalendarView, isAboutToDisplayCell cell: JTAppleDayCellView, date: NSDate, cellState: CellState) {
    (cell as! CellView).setupCellBeforeDisplay(cellState, date: date)
}

}

All 14 comments

taking a look

I just added this calendar today and I am getting the same problem (crashes at calendarView.datasource = self with EXC_BAD_ACCESS).

Guys, you are using a very old version of the calendar :(
Can you please update? the latest version is 4.1.0

I'm not sure about @stkq because I havent seen the code yet.
But @yillivs, please update!

@yillivs can you show me what you have in your pod file?

I tried using pod update various times. But when I do use my pod outdated command I find that indeed I have been using version 3.0.1 of JTAppleCalendar

screen shot 2016-07-07 at 2 10 52 pm

Will I have to change the version in the podfile for the program to compile correctly?

Yes! :)

Change the number from 3.0.1 to 4.1.0
Keep in mind there is an important build change in 4.1.0.

The number of rows per month is now used in the configureCalendar method.
This fixed a lot of the issues you are currently having. So after you update, your build will fail until you fix it.

If you want to know how to setup the calendar correctly, look at the sample application here on github

Do a:
git clone https://github.com/patchthecode/JTAppleCalendar.git

Then head to the Example folder, and open the project. Take a look at how to set it up easily.
If you need more help, tell me here. I check this message system every 30 mins. So expect an answer soon.

Also, keep in mind that version 4.1.1 is coming over the weekend. So look out for that too.

@yillivs btw, if you set a specific version number in your pod file, then you will always be using that version. The Pod assumes that you do not want to update. If you want it to update on a minor version change, then omit the last number. If you want it to be always up to date, then remove all the version numbers all together. Do a google search to get more information on how the pod version system works.

Thanks a bunch, helped me out immensely. Ah that makes sense how hard coding the version would prevent podfile update to work. I'll keep an eye out for 4.1.1 have a good day.

cheers 馃憤

Btw, one other thing i see you using in your code is this:
calendarView.frame = calendarView.frame
please remove this. It is bad for performance.

It was there in the first version. But has since been removed.

Crash:
screen shot 2016-07-08 at 09 43 51

Podfile line:
pod 'JTAppleCalendar', '~> 4.1.0'

configureCalendar function:

func configureCalendar(calendar: JTAppleCalendarView) -> (startDate: NSDate, endDate: NSDate, numberOfRows: Int, calendar: NSCalendar) {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy MM dd"
let firstDate = formatter.dateFromString("2016 01 01")
let secondDate = NSDate()
let numberOfRows = 6
let aCalendar = NSCalendar.currentCalendar()
return (startDate: firstDate!, endDate: secondDate, numberOfRows: numberOfRows, calendar: aCalendar)
}

@stkq is it possible that you can zip your a sample project and send it here?

@patchthecode Actually, the problem was in my storyboard. I hadn't specified the class for the calendar view. The crash is gone now.

@stkq cool. glad it works. cheers

Was this page helpful?
0 / 5 - 0 ratings