arrays - Swift 3 - 转换值时总是得到 nil

标签 arrays swift dictionary casting swift3

我目前在获取一些数据时进行类型转换时遇到了麻烦。

这是我在调试控制台中的数据:

(7, ["name": Doe, "id": 2042, "website": http://somedomaine.com/, "logo": http://somedomaine.com/img/, "category_id": 33, "category": Institutional partners, "order": 7])

我确信这确实很明显,但我不知道如何访问 nameid 等数据,这让我头疼。这就是我所做的:

let section = ( index as NSIndexPath ).section + 1 as Int
let row = ( index as NSIndexPath ).row as Int

let sectionContent = posts![ section ] as? Array< Any >

let entry = ( sectionContent?[ row ] as AnyObject )

之后,我能得到的最好结果就是nil!

这里有更多细节:posts 变量在类的顶部启动,如下所示:

internal var posts: [ Int: AnyObject ]?

数据是这样解析的(原始数据来自 json feed,这很好):

func parsePosts( posts: [ Dictionary< String, AnyObject > ] ) -> ( [ Int: AnyObject ], Int ) {

    var theCategories = [ Int : AnyObject ]()
    var theSponsors = [ Int : AnyObject ]()
    var theDict = [ Int : Array< Any > ]()

    for item in posts {

        let itemID = item[ "id" ] as! Int
        let sponsorMeta = item[ "sponsor_meta" ]
        let category = sponsorMeta?[ "category" ] as? String
        let title = item[ "title" ]
        let name = title?[ "rendered" ] as? String

        if !( category ?? "" ).isEmpty {

            let order = Int( ( sponsorMeta?[ "order" ] as? String )! )
            let categoryID = sponsorMeta?[ "category_id" ] as! Int
            let categoryOrder = sponsorMeta?[ "category_order" ] as! Int
            let website = sponsorMeta?[ "url" ] as? String
            let logo = sponsorMeta?[ "logo" ] as? [ String: Any ]
            let logoSizes = logo?[ "sizes" ] as? [ String: Any ]
            let imgUrl = logoSizes?[ "medium_large" ] as? String

            let sponsor = [ "id": itemID, "name": ( name )! as String, "logo": imgUrl! as String, "website": ( website )! as String, "category": category!, "order": order!, "category_id": categoryID ] as [ String : Any ]
            let category = [ "name": category!, "order": categoryOrder ] as [ String : Any ]

            theCategories[ categoryID ] = category as AnyObject
            theSponsors[ itemID ] = sponsor as AnyObject

        }

    }

    for ( k, value ) in theCategories {

        let categoryOrder = value[ "order" ] as! Int
        var catDict = [ Int : [ String: AnyObject ] ]()

        for ( _, element ) in theSponsors {

            let sponsorCategoryID = element[ "category_id" ] as! Int

            if sponsorCategoryID == k {

                let sponsorOrder = element[ "order" ] as! Int
                catDict[ sponsorOrder ] = element as? [ String : AnyObject ]

            }

        }

        let sortedCat = catDict.sorted( by: { $0.0 < $1.0 } )
        theDict[ categoryOrder ] = sortedCat

    }

    let sortedDict = theDict.sorted( by: { $0.0 < $1.0 } )
    var finalDict = [ Int: AnyObject ]()

    for ( t, v ) in sortedDict {
        finalDict[ t ] = v as AnyObject?
    }

    return ( finalDict, theCategories.count )

}

我确信这只是一件小事,但我已经玩了几个小时的类型转换类型,但还没有找到解决方案。

有人有建议吗?我们将不胜感激。谢谢!

最佳答案

我看到您已经创建了一个元组,为了从该元组访问该数据,我建议使用这种方法

let posts = (7, dataDictioanry: ["name": "Doe", "id": 2042, "website": "http://somedomaine.com/", "logo": "http://somedomaine.com/img/", "category_id": 33, "category": "Institutional partners", "order": 7])

posts.dataDictioanry["name"]

关于arrays - Swift 3 - 转换值时总是得到 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40786703/

相关文章:

javascript - 使用字符串中数组的每个元素组成标准化的句子

ios - 如何异步下载图片并保持顺序?

php - 复制带有引用的数组;如何仅获取值

ios - 打开ios拨号面板

python - 通过按 python 中数据帧列的值分组来创建字典

javascript - 对一个数组进行排序并使其他两个数组保持同步

Swift 枚举存储属性

ios - Swift 中从子级到父级的调用函数

swift - 你能强制字典返回非可选或断言吗?

python - 使用字典从数据框中选择多列