1

All, I am really new to iOS development so that means I am new to Swift. I have knowledge of other programming languages like c#, PHP, Node JS, etc. However, I have found myself stuck and can't find any answers.

I am working with STTwitter and I am trying to pull the status of my Twitter. I can pull it fine using the normal method. When I try to add a loop, I get an error: Type 'Any' has no subscript members.

// Verifying Twitter API creds
    twitter?.verifyCredentials(userSuccessBlock: { (username, userId) in

        // Get Twitter Timeline Status
        twitter?.getHomeTimeline(sinceID: nil, count: 10, successBlock: { (statuses) -> Void in

            for status in statuses! {
                print(status["text"])
            }

        }, errorBlock: { (error) in
            print(error)
        })

        print(username, userId)
    }, errorBlock: { (error) in
        print(error)
    })

Here is a screenshot of the actual editor with the error

The error is on the "status" inside the print statement inside the for loop.

Any help would be great!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Optimus24
  • 13
  • 2
  • It seems that this library is written in Objective-C, and `getHomeTimeline` accepts a "block" which is passed an `NSArray *`, which translates in Swift into `[Any]`, so you must translate this `Any`, to the datatype really existing. If you are sure it is a dictionary, do this: `(status as? NSDictionary)?["text"]` – user9335240 Feb 15 '18 at 06:32
  • OMG That worked!!! Thank you so much @user9335240 – Optimus24 Feb 15 '18 at 06:42

1 Answers1

0

getHomeTimeline accepts a "block" (or Swift closure) that is passed an NSArray *, which is translated in Swift to [Any]. So

    // Get Twitter Timeline Status
    twitter?.getHomeTimeline(sinceID: nil, count: 10, successBlock: { (statuses) -> Void in

        for status in statuses! {
            print((status as? NSDictionary)?["text"])
        }

    }
user9335240
  • 1,739
  • 1
  • 7
  • 14