0

for example here is my code in swift and then the equivalent in objective C. I understand the safety implications, but I'm wondering if anyone has created an extension that can handle this?

Swift

if tweetDict.value(forKeyPath: "extended_entities.media") != nil {
    let mediaDict = tweetDict.value(forKeyPath: "extended_entities.media") as! NSArray
    let firstObject = mediaDict.firstObject as! NSDictionary
    if firstObject.value(forKeyPath: "type") as! String == "video" {
        if let videoInfo = firstObject.value(forKeyPath: "video_info") as? NSDictionary {
            if let variants = videoInfo.value(forKeyPath: "variants") as? NSArray {
                if let firstVariant = variants[0] as? NSDictionary {
                    if let videoURL = firstVariant.value(forKeyPath: "url") as? String {


                    }
                }
            }
        }
    }
}

Obj-C

NSString *videoURL = tweetDict[@"extended_entities"][@"media"][0]["video_info"]["variants"][0]["url"]
Halpo
  • 2,982
  • 3
  • 25
  • 54
  • Before in Swift it was possible. Now it isn't natively. Question: Why do you have that much Dictionary, an not using Custom Struct/Model? – Larme Jun 19 '20 at 15:57
  • 4
    As Larme suggested parse the data into custom structs. Then you can use native Swift Keypath or simple dot notation. And don't use `NS...` collection types in Swift at all – vadian Jun 19 '20 at 16:04
  • You can look at SwiftyJSON – Cy-4AH Jun 19 '20 at 16:40
  • 1
    That library hasn't been updated for over a year. Better to understand how to implement `Codable`. – koen Jun 19 '20 at 17:18
  • This isn't really feasible in Swift without casting, check https://stackoverflow.com/a/31950592/5329717 – Kamil.S Jun 20 '20 at 07:32

0 Answers0