Swift 3 - 施放值时始终为零

时间:2016-11-24 12:41:54

标签: 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 )

}

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

有人有建议吗?非常感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

我看到你创建了一个元组,为了从该元组中访问该数据,我建议采用这种方法

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"]