JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Error: Thread 1 EXC_BAD_ACCESS

Open yillivs opened this issue 10 years ago • 14 comments

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)
}

}

yillivs avatar Jul 06 '16 22:07 yillivs

taking a look

patchthecode avatar Jul 07 '16 02:07 patchthecode

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

stkq avatar Jul 07 '16 15:07 stkq

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!

patchthecode avatar Jul 07 '16 15:07 patchthecode

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

patchthecode avatar Jul 07 '16 15:07 patchthecode

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?

yillivs avatar Jul 07 '16 18:07 yillivs

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.

patchthecode avatar Jul 07 '16 18:07 patchthecode

@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.

patchthecode avatar Jul 07 '16 18:07 patchthecode

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.

yillivs avatar Jul 07 '16 18:07 yillivs

cheers 👍

patchthecode avatar Jul 07 '16 18:07 patchthecode

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.

patchthecode avatar Jul 07 '16 18:07 patchthecode

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 avatar Jul 08 '16 08:07 stkq

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

patchthecode avatar Jul 08 '16 09:07 patchthecode

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

stkq avatar Jul 08 '16 10:07 stkq

@stkq cool. glad it works. cheers

patchthecode avatar Jul 08 '16 14:07 patchthecode