故事板的约束不允许代码 运行
Constraints on storyboard not allowing code to run
我正在创建一个测验应用程序作为一个学校项目,但是当我创建问题时出现了问题。测验本身有效,但是当我向故事板上的按钮添加约束时,出现了一些问题,我想知道我是否可以获得一些帮助?代码如下,语言为swift:
class MathsViewController: UIViewController {
let questions = ["What is the turning point of the graph, y = x^2 + 8x + 6 ?", "What are the x-intercepts of the graph, y = x^2 + 11x + 18 ?", "What is the triganomic ratio used when relating the opposite and adjacent?", "What would be the final form of the equation, 14/x = sin(20) ?", "Which equation, would have the steepest slope?", "If we added every positive whole number, what would the solution be?", "Which of the following is the fibonacci sequnce?", "What are the first 10 digits of pi?", "What is the probability of getting a 6 in all your rolls, when rolling a dice 3 times?", "If the hypostenuse of a triangle is 26cm, and a side edge is of 10cm long. What is the length of the other side?"]
let answers = [["(-4, -10)", "(0,0)", "(-6, -1)"], ["x = -2 or x = -9", "x = 18 or x = 11", "x = 9 or x = 2"], ["tan", "cos", "sin"], ["x = 14/sin(20)", "x = 14sin^-1/20", "x = sin^-1(20)"], ["y = 5x+2", "y = 1/3x+4", "y = x+16"], ["-1/12", "infinity", "0"], ["1,1,2,3,5,8,13", "1,2,3,4,5,6,7", "1,1,2,2,4,8,32"], ["3.1415926535", "3.141533536", "3.1415934435"], ["1/216", "1/36", "1/6"], ["24cm", "28cm", "26cm"]]
//Variables
var currentQuestion = 0
var rightAnswerPlacement = 0
var points = 0;
//Label
@IBOutlet weak var Label: UILabel!
//Button
@IBAction func action(_ sender: Any)
{
if (sender as AnyObject).tag == Int(rightAnswerPlacement)
{
print("RIGHT!")
points += 1
}
else
{
print("WRONG!!!")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
print(points)
}
override func viewDidLayoutSubviews() {
newQuestion()
}
//Function that displays new question
func newQuestion()
{
Label.text = questions[currentQuestion]
rightAnswerPlacement = Int(arc4random_uniform(3)+1)
//Create Button
var button:UIButton = UIButton()
var x = 1
for i in 1...3
{
//Create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
}
else
{
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
以下是错误消息的一些图片:
The code with the error message
Error Message in the debug area
此外,这里有一些我的故事板图片可以帮助您形象化:
Quiz interface
Final Screen
最后的屏幕是空白的,只有一个标签,这是测验完成后出现的屏幕,我需要对其进行编码,以便分数将替换标签。
感谢任何帮助。
提前致谢!
发生崩溃是因为您试图索引超过 questions
数组的长度,而不是因为违反约束。
索引超出范围意味着您在数组中有 n
个元素,但您试图访问不存在的 n+1
。所以我假设问题出在 currentQuestion
的索引中。
如果您使用数组,您需要记住数组中的索引从 0
开始并在 n-1
结束,因此如果您的 questions
数组中有 3 个元素,您可以像这样访问它们questions[0]
、questions[1]
、questions[2]
。但是,如果您尝试访问 questions[3]
,您将遇到此类错误。
先明白这不是约束错误
这是您的应用在 运行 时间发生的异常(运行 时间错误)。
假设您的“问题”数组中有 10 个问题。您正在检查条件:-
if (currentQuestion != questions.count)
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
此处将对大约 0 到第 10 个位置进行迭代,因为您获得的数组计数将为 11。但在这种情况下,您的“问题”的第 10 个位置将没有元素大批。所以,你必须把这样的条件
if (currentQuestion != (questions.count - 1))
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
这将从 0 迭代到第 9 个位置,这是数组中的第 10 个问题。
请理解@MohyG 试图向您解释的内容,然后继续执行此代码。
我正在创建一个测验应用程序作为一个学校项目,但是当我创建问题时出现了问题。测验本身有效,但是当我向故事板上的按钮添加约束时,出现了一些问题,我想知道我是否可以获得一些帮助?代码如下,语言为swift:
class MathsViewController: UIViewController {
let questions = ["What is the turning point of the graph, y = x^2 + 8x + 6 ?", "What are the x-intercepts of the graph, y = x^2 + 11x + 18 ?", "What is the triganomic ratio used when relating the opposite and adjacent?", "What would be the final form of the equation, 14/x = sin(20) ?", "Which equation, would have the steepest slope?", "If we added every positive whole number, what would the solution be?", "Which of the following is the fibonacci sequnce?", "What are the first 10 digits of pi?", "What is the probability of getting a 6 in all your rolls, when rolling a dice 3 times?", "If the hypostenuse of a triangle is 26cm, and a side edge is of 10cm long. What is the length of the other side?"]
let answers = [["(-4, -10)", "(0,0)", "(-6, -1)"], ["x = -2 or x = -9", "x = 18 or x = 11", "x = 9 or x = 2"], ["tan", "cos", "sin"], ["x = 14/sin(20)", "x = 14sin^-1/20", "x = sin^-1(20)"], ["y = 5x+2", "y = 1/3x+4", "y = x+16"], ["-1/12", "infinity", "0"], ["1,1,2,3,5,8,13", "1,2,3,4,5,6,7", "1,1,2,2,4,8,32"], ["3.1415926535", "3.141533536", "3.1415934435"], ["1/216", "1/36", "1/6"], ["24cm", "28cm", "26cm"]]
//Variables
var currentQuestion = 0
var rightAnswerPlacement = 0
var points = 0;
//Label
@IBOutlet weak var Label: UILabel!
//Button
@IBAction func action(_ sender: Any)
{
if (sender as AnyObject).tag == Int(rightAnswerPlacement)
{
print("RIGHT!")
points += 1
}
else
{
print("WRONG!!!")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
print(points)
}
override func viewDidLayoutSubviews() {
newQuestion()
}
//Function that displays new question
func newQuestion()
{
Label.text = questions[currentQuestion]
rightAnswerPlacement = Int(arc4random_uniform(3)+1)
//Create Button
var button:UIButton = UIButton()
var x = 1
for i in 1...3
{
//Create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
}
else
{
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
以下是错误消息的一些图片:
The code with the error message
Error Message in the debug area
此外,这里有一些我的故事板图片可以帮助您形象化:
Quiz interface
Final Screen
最后的屏幕是空白的,只有一个标签,这是测验完成后出现的屏幕,我需要对其进行编码,以便分数将替换标签。
感谢任何帮助。
提前致谢!
发生崩溃是因为您试图索引超过 questions
数组的长度,而不是因为违反约束。
索引超出范围意味着您在数组中有 n
个元素,但您试图访问不存在的 n+1
。所以我假设问题出在 currentQuestion
的索引中。
如果您使用数组,您需要记住数组中的索引从 0
开始并在 n-1
结束,因此如果您的 questions
数组中有 3 个元素,您可以像这样访问它们questions[0]
、questions[1]
、questions[2]
。但是,如果您尝试访问 questions[3]
,您将遇到此类错误。
先明白这不是约束错误
这是您的应用在 运行 时间发生的异常(运行 时间错误)。
假设您的“问题”数组中有 10 个问题。您正在检查条件:-
if (currentQuestion != questions.count)
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
此处将对大约 0 到第 10 个位置进行迭代,因为您获得的数组计数将为 11。但在这种情况下,您的“问题”的第 10 个位置将没有元素大批。所以,你必须把这样的条件
if (currentQuestion != (questions.count - 1))
{
newQuestion()
}
else
{
performSegue(withIdentifier: "showScore", sender: self)
}
这将从 0 迭代到第 9 个位置,这是数组中的第 10 个问题。 请理解@MohyG 试图向您解释的内容,然后继续执行此代码。