gpt4 book ai didi

ios - 从另一个文件快速访问变量

转载 作者:搜寻专家 更新时间:2023-10-31 22:00:42 26 4
gpt4 key购买 nike

我是一个新手 swift 程序员,我被要求编写一个应用程序,允许你输入一个词,然后生成一个随机的俳句这是一个带有两个 ViewController 的选项卡式应用程序。 (诗)基于那个词。所以在 FirstViewController 中我有数据,我想在 SecondViewController 中以一种很好的方式显示该数据。我有所有的诗行,都在 FirstViewController 中,但我想在 SecondViewController 中访问这些变量。我已经尝试创建一个函数,它只返回它们,然后在 SecondViewController 中调用该函数,但没有任何结果,因为该函数只是返回 nil。如果你们中的任何人能提供帮助,我们将很高兴谢谢!

这是 FirstViewController:

import UIKit
import Foundation


class FirstViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var keyWordTextField: UITextField!

@IBOutlet weak var syllableSlider: UISlider!
@IBOutlet weak var syllableSliderLabel: UILabel!
var syllableSliderValue = 1


@IBOutlet weak var lineOneTextField: UITextField!

@IBOutlet weak var lineTwoTextField: UITextField!
@IBOutlet weak var lineThreeTextField: UITextField!


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lineOneTextField.text = "Rad 1"
lineTwoTextField.text = "Rad 2"
lineThreeTextField.text = "Rad 3"


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func syllableValueChanged(sender: UISlider) {
syllableSliderValue = Int((sender.value))
syllableSliderLabel.text = "Ordet har: \(syllableSliderValue) stavelser"
}


@IBAction func getNewHaiku() {
if keyWordTextField.text != "" {
let keyWord = keyWordTextField.text
let lineOne = generateLine(keyWord: keyWord, syllables: syllableSliderValue, lineSyllableLenght: 5)
let lineTwo = generateLine(keyWord: keyWord, syllables: syllableSliderValue, lineSyllableLenght: 7)
let lineThree = generateLine(keyWord: keyWord, syllables: syllableSliderValue, lineSyllableLenght: 5)
lineOneTextField.text! = lineOne
lineTwoTextField.text! = lineTwo
lineThreeTextField.text! = lineThree
}

}
func generateLine(#keyWord: String, syllables : Int, lineSyllableLenght : Int) -> String {
let oneSyllables = Dict().oneSyllables
let twoSyllables = Dict().twoSyllables
let threeSyllables = Dict().threeSyllables
let fourSyllables = Dict().fourSyllables

let randomOneSyllableWordNumber = Int(arc4random_uniform(UInt32(oneSyllables.count)))
let randomTwoSyllableWordNumber = Int(arc4random_uniform(UInt32(twoSyllables.count)))
let randomThreeSyllableWordNumber = Int(arc4random_uniform(UInt32(threeSyllables.count)))
let randomFourSyllableWordNumber = Int(arc4random_uniform(UInt32(fourSyllables.count)))

var lineArray : [String] = []
var line = ""
lineArray.append(keyWord)

if syllables == 1 {
let randomWordMethod = Int(arc4random_uniform(2))

if randomWordMethod == 0 {

lineArray.append(fourSyllables[randomFourSyllableWordNumber])

} else if randomWordMethod == 1 {

lineArray.append(threeSyllables[randomThreeSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
} else if randomWordMethod == 2 {

lineArray.append(oneSyllables[randomOneSyllableWordNumber])
lineArray.append(twoSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}

} else if syllables == 2 {
let randomWordMethod = Int(arc4random_uniform(2))

if randomWordMethod == 0 {

lineArray.append(twoSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomTwoSyllableWordNumber])
} else if randomWordMethod == 1 {

lineArray.append(threeSyllables[randomThreeSyllableWordNumber])
} else if randomWordMethod == 2 {
lineArray.append(twoSyllables[randomTwoSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}
} else if syllables == 3 {
let randomWordMethod = Int(arc4random_uniform(1))

if randomWordMethod == 0 {
lineArray.append(twoSyllables[randomTwoSyllableWordNumber])
} else if randomWordMethod == 1 {
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}

} else if syllables == 4 {
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}

if lineSyllableLenght == 7 {
let randomWordMethod = Int(arc4random_uniform(1))

if randomWordMethod == 0 {
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
} else if randomWordMethod == 1 {
lineArray.append(twoSyllables[randomTwoSyllableWordNumber])
}
}
for word in lineArray {
line += " \(word)"
}
line += ","

return line
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func getData() -> (line2: String, line3: String) {

return (lineTwoTextField.text, lineThreeTextField.text)

}


}

Ps,“Dict”是另一个文件,但只包含单词。

第二个 View Controller 是空白的。

最佳答案

或者您可以将其设为全局变量,这样任何文件都可以访问它。

struct structname {
static var yourvariable = value
}

调用时输入

filename.structname.yourvariable

关于ios - 从另一个文件快速访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28002847/

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