最新消息:20210917 已从crifan.com换到crifan.org

[未解决]swift中出错:Cannot assign value of type ‘Int64′ to type ‘AnyObject?’

Swift crifan 1748浏览 0评论
        var httpParams:[String:AnyObject]? = nil
       
        if self.messageList.count > 0 {
            httpParams = [String:AnyObject]()
            let earliestMsg:Message = self.messageList[0]
           
            let earliestMsgSeq:Int64 = earliestMsg.seq
            print("earliestMsgSeq=\(earliestMsgSeq)")
           
            let lastSeq:Int64 = earliestMsgSeq – 1
            print("lastSeq=\(lastSeq)") //
            httpParams!["last"] = lastSeq
        }else{
            httpParams = nil
        }

结果出错:

 Cannot assign value of type ‘Int64’ to type ‘AnyObject?’

如图:

搜:

swift  Cannot assign value of type ‘Int64’ to type ‘AnyObject?’

Cannot assign a value of type ‘AnyObject?’ to a value of type ‘String!’ – Swift with Parse – Stack Overflow

ios – Cannot assign a value of type ‘int’ to a value of type ‘String?’ – Stack Overflow

casting – Swift – Cast Int64 to AnyObject for NSMutableArray – Stack Overflow

试了试,int是可以的:

        var httpParams:[String:AnyObject]? = nil
       
        if self.messageList.count > 0 {
            httpParams = [String:AnyObject]()
            let earliestMsg:Message = self.messageList[0]
           
//            let earliestMsgSeq:Int64 = earliestMsg.seq
            let earliestMsgSeq:Int = 0
            print("earliestMsgSeq=\(earliestMsgSeq)")
           
//            let lastSeq:Int64 = earliestMsgSeq – 1
            let lastSeq:Int = earliestMsgSeq – 1
            print("lastSeq=\(lastSeq)") //
            httpParams!["last"] = lastSeq
        }else{
            httpParams = nil
        }

结果写成:

        var httpParams:[String:AnyObject]? = nil
       
        if self.messageList.count > 0 {
            httpParams = [String:AnyObject]()
            let earliestMsg:Message = self.messageList[0]
           
            let earliestMsgSeq:Int64 = earliestMsg.seq
            print("earliestMsgSeq=\(earliestMsgSeq)")
           
            let lastSeq:Int64 = earliestMsgSeq – 1
            print("lastSeq=\(lastSeq)") //
            httpParams!["last"] = NSNumber(longLong: lastSeq)
        }else{
            httpParams = nil
        }

就可以了。

转载请注明:在路上 » [未解决]swift中出错:Cannot assign value of type ‘Int64′ to type ‘AnyObject?’

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
95 queries in 0.233 seconds, using 23.35MB memory