0

Problem: I can't assign a string array (typed as 'AnyObject') to an empty array.

Steps:
1. Get a dictionary containing strings & arrays.
2. Grab an array from this dictionary via the key 'photos'.
Note: Xcode's warning suggest that I give the explicit type 'AnyObject'.
3. Create an empty array.
4. Attempt to assign it (failed).

let responseDictionary = responseDict as [String : AnyObject]
let ricPhotos:AnyObject = responseDictionary["photos"]!              
var thePhotos:Array<AnyObject>?              
thePhotos = ricPhotos  <--- fails

Compiler Error:

...'AnyObject' is not convertible to 'Array<AnyObject>'

Question: How do I assign 'ricPhotos' to the empty array 'thePhotos' and preferably cast 'AnyObject' to 'String'?


Revision

let responseDictionary = responseDict as [String : AnyObject]
var anyObject: AnyObject? = responseDictionary["photos"]

Okay, 'anyObject' appears to be a Dictionary, and inside it is 'photo' which is an array; as seen in the data. Here's some of the data(anyObject):

{
    page = 1;
    pages = 1334;
    perpage = 100;
    photo =     (
                {
            farm = 3;
            "height_m" = 336;
            "height_s" = 161;
            "height_sq" = 75;
            "height_t" = 67;
            id = 15166756998;
            isfamily = 0;
            isfriend = 0;
            ispublic = 1;
            owner = "127012961@N08";
            secret = 053032f300;
            server = 2941;
            title = "You @NYPL";
            "url_m" = "https://farm3.staticflickr.com/2941/15166756998_053032f300.jpg";
            "url_s" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_m.jpg";
            "url_sq" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_s.jpg";
            "url_t" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_t.jpg";
            "width_m" = 500;
            "width_s" = 240;
            "width_sq" = 75;
            "width_t" = 100;
        },
        ...etc.

I want to grab the 'photo' array. But I can't downcast 'anyObject' to 'Dictionary' so that I can subscript it. I tried:

var anyObject:Dictionary = responseDictionary["photos"]

But I got:

'(String, AnyObject)' is not convertible to '[String : AnyObject]'

So I'm stuck with:

var anyObject: AnyObject? = responseDictionary["photos"]

So with anyObject, I tried to access 'photos':

let RicPhotos = anyObject["Photo"] as [String : AnyObject]

...I also tried:

let RicPhotos = anyObject["Photo"] as Array<Dictionary<String,String>>

But I got:

'AnyObject?' does not have a member named 'subscript'

I can see the data, but I can't extract into an empty variable.
I attempted to downcast to a specific type (Dictionary) but the compiler refuses.

There must be a strait forward way of getting an embedded array from a dictionary whilst casting to its respective cast (without the 'anyObject').

Any ideas?

1 Answer 1

1

Edited my answer to fit your edit...

let responseDictionary = [:] as [String : AnyObject]
var photosDic: AnyObject? = responseDictionary["photos"]
if let photosDic = photosDic as? Dictionary<String, AnyObject> {
    var photosArray: AnyObject? = photosDic["photo"]
    if let photosArray = photosArray as? Array<Dictionary<String, AnyObject>> {
        //There you go
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

let ricPhotos:Array = responseDic["photos"] <-- I get the compiler error: '(String, AnyObject)' is not convertible to 'Array<T>'
"let photos = anyObject as? Array<String>" <- returns nil. Swift is frustrating.
Does it fix your issue ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.