所以我正在重寫一個開源項目,目前的目標是迅速。我認爲解決這個複雜項目的最好方法是每次重寫和整合每個類。所以我重新編寫了我的第一個類,並且能夠將所有的目標c類指向swift類,並且沒有編譯錯誤。在目標c項目中使用swift類調試問題的技巧?
當我去運行程序,它失敗。我相信這是因爲一個對象沒有被創建。 swiftclass-swift.h文件被創建,但我不得不手動添加一個方法,而不是自動創建。有什麼技術或文章是最好的方法來調試問題可能是什麼?我是否應該在每次新班級召集時創建斷點或者是否有更好的工作流程來識別問題?
我已經創建了類正在初始化的斷點,我沒有看到任何明顯的。另外,我嘗試了針對類創建單元測試,但這些不會運行,因爲xcode在運行測試之前嘗試編譯該程序。另請注意,該課程在操場上運行良好。任何幫助或信息閱讀將不勝感激。
這裏是類:
class DateRange: NSObject, NSCopying {
private var dateFormatter = NSDateFormatter()
var start: NSDate
var end: NSDate
var isEmpty: Bool {
get {return self.start.self.isEqualToDate(self.end)}
}
init(start: NSDate?, end: NSDate?) {
self.start = start!
self.end = end!
super.init()
}
class func dateRange<DR: DateRange>(start: NSDate?, end: NSDate?) -> DR {
let dateR = DateRange(start: start, end: end) as! DR
return dateR
}
func components(unitFlags: NSCalendarUnit, forCalendar calendar: NSCalendar) -> NSDateComponents? {
self.checkIfValid()
return calendar.components(unitFlags, fromDate: self.start, toDate: self.end, options: NSCalendarOptions.WrapComponents)
}
func checkIfValid() {
assert(self.start.compare(self.end) != .OrderedDescending)
}
func containsDate(date: NSDate) -> Bool {
self.checkIfValid()
return date.compare(self.start) != .OrderedDescending || self.end.compare(self.start) != .OrderedDescending
}
func intersectDateRange(range: DateRange?) {
self.checkIfValid()
if (range!.end.compare(self.start) != .OrderedDescending || self.end.compare(range!.start) != .OrderedDescending) {
self.end = self.start
return
}
if self.start.compare(range!.start) == .OrderedAscending {
self.start = range!.start
}
if range!.end.compare(self.end) == .OrderedAscending {
self.end = range!.end
}
}
func intersectsDateRange(range: DateRange?) -> Bool {
if range!.end.compare(self.start) != .OrderedDescending || self.end.compare(range!.start) != .OrderedDescending {
return false
}
return true
}
func includesDateRange(range: DateRange?) -> Bool {
if range!.start.compare(self.start) == .OrderedAscending || self.end.compare(range!.end) == .OrderedAscending { return false
}
return true
}
func unionDateRange(range: DateRange?) {
self.checkIfValid()
range!.checkIfValid()
self.start = self.start.earlierDate((range?.start)!)
self.end = self.end.laterDate((range?.end)!)
}
func enumerateDaysWithCalendar(calendar: NSCalendar?, usingBlock: (day: NSDate?, stop: Bool?) ->()) -> Void {
let comp: NSDateComponents = NSDateComponents()
comp.day = 1
var date: NSDate = self.start
let stop: Bool = false
while !stop && date.compare(self.end) == .OrderedAscending {
usingBlock(day: date, stop: stop)
date = (calendar?.dateByAddingComponents(comp, toDate: self.start, options: NSCalendarOptions.WrapComponents))!
comp.day += 1
}
}
func isEqualToDate(range: DateRange?) -> Bool {
return range == range!.start.isEqualToDate(self.start) && range!.end.isEqualToDate(self.end)
}
// MARK: - NSObject
func copyWithZone(zone: NSZone) -> AnyObject {
return DateRange(start: self.start, end: self.end)
}
override func isEqual(object: AnyObject?) -> Bool {
var isObject = Bool()
if self.isEqual(object) {
isObject = true
}
if ((object?.isKindOfClass(DateRange)) != nil) {
isObject = false
}
return isObject
}
}
你沒有提到它,所以我想我會問,你的橋接頭設置是否正確?當你運行它時說「失敗」,是整個程序失敗還是隻是調用新的類? – fsb
標題已創建,我確實需要添加一個未創建的方法。我更新了這個問題。 – Chris
你能描述失敗嗎? –