ios - Could not cast value of type 'Swift.Array<Swift.String>' (0x61000028bb50) to 'Swift.String' (0x10cbd0ae0) -


i have program initial search of users populates table. there 2 buttons within table accept users request , deny, these buttons call insert function inputs results in database. array results being passed function i'm getting error of 'could not cast value of type 'swift.array' (0x61000028bb50) 'swift.string' (0x10cbd0ae0)' on following line of code:

    self.insertshot("no", (gname as? string anyobject)) 

the gname variable declared follows:
var gname = string

here code getting array data:

    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 anyobject) as! string)     self.tableview.reloaddata() 

insertshot function

public func insertshot(_ rating : string,_ gname : anyobject) {     // shortcuts data passed php file      let reviewer = user?["username"] as! string     print("print 3", gname)              // url path php file     let url = url(string: "http://www.xxxxx.com/xxxxx.php")!     var request = urlrequest(url: url)     request.httpmethod = "post"      // param passed php file     let param = [         "user" : reviewer,         "revieweduser" : gname,         "rating" : rating     ] [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          // main queu communicate user         dispatchqueue.main.async(execute: {               if error == nil {                  {                      // json containes $returnarray php                     let json = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? nsdictionary                      // declare new var store json inf                     guard let parsejson = json else {                         print("error while parsing")                         return                     }                      // message $returnarray["message"]                     let message = parsejson["users"]                     //print(message)                      // if there message - post made                     if message != nil {                          // reset ui                         // self.msgtxt.text = ""                          // switch scene                         //self.tabbarcontroller?.selectedindex = 3                         _ = self.navigationcontroller?.popviewcontroller(animated: true)                       }                  } catch {                      // main queue communicate user                     dispatchqueue.main.async(execute: {                         let message = "\(error)"                         appdelegate.infoview(message: message, color: colorsmoothred)                     })                     return                  }              } else {                  // main queue communicate user                 dispatchqueue.main.async(execute: {                     let message = error!.localizeddescription                     appdelegate.infoview(message: message, color: colorsmoothred)                 })                 return              }           })          }.resume()     return  } 

the following functions used when yes or no buttons selected , call insertshots function:

@ibaction func nobtn_clicked(_ sender: uibutton) {     print("print 6", gname)     self.insertshot("no", (gname as? string anyobject))     //dosearch("")  } @ibaction func yesbtn_clicked(_ sender: uibutton, users: anyobject) {      self.insertshot("yes", (gname as? string anyobject))  } 

it seems may inadvertently passing array self.gname instead of simple string. context have, can't tell if you're trying pass in array or string. code making use of "no type" types, such any , anyobject. explicitly disabling safety , helpfulness of swift type system.

i'd recommend changing function declaration indicate need. such as:

public func insertshot(_ rating: string, _ gname: string) 

then within body, can change dictionary declaration to

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

then drop cast:

request.httpbody = createbodywithparams(param, boundary: boundary) 

you passing in self.gname array of strings. need pass in string.

change this: if let gnamestring = self.gname.first { self.insertshot("yes", gnamestring) }

note: code creating array, presumably need figure out which value in array want pass in.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -