无法转换类型&#39; Swift.Array <swift.string>&#39; (0x61000028bb50)到&#39; Swift.String&#39; (0x10cbd0ae0)

时间:2017-08-18 18:59:57

标签: ios arrays swift

我有一个程序可以对用户进行初始搜索,然后填充表格。表中有两个按钮用于接受用户请求而另一个按钮用于拒绝,这些按钮调用插入函数,该函数在数据库中输入结果。数组结果被传递给函数,但是我得到了错误&#39; 无法转换类型&#39; Swift.Array&#39; (0x61000028bb50)到&#39; Swift.String&#39; (0x10cbd0ae0)&#39; 在以下代码行中:

    self.insertShot("No", (gname as? String as AnyObject))

gname变量声明如下:
        var gname = String

以下是获取数组数据的代码:

    for i in 0 ..< self.guest.count {

    let ava = self.guest[i]["ava"] as? String

    let gname = self.guest[i]["username"] as? String
    let city = self.guest[i]["city"] as? String
    let state = self.guest[i]["state"] as? String
    let url = NSURL(string: ava!)! 
    self.avas.append(image)
    self.gname.append((gname as AnyObject) as! String)
    self.tableView.reloadData()

InsertShot功能

public func insertShot(_ rating : String,_ gname : AnyObject) {
    // shortcuts to data to be passed to php file

    let reviewer = user?["username"] as! String
    print("print 3", gname)        

    // url path to php file
    let url = URL(string: "http://www.xxxxx.com/xxxxx.php")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    // param to be passed to php file
    let param = [
        "user" : reviewer,
        "revieweduser" : gname,
        "rating" : rating
    ] as [String : Any]

    // body
    let boundary = "Boundary-\(UUID().uuidString)"
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

    // ... body
    request.httpBody = createBodyWithParams(param as? [String : String], boundary: boundary)


    // launch session
    URLSession.shared.dataTask(with: request) { data, response, error in

        // get main queu to communicate back to user
        DispatchQueue.main.async(execute: {


            if error == nil {

                do {

                    // json containes $returnArray from php
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                    // declare new var to store json inf
                    guard let parseJSON = json else {
                        print("Error while parsing")
                        return
                    }

                    // get message from $returnArray["message"]
                    let message = parseJSON["users"]
                    //print(message)

                    // if there is some message - post is made
                    if message != nil {

                        // reset UI
                        // self.msgTxt.text = ""

                        // switch to another scene
                        //self.tabBarController?.selectedIndex = 3
                        _ = self.navigationController?.popViewController(animated: true)


                    }

                } catch {

                    // get main queue to communicate back to user
                    DispatchQueue.main.async(execute: {
                        let message = "\(error)"
                        appDelegate.infoView(message: message, color: colorSmoothRed)
                    })
                    return

                }

            } else {

                // get main queue to communicate back to user
                DispatchQueue.main.async(execute: {
                    let message = error!.localizedDescription
                    appDelegate.infoView(message: message, color: colorSmoothRed)
                })
                return

            }


        })

        }.resume()
    return

}

当选择“是”或“否”按钮并调用InsertShots函数时,将使用以下函数:

@IBAction func noBtn_clicked(_ sender: UIButton) {
    print("print 6", gname)
    self.insertShot("No", (gname as? String as AnyObject))
    //doSearch("")

}
@IBAction func yesBtn_clicked(_ sender: UIButton, users: AnyObject) {

    self.insertShot("Yes", (gname as? String as AnyObject))

}

1 个答案:

答案 0 :(得分:0)

看起来你可能无意中传递了数组self.gname而不是简单的String。从我的上下文,我不知道你是否试图传入数组或字符串。您的代码过多地使用了“无类型”类型,例如AnyAnyObject。您明确禁用了Swift类型系统的安全性和实用性。

我建议您更改功能声明以准确指出您需要的内容。如:

public func insertShot(_ rating: String, _ gname: String)

然后在正文中,您可以将字典声明更改为

let param = [
    "user" : reviewer,
    "revieweduser" : gname,
    "rating" : rating
]

然后放弃演员:

request.httpBody = createBodyWithParams(param, boundary: boundary)

您正在传递self.gname Array String的{​​{1}}。您需要传递String

你可以改变这个:     如果让gnameString = self.gname.first {         self.insertShot(“是”,gnameString)     }

注意:您的代码正在创建一个数组,您可能需要在要传入的数组中找出哪个值。