0

I am new to swift. I want to get options in

fields: [{ config: { options: [{a: "one", b: "two"}] }}]

I have tried the following code but it brings an error "AnyObject? does not have a member named subscript".

my code

limmouyleng
  • 127
  • 1
  • 3
  • 15

3 Answers3

3

AnyObject? is really Optional<AnyObject>, and subscripts are not defined on the Optional type, however, I tried your code with a simulated fields dictionary and I had no issue, so may be your example has context not expressed here? Try making it ... as? AnyObject { rather than ... as AnyObject! { to see if that makes any difference. Also, simply quitting and reopening Xcode has shown to fix many of these types of issues :-)

BJ Miller
  • 1,536
  • 12
  • 16
1

I tried this and it works.

if let config: AnyObject? = fields[i]["config"] {
          if let options: AnyObject? = config?["options"] {
            println("options : \(options)")
          }
        }
limmouyleng
  • 127
  • 1
  • 3
  • 15
1

I solve this problem just with a project - clean .

Asnag
  • 100
  • 1
  • 1
  • 10