最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift出错:NSForwarding: warning of class does not implement methodSignatureForSelector trouble ahead

Swift crifan 4413浏览 0评论

代码:

class CalculateElapsedTime {
    let uniqueId:String

    var startTime:NSDate
    var endTime:NSDate

    var elapsedTimeInSec:Double {
        let diffTimeInSec:Double = self.endTime.timeIntervalSinceDate(self.startTime)
        return diffTimeInSec
    }
   
    init(uniqueId:String = ""){
        self.uniqueId = uniqueId

        self.startTime = NSDate()
        self.endTime = self.startTime
       
//        start()
    }

    func start() -> NSDate {
        self.startTime = NSDate()
        self.endTime = self.startTime
       
        return self.startTime
    }
   
    func stop() -> Double {
        self.endTime = NSDate()
       
        return elapsedTimeInSec
    }

}
                let calcTime = CalculateElapsedTime()
                let extraParas:Dictionary<String, AnyObject> = [
                    "calcTime" : calcTime,
                ]
                getUrlRespJsonDict_async(getUserIdUrl, extraParas: extraParas, respJsonDictHandler: getUserIdJsonDictHandler)
    func getUserIdJsonDictHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON) {
        /*
        decodedJsonDict=user-7aa7e3c3-bf8f-46e1-8cbe-3a5aad6e9dfb
        */
       
        if extraParas != nil {
            if var calcTime = extraParas!["calcTime"] {
                calcTime = calcTime as! CalculateElapsedTime
                let elapsedTime = calcTime!.stop()
                gLog.debug("elapsedTime=\(elapsedTime)")
            }
        }

结果出错,挂了:

2016-03-30 19:40:08.276 简道[475:53604] *** NSForwarding: warning: object 0x13671e610 of class ‘ÁÆÄÈÅì.CalculateElapsedTime’ does not implement methodSignatureForSelector: — trouble ahead
Unrecognized selector -[简道.CalculateElapsedTime stop]

搜:

NSForwarding warning of class does not implement methodSignatureForSelector trouble ahead

Does not implement methodSignatureForSelector: – trouble ahead – | Touch Code Magazine

Object X of class Y does not implement methodSignatureForSelector in Swift – Stack Overflow

去加上继承NSObject

然后就可以了:

//class CalculateElapsedTime {
class CalculateElapsedTime: NSObject {
    let uniqueId:String

    var startTime:NSDate
    var endTime:NSDate

    var elapsedTimeInSec:Double {
        let diffTimeInSec:Double = self.endTime.timeIntervalSinceDate(self.startTime)
        return diffTimeInSec
    }
   
    init(uniqueId:String = ""){
        self.uniqueId = uniqueId

        self.startTime = NSDate()
        self.endTime = self.startTime
       
//        start()
    }

    func start() -> NSDate {
        self.reset()
       
        return self.startTime
    }
   
    func stop() -> Double {
        self.endTime = NSDate()
       
        return elapsedTimeInSec
    }

    func reset(){
        self.startTime = NSDate()
        self.endTime = self.startTime
    }

}
func calcTimeStart(uniqueId:String){
    gLog.debug("\(uniqueId)")

    let calcTime = CalculateElapsedTime()
    gCalcTime[uniqueId] = calcTime
}

func calcTimeEnd(uniqueId:String){
    if gCalcTime.keys.contains(uniqueId) {
        let calcTime = gCalcTime[uniqueId]!
        let elapsedTime = calcTime.stop()
        gLog.debug("\(uniqueId) elapsedTime=\(String(format: "%.4f", elapsedTime))")
        calcTime.reset()
    }

}
        calcTimeStart("loginWithUserid")
        getUrlRespJsonDict_async(genAuthUrl, extraParas: extraParas, respJsonDictHandler: getAcessTokenHandler)
    }
   
    func getAcessTokenHandler(getJsonOk:Bool, extraParas:AnyObject?, respJsonDict:JSON) {
        /*
        {
          "data" : "2bh3ertj1a7a7jfj8hut0t3k0k",
          "message" : "ok",
          "code" : 200
        }
        */
        calcTimeEnd("loginWithUserid")

转载请注明:在路上 » [已解决]swift出错:NSForwarding: warning of class does not implement methodSignatureForSelector trouble ahead

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.174 seconds, using 22.16MB memory