由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFTimer copyWithZone:]:发送到实例的无法识别的选择器

时间:2016-07-20 02:05:32

标签: ios swift nsdate nstimer uicollectionviewcell

所以我已经阅读了有关标题中的错误的其他问题,我已经尝试过他们的解决方案,但它们对我不起作用。

viewDidLoad()中,我有两个计时器:

dayCountTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(DateMenuViewController.dayCount(_:)), userInfo: nil, repeats: true)
daySinceTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(DateMenuViewController.daysSinceOrUntil(_:)), userInfo: nil, repeats: true)

他们称这两种方法为:

// For dayCountLabel.text
func dayCount (dayCount: String) -> String{
    let day = dayCount
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
    let date1: NSDate = dateFormatter.dateFromString(day)!
    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day], fromDate: date1, toDate: date2, options: NSCalendarOptions.init(rawValue: 0))
    let difference = String("\(abs(diffDateComponents.day))")
    return difference
}

// For daySinceOrUntilLabel.text
func daysSinceOrUntil (daySince: String) -> String {
    let day = daySince
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMMM yyyy hh:mm a"
    let date1: NSDate = dateFormatter.dateFromString(day)!
    let diffDateComponents = NSCalendar.currentCalendar().components([NSCalendarUnit.Day], fromDate: date1, toDate: date2, options: NSCalendarOptions.init(rawValue: 0))

    if date1.compare(date2) == NSComparisonResult.OrderedDescending  {
        let text: String = "Days Until"
        return text
    } else if date1.compare(date2) == NSComparisonResult.OrderedAscending {
        let text: String = "Days Since"
        return text
    } else {
       return "Days"
    }

}

以下列方式在UICollectionViewCell内使用。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

cell.dayCountLabel.text = dayCount(Globals.datesArray[indexPath.item])
cell.daySinceOrUntilLabel.text = daysSinceOrUntil(Globals.datesArray[indexPath.item])

我在这里做错了什么? 感谢

0 个答案:

没有答案