【问题标题】:Extra argument Swift? [duplicate]额外的论点斯威夫特? [复制]
【发布时间】:2016-05-28 14:00:16
【问题描述】:

调用中的额外参数“错误”

func handleReceivedDataWithNotification(notification:NSNotification){
        let userInfo = notification.userInfo! as Dictionary
        let receivedData:NSData = userInfo["data"] as! NSData

        let message = NSJSONSerialization.JSONObjectWithData(receivedData, options: NSJSONReadingOptions.AllowFragments, error: nil) // This the error line
        let senderPeerId:MCPeerID = userInfo["peerID"] as! MCPeerID
        let senderDisplayName = senderPeerId.displayName

        if message.objectForKey("string")?.isEqualToString("New Game") == true{
            let alert = UIAlertController(title: "TicTacToe", message: "\(senderDisplayName) has started a new Game", preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)

            resetField()
        }else{
            var field:Int? = message.objectForKey("field")?.integerValue
            var player:String? = message.objectForKey("player") as? String

            if field != nil && player != nil{
                fields[field!].player = player
                fields[field!].Player(player!)

                if player == "x"{
                    currentPlayer = "o"
                }else{
                    currentPlayer = "x"
                }

                checkResults()

            }

        }


    }

【问题讨论】:

  • 对于投票结束问题的人来说,不,很清楚要问什么。
  • Swift 改为使用throws,而不是输入输出错误。见developer.apple.com/library/ios/documentation/Swift/Conceptual/…
  • 有很多关于“调用中的额外参数'错误'”的问题(例如在相关列表中)。搜索“JSONObjectWithData Extra argument 'error' in call”会立即显示解决方案。

标签: ios xcode swift swift2.2


【解决方案1】:

您可能正在尝试将 Swift 1.x 调用与 Swift 2.x 编译器一起使用。

Swift 在 1 和 2 之间经历了很大的变化,现在该方法(以及大多数其他报告错误的方法)抛出异常而不是传入错误参数。

current documentation 显示签名为:

 class func JSONObjectWithData(_ data: NSData,
                  options opt: NSJSONReadingOptions) throws -> AnyObject 

要了解如何处理异常,请查看documentation

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2017-03-24
    • 1970-01-01
    • 2016-02-08
    • 2017-11-13
    • 2015-08-19
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多