-2

Value of optional type '[AnyObject]?' must be unwrapped to a value of type '[AnyObject]'

I am passing value of one array to another array

first array declared

var arrayShow = [AnyObject]()

second array declared

var arrayData = [AnyObject]()

i m getting crash here : i have to make below line as options and remove ! @vadian vc.arrayData = (self.arrayShow[tag] as? [AnyObject])!

My Code :

let status = self.nullToNil(value: result["status"]) as? String ?? ""
                let doc_url = self.nullToNil(value: result["doc_url"]) as? String ?? ""
                self.arrayShow.removeAll()

                if(status == "200"){
                    let data = self.nullToNil(value: result["data"]) as? [[String:AnyObject]]
                    self.arrayShow.append(data as AnyObject)
                    print("Array : \(self.arrayShow)")

                    DispatchQueue.main.async {
                        if(data?.count == 0){
                            self.alert(message: "No Data Found.")
                        }else{
                            if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewPrescriptionReport") as? ViewPrescriptionReport {
                                vc.modalPresentationStyle = .overCurrentContext
                                vc.strTitle = "View Prescription"
                                vc.strURL = doc_url
                                vc.arrayData = (self.arrayShow[tag] as? [AnyObject])!
                                self.present(vc, animated:true, completion: nil)
                            }
                        }
                    }
                }
                else{
                    print("no data found")
                }
Dilip Tiwari
  • 1,441
  • 18
  • 31
  • Actually the error means *Make Unwrapped and Remove Optional in Swift* – vadian Jan 31 '20 at 05:53
  • Can you show the code that actually produces the error? Neither of these arrays are optional. And why are you using arrays of `AnyObject`? Don't you know the actual type of the elements? – Paulw11 Jan 31 '20 at 05:54
  • The only way you can eliminate the ! Is to use a conditional `if let element = arrayShow[tag] as? [AnyObject] { arrayData = element }` but I suspect that this will not enter the block because it seems that the element in `arrayShow[tag]` is not an array of AnyObject I.e. you are saying that arrayShow is an array of arrays – Paulw11 Jan 31 '20 at 05:59
  • Are you retrieving JSON? There are much better ways of handling that – Paulw11 Jan 31 '20 at 06:00
  • Can you show your `JSON`? – Rob Jan 31 '20 at 06:02
  • So use a Codable struct and JSONDecoder. Don't use AnyObject when you know the types that are expected (e.g. String) – Paulw11 Jan 31 '20 at 06:02
  • i m not using Codable i need to do same as it was working properly – Dilip Tiwari Jan 31 '20 at 06:04
  • @DilipTiwari It is better to use `Codable`, it will keep you away from this kind of `AnyObject` things. It's a better practice. – Rob Jan 31 '20 at 06:10

1 Answers1

1

As arrayShow is not [[]] i.e Array of Arrays so the only way to remove ! is to get element at [tag] from arrayShow and append to a newly created array. Like this.

...
 let element = self.arrayShow[tag]
  vc.arrayData = [AnyObject]()
  vc.arrayData.append(element)
...

Afsar edrisy
  • 1,985
  • 12
  • 28