gpt4 book ai didi

IOS Swift 从 [String : [AnotherKindOfDictionary] ] ( ) 的字典中读取数据

转载 作者:行者123 更新时间:2023-11-30 12:41:44 24 4
gpt4 key购买 nike

我想从包含图像字典(或任何类型的对象)的字典中读取数据。每个字典都有一个键(字符串)。

为了直观地理解,这就是我想要实现的目标:

userIdOne -> [图像1、图像2、图像3、图像4]

userIdTwo -> [图像1,图像2,图像3]

userIdThree -> [图像1、图像2、图像3、图像4、图像5]

userIdFour -> [图像1,图像2]

注意:尽管这些图像具有相同的“标题”,但它们并不是同一个图像。它们只属于每个单独的用户。 userId 是 [String:... 图像字典是我在这个问题的标题中提到的 [AnotherKindOfDictionary]。我想要每个单元格中的每个用户 ID 及其图像。因此,总共会显示 4 个单元格,但点击时,它们的图像会按顺序显示。

问题是我想将这些数据放入 UITableView 或 UICollectionView 中。我之前都用过,所以无论哪个都有效。类似于 Snapchat 的工作原理。每当点击某个单元格时,就会按顺序显示该用户的图像。

我已经能够将数据加载到字典中,每个 userID 都是关键,但我在使用 collectionView 中的数据时遇到问题(我当前的选择,尽管我可以使用 tableView)

这是我的代码:

var stories = [String : [StoryMedia]]()

// StoryMedia is a struct containing info

struct StoryMedia {
var storyMediaId: String?
var creatorId: String?

var datePosted: String?

var imageUrl: String?

init(storyMediaKey: String, dict: Dictionary<String, AnyObject>) {
storyMediaId = storyMediaKey
creatorId = dict["creatorId"] as? String
datePosted = dict["dateposted"] as? String

imageUrl = dict["imageUrl"] as? String

}
}


... Now in the actual viewController class UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return stories.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let storyCell: StoryCell!

storyCell = collectionView.dequeueReusableCell(withReuseIdentifier: storyReuseIdentifier, for: indexPath) as! StoryCell

// What should I do here?

return storyCell
}

问题在于尝试设置每个单元格。我无法通过键提取每个字典值并将其用于每个单元格。

我尝试过使用:

// Failed attempt 1)
let story = stories[indexPath.row]
// but I get an ambiguous reference to member 'subscript' error


// Failed attempt 2)
for story in stories {
let creatorId = story.key
let sequenceOfStoryItems = story.value


for singleStoryItem in sequenceOfStoryItems {

// do something...

}

}

// but looping through an array for a collection view cell
// does nothing to display the data and if I were to guess,
// would be detrimental to memory if
// I had a lot of "friends" or users in my "timeline"

最佳答案

字典没有排序,因此将其用于 cellForItem 函数很尴尬(以及为什么不能使用该下标)。您也许可以使用字典的值(即忽略键),但这可能是不同的 b/n 运行。我的意思是,您可以在 Stories.values 数组上使用下标(不记得“values”的确切调用,但它很接近...allValues?...不确定,没有办法现在仔细检查)而不是故事字典。

关于IOS Swift 从 [String : [AnotherKindOfDictionary] ] ( ) 的字典中读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165061/

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