gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:52:34 25 4
gpt4 key购买 nike

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

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

(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com