简体   繁体   English

如何调用结构内的属性? Swift

[英]How to call a property inside a struct? Swift

How to return choice1Destination as an Int when user press the choice1 button or return choice2Destination when user press the choice2 button?当用户按下choice1按钮时如何将choice1Destination作为Int返回,或者当用户按下choice2按钮时如何返回choice2Destination?

class ViewController: UIViewController {
    
    @IBOutlet weak var storyLabel: UILabel!
    @IBOutlet weak var choice1Button: UIButton!
    @IBOutlet weak var choice2Button: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
    }
    
    let story = [
    Story(title: "Your car has blown a tire on a winding road in the middle of nowhere with no cell phone reception. You decide to hitchhike. A rusty pickup truck rumbles to a stop next to you. A man with a wide brimmed hat with soulless eyes opens the passenger door for you and asks: 'Need a ride, boy?'.",choice1: "I'll hop in. Thanks for the help!",
          choice1Destination: 2,choice2: "Better ask him if he's a murderer first.",
          choice2Destination: 1),
    
    Story(title: "He nods slowly, unfazed by the question.",
          choice1: "At least he's honest. I'll climb in.", choice1Destination: 2,
          choice2: "Wait, I know how to change a tire.", choice2Destination: 3),
    
    Story(title: "As you begin to drive, the stranger starts talking about his relationship with his mother. He gets angrier and angrier by the minute. He asks you to open the glovebox. Inside you find a bloody knife, two severed fingers, and a cassette tape of Elton John. He reaches for the glove box.",choice1: "I love Elton John! Hand him the cassette tape.", choice1Destination: 5,choice2: "It's him or me! You take the knife and stab him.", choice2Destination: 4),
    
    Story(title: "What? Such a cop out! Did you know traffic accidents are the second leading cause of accidental death for most adult age groups?",choice1: "The", choice1Destination: 0,choice2: "End", choice2Destination: 0),
    
    Story(title: "As you smash through the guardrail and careen towards the jagged rocks below you reflect on the dubious wisdom of stabbing someone while they are driving a car you are in.",choice1: "The", choice1Destination: 0,choice2: "End", choice2Destination: 0),
    
    Story(title: "You bond with the murderer while crooning verses of 'Can you feel the love tonight'. He drops you off at the next town. Before you go he asks you if you know any good places to dump bodies. You reply: 'Try the pier.'",choice1: "The", choice1Destination: 0,choice2: "End", choice2Destination: 0)
    ]

    
    @IBAction func pressButton(_ sender: UIButton) {
        let userChoice = sender.currentTitle!
        if userChoice == "choice1Button"{
            return //How to return choice1Destination as an Int when user press the choice1 button or return choice2Destination when user press the choice2 button? //
        }

Don't check the title, instead check the var:不要检查标题,而是检查 var:

if sender == choice1Button {
} 
else { //it's choice2Button
}

Going further:更进一步:

func updateStory(with story: Story) {
    self.currentStory = story 
    storyLabel.text = story.title
    choice1Button.setTitle(story.choice1, for: .normal)
    choice2Button.setTitle(story.choice2, for: .normal)
}

We'd have then, renaming let story = [Story(...), Story(...)] into stories which make more sense in my opinion.然后,我们将let story = [Story(...), Story(...)]重命名为我认为更有意义的stories In viewDidLoad(), you'd call updateStory(stories[0])在 viewDidLoad() 中,你会调用updateStory(stories[0])

if sender == choice1Button {
    let nextStory = stories[currentStory.choice1Destination]
    updateStory(with nextStory)
} 
else { //it's choice2Button
    let nextStory = stories[currentStory.choice2Destination]
    updateStory(with nextStory)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM