Swift - 无法为“NSDictionary”类型调用初始值设定项

时间:2015-09-29 18:29:43

标签: ios swift swift2

自升级到Xcode7后,我收到以下错误:

Cannot invoke initializer for type 'NSDictionary' with an argument list of type '(objects: [AnyObject!], forKeys: [String])'

在这行代码上:

self.sessionBids!.addObject(NSDictionary(objects: [PFUser.currentUser().objectId, PFUser.currentUser().objectForKey("username"), self.bidTextField.text], forKeys: ["user", "name", "bid"]))

有人可以解释原因吗?

编辑:以下是完整的代码块

if(self.bidTextField.text!.rangeOfString("^[0-9]*$", options: .RegularExpressionSearch) != nil) {
                    self.sessionBids = array[0].objectForKey("bids") as? NSMutableArray
                    var lastSessionBid : NSDictionary
                    SVProgressHUD.showProgress(50)
                    var previousHighBid : Int! = 0
                    if(self.sessionBids == nil) {
                        self.sessionBids = NSMutableArray()

                    } else {
                        lastSessionBid = self.sessionBids.objectAtIndex(self.sessionBids.count - 1) as! NSDictionary
                        previousHighBid = Int(lastSessionBid.objectForKey("bid") as! String)
                    }

                    if( previousHighBid >= Int(self.bidTextField.text!)) {
                        print("bid is lower than current bid")
                        SVProgressHUD.showErrorWithStatus("Bid is lower than current bid!")
                        return

                    } else {

                        self.sessionBids!.addObject(NSDictionary(objects: [PFUser.currentUser().objectId, PFUser.currentUser().objectForKey("username"), self.bidTextField.text], forKeys: ["user", "name", "bid"]))
                        SVProgressHUD.showProgress(75)
                        self.session.setObject(self.sessionBids, forKey: "bids")
                        self.session.save()
                        self.keyboardShowing = false
                        self.reloadSessionBids()
                        SVProgressHUD.showProgress(100)
                        SVProgressHUD.showSuccessWithStatus("Successfully Added Bid")
                    }

                } else {
                    SVProgressHUD.showErrorWithStatus("Bid must be a number!")
                }

3 个答案:

答案 0 :(得分:1)

您正在尝试调用NSDictionary的init(objects: [AnyObject], forKeys keys: [NSCopying])初始值设定项。 对象:[AnyObject] 不能包含Optionals(根据其声明),似乎PFUser.currentUser().objectIdPFUser.currentUser().objectForKey("username")self.bidTextField.text都是Optionals,这是为什么你会收到错误。

要解决此问题,建议vadian,您需要打开该数组中的所有选项。

答案 1 :(得分:0)

字典的对象和键都不能是nil 确保所有对象都是非选项。

答案 2 :(得分:0)

尝试调用NSDictionary's public init(objects: UnsafePointer<AnyObject?>, forKeys keys: UnsafePointer<NSCopying?>, count cnt: Int)代替:

self.sessionBids!.addObject(NSDictionary(objects: [PFUser.currentUser().objectId, PFUser.currentUser().objectForKey("username"), self.bidTextField.text], forKeys: ["user", "name", "bid"], count: 3))

或者,尝试使用本机词典而不是NSDictionary。

说:

var bidDict = [String : AnyObject]()
bid["user"] = PFUser.currentUser().objectId ?? "unknown"
bid["name"] = PFUser.currentUser().objectForKey("username") ?? "unknown"
bid["bid"] = self.bidTextField.text ?? "unknown"

然后插入你的数组:

self.sessionBids!.addObject(bid)