如何在Swift中使用词典?
我创建了字典,如下所示如何在Swift中使用词典?,swift,dictionary,tableview,swift-dictionary,Swift,Dictionary,Tableview,Swift Dictionary,我创建了字典,如下所示 let bookList = [ ["title" : "Harry Potter", "author" : "Joan K. Rowling" "image" : image // UIImage is added. ], ["title" : "Twilight", "author" : " Stephenie Meyer", "image" : image ], ["title"
let bookList = [
["title" : "Harry Potter",
"author" : "Joan K. Rowling"
"image" : image // UIImage is added.
],
["title" : "Twilight",
"author" : " Stephenie Meyer",
"image" : image
],
["title" : "The Lord of the Rings",
"author" : "J. R. R. Tolkien",
"image" : image]
我想用这个书单做一个表视图
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "listCell") as? ListCell else { return UITableViewCell() }
let book = bookList[indexPath.row]
cell.configureCell(title: book.???, author: ???, bookImage: ???)
return cell
}
我应该如何使用字典的值和键来配置单元格?强烈建议使用自定义结构而不是字典
struct Book {
let title : String
let author : String
let image : UIImage
}
var bookList = [Book(title: "Harry Potter", author: "Joan K. Rowling", image: image),
Book(title: "Twilight", author: "Stephenie Meyer", image: image),
Book(title: "The Lord of the Rings", author: "J. R. R. Tolkien", image: image)]
最大的好处是您有不同的非可选类型,而不需要任何类型转换
let book = bookList[indexPath.row]
cell.configureCell(title: book.title, author: book.author, bookImage: book.image)
此外,我要声明
configureCell
func configureCell(book : Book)
并通过
cell.configureCell(book: bookList[indexPath.row])
然后,您可以将结构的成员直接分配给
configureCell
中的标签。强烈建议使用自定义结构而不是字典
struct Book {
let title : String
let author : String
let image : UIImage
}
var bookList = [Book(title: "Harry Potter", author: "Joan K. Rowling", image: image),
Book(title: "Twilight", author: "Stephenie Meyer", image: image),
Book(title: "The Lord of the Rings", author: "J. R. R. Tolkien", image: image)]
最大的好处是您有不同的非可选类型,而不需要任何类型转换
let book = bookList[indexPath.row]
cell.configureCell(title: book.title, author: book.author, bookImage: book.image)
此外,我要声明
configureCell
func configureCell(book : Book)
并通过
cell.configureCell(book: bookList[indexPath.row])
然后,您可以将结构的成员直接分配给
configureCell
中的标签。在这里,字典不是您最好的结构
字典的问题是,您必须处理类型转换(因为您的字典是[String:Any]
),并处理字典查找是可选的,因为可能缺少键
您可以做(不推荐):
看到这有多痛苦了吗
相反,请使用自定义的struct
来表示您的书籍:
struct Book {
var title: String
var author: String
var image: UIImage
}
let bookList = [
Book(
title : "Harry Potter",
author : "Joan K. Rowling",
image : image // UIImage is added.
),
Book(
title : "Twilight",
author : " Stephenie Meyer",
image : image
),
Book(
title : "The Lord of the Rings",
author : "J. R. R. Tolkien",
image : image
)
]
然后,您的配置就变成:
cell.configureCell(title: book.title, author: book.author, bookImage: book.image)
字典不是你最好的结构 字典的问题是,您必须处理类型转换(因为您的字典是
[String:Any]
),并处理字典查找是可选的,因为可能缺少键
您可以做(不推荐):
看到这有多痛苦了吗
相反,请使用自定义的struct
来表示您的书籍:
struct Book {
var title: String
var author: String
var image: UIImage
}
let bookList = [
Book(
title : "Harry Potter",
author : "Joan K. Rowling",
image : image // UIImage is added.
),
Book(
title : "Twilight",
author : " Stephenie Meyer",
image : image
),
Book(
title : "The Lord of the Rings",
author : "J. R. R. Tolkien",
image : image
)
]
然后,您的配置就变成:
cell.configureCell(title: book.title, author: book.author, bookImage: book.image)
的确更令人信服的是,当我们两个人说出来时,这是一个值得考虑的想法。更令人信服的是,当我们两个人说出来时,这是一个值得考虑的想法。