我正在编写应用程序以使用Swift显示和浏览一系列问题和答案。我目前有一个问题和相关答案的字典,然后我使用.map来创建一个'flashcard'对象数组(每个对象都有一个问题及其相关的答案)。
当我然后从数组中访问这些对象时,由于某种原因,返回的第0个项目似乎是我的字典中的第二个项目而不是第一个项目(例如“问题2”)。有人可以解释为什么会这样吗?这是创建数据数组的类,并提供将项返回到相关视图控制器的方法:
import Foundation
class Deck {
// Create constant to hold the deck of cards
private var cards: [Flashcard]
var bookmark: Int // Remembers which card we are on
init() {
let cardData = ["Question 1" : "Answer 1",
"Question 2" : "Answer 2",
"Question 3" : "Answer 3"]
cards = cardData.map { Flashcard(question: $0, answer: $1) }
bookmark = 0 // Start with bookmark on first item in deck
}
// Getter method to return current card
public func getCurrentCard() -> Flashcard {
print("Bookmark set to \(bookmark)")
return cards[bookmark]
}
// Getter method to return the next card in the deck
public func getNextCard() -> Flashcard {
bookmark += 1
print("Bookmark set to \(bookmark)")
return cards[bookmark]
}
// Getter method to return previous card in deck
public func getLastCard() -> Flashcard {
bookmark -= 1
print("Bookmark set to \(bookmark)")
return cards[bookmark]
}
}
然后这是我的视图控制器,我请求显示对象:
import UIKit
class QuestionController: UIViewController {
// Outlet for the 'question' label
@IBOutlet weak var questionTextView: UITextView!
// Outlet for the next button
@IBAction func nextQuestion(_ sender: UIButton) {
flashcard = deck.getNextCard()
questionTextView.text = flashcard?.question
}
// Outlet for last button
@IBAction func lastQuestion(_ sender: UIButton) {
flashcard = deck.getLastCard()
questionTextView.text = flashcard?.question
}
// variable to hold current flashcard
var flashcard: Flashcard?
let deck = Deck() // Holds deck of data
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
flashcard = deck.getCurrentCard()
questionTextView.text = flashcard?.question
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Prepare flashcard item to be passed to the answer controller before segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let answerController = segue.destination as? AnswerController {
answerController.flashcard = flashcard
}
}
}
当我运行应用程序时,显示的第一个问题是“问题2”,当我期待它是“问题1”时......?
答案 0 :(得分:3)
问题是Dictionary
中键值对的顺序是未指定 - 它完全依赖于实现,并且不保证与订单相同其中键值对出现在用于创建字典的字典文字中。
在您的情况下,一个简单的解决方案是使用而不是字典,而是DictionaryLiteral
- 它维护键值对的顺序:
init() {
let cardData: DictionaryLiteral = ["Question 1" : "Answer 1",
"Question 2" : "Answer 2",
"Question 3" : "Answer 3"]
cards = cardData.map { Flashcard(question: $0, answer: $1) }
bookmark = 0 // Start with bookmark on first item in deck
}
这是因为under the hood,DictionaryLiteral
只不过是一组键值元组。因此,您会注意到它与Dictionary
的行为完全不同,并且维护键值对的顺序:
Int
索引,您可以使用Hashable
因此,在将键值对打包到实际数据结构中之前,它的主要用途实际上只是一些瞬态语法糖(允许使用字典文字) - 这就是我们在这里使用它的方式