Using two values from a function [duplicate]
This question already has an answer here:
Cannot use instance member within property initializer
1 answer
I've started to make a basic quiz game using Swift. I've written a struct to define what I want relating to each question:
struct Question {
let question: String
let answers: [String]
let correctAnswer: Int
}
The game will be a basic maths quiz, so will show random maths questions. I've written a function to randomise the questions and obtain the answers:
func questionAnswerBuilder() -> (question: String, answer: String) {
let first: Int = randomNumber()
let second: Int = randomNumber()
let arr = [first,second].sorted(by: >)
let firstAsString = String(arr[0])
let secondAsString = String(arr[1])
let questionString = "(firstAsString) + (secondAsString)"
let question = questionString
//Answer:
let answerString = first + second
let answer = String(answerString)
return (question, answer)
}
I have tested this in playgrounds and it works fine. Each time it creates a random number and outputs that along with the correct answer. Now I'd like to use this in each question...
var questionTest = questionAnswerBuilder()
var questions: [Question] = [
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)
]
For info, the randomAnswer() function is not displayed here. It's basically just a random number generator.
When I input all of this into Xcode I get the following message:
Cannot use instance member 'questionTest' within property initializer; property initializers run before 'self' is available.
I currently have the questionAnswerBuilder() function in its own swift file. I have tried moving this to the same file as my questions variable but I still get the same fault. I have also tried making the questionTest variable lazy but this makes no difference.
Please please please could someone point out where I am going wrong!
Thanks in advance!
swift function
marked as duplicate by Fogmeister
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 16:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Cannot use instance member within property initializer
1 answer
I've started to make a basic quiz game using Swift. I've written a struct to define what I want relating to each question:
struct Question {
let question: String
let answers: [String]
let correctAnswer: Int
}
The game will be a basic maths quiz, so will show random maths questions. I've written a function to randomise the questions and obtain the answers:
func questionAnswerBuilder() -> (question: String, answer: String) {
let first: Int = randomNumber()
let second: Int = randomNumber()
let arr = [first,second].sorted(by: >)
let firstAsString = String(arr[0])
let secondAsString = String(arr[1])
let questionString = "(firstAsString) + (secondAsString)"
let question = questionString
//Answer:
let answerString = first + second
let answer = String(answerString)
return (question, answer)
}
I have tested this in playgrounds and it works fine. Each time it creates a random number and outputs that along with the correct answer. Now I'd like to use this in each question...
var questionTest = questionAnswerBuilder()
var questions: [Question] = [
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)
]
For info, the randomAnswer() function is not displayed here. It's basically just a random number generator.
When I input all of this into Xcode I get the following message:
Cannot use instance member 'questionTest' within property initializer; property initializers run before 'self' is available.
I currently have the questionAnswerBuilder() function in its own swift file. I have tried moving this to the same file as my questions variable but I still get the same fault. I have also tried making the questionTest variable lazy but this makes no difference.
Please please please could someone point out where I am going wrong!
Thanks in advance!
swift function
marked as duplicate by Fogmeister
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 16:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Cannot use instance member within property initializer
1 answer
I've started to make a basic quiz game using Swift. I've written a struct to define what I want relating to each question:
struct Question {
let question: String
let answers: [String]
let correctAnswer: Int
}
The game will be a basic maths quiz, so will show random maths questions. I've written a function to randomise the questions and obtain the answers:
func questionAnswerBuilder() -> (question: String, answer: String) {
let first: Int = randomNumber()
let second: Int = randomNumber()
let arr = [first,second].sorted(by: >)
let firstAsString = String(arr[0])
let secondAsString = String(arr[1])
let questionString = "(firstAsString) + (secondAsString)"
let question = questionString
//Answer:
let answerString = first + second
let answer = String(answerString)
return (question, answer)
}
I have tested this in playgrounds and it works fine. Each time it creates a random number and outputs that along with the correct answer. Now I'd like to use this in each question...
var questionTest = questionAnswerBuilder()
var questions: [Question] = [
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)
]
For info, the randomAnswer() function is not displayed here. It's basically just a random number generator.
When I input all of this into Xcode I get the following message:
Cannot use instance member 'questionTest' within property initializer; property initializers run before 'self' is available.
I currently have the questionAnswerBuilder() function in its own swift file. I have tried moving this to the same file as my questions variable but I still get the same fault. I have also tried making the questionTest variable lazy but this makes no difference.
Please please please could someone point out where I am going wrong!
Thanks in advance!
swift function
This question already has an answer here:
Cannot use instance member within property initializer
1 answer
I've started to make a basic quiz game using Swift. I've written a struct to define what I want relating to each question:
struct Question {
let question: String
let answers: [String]
let correctAnswer: Int
}
The game will be a basic maths quiz, so will show random maths questions. I've written a function to randomise the questions and obtain the answers:
func questionAnswerBuilder() -> (question: String, answer: String) {
let first: Int = randomNumber()
let second: Int = randomNumber()
let arr = [first,second].sorted(by: >)
let firstAsString = String(arr[0])
let secondAsString = String(arr[1])
let questionString = "(firstAsString) + (secondAsString)"
let question = questionString
//Answer:
let answerString = first + second
let answer = String(answerString)
return (question, answer)
}
I have tested this in playgrounds and it works fine. Each time it creates a random number and outputs that along with the correct answer. Now I'd like to use this in each question...
var questionTest = questionAnswerBuilder()
var questions: [Question] = [
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)
]
For info, the randomAnswer() function is not displayed here. It's basically just a random number generator.
When I input all of this into Xcode I get the following message:
Cannot use instance member 'questionTest' within property initializer; property initializers run before 'self' is available.
I currently have the questionAnswerBuilder() function in its own swift file. I have tried moving this to the same file as my questions variable but I still get the same fault. I have also tried making the questionTest variable lazy but this makes no difference.
Please please please could someone point out where I am going wrong!
Thanks in advance!
This question already has an answer here:
Cannot use instance member within property initializer
1 answer
swift function
swift function
edited Nov 24 '18 at 18:42
rmaddy
242k27317381
242k27317381
asked Nov 24 '18 at 16:22
evorg88evorg88
918
918
marked as duplicate by Fogmeister
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 16:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Fogmeister
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 16:30
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You cannot initialize properties which depend on each other on the top level of a class.
A solution is to initialize questions lazily
lazy var questions: [Question] = {
return [Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)]
}()
Excellent it worked! Thank you very much for your help!
– evorg88
Nov 24 '18 at 16:46
add a comment |
Oh, you can’t use instance variables inside other instance variables.
You could fix this by changing your questions array to lazy var questions.
This will not create the array at the point of instantiating it. It will only create the array the first time you access it.
Thanks for your answer. I had already seen the post you have marked as a duplicate - it makes more sense now! Thanks
– evorg88
Nov 24 '18 at 16:47
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot initialize properties which depend on each other on the top level of a class.
A solution is to initialize questions lazily
lazy var questions: [Question] = {
return [Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)]
}()
Excellent it worked! Thank you very much for your help!
– evorg88
Nov 24 '18 at 16:46
add a comment |
You cannot initialize properties which depend on each other on the top level of a class.
A solution is to initialize questions lazily
lazy var questions: [Question] = {
return [Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)]
}()
Excellent it worked! Thank you very much for your help!
– evorg88
Nov 24 '18 at 16:46
add a comment |
You cannot initialize properties which depend on each other on the top level of a class.
A solution is to initialize questions lazily
lazy var questions: [Question] = {
return [Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)]
}()
You cannot initialize properties which depend on each other on the top level of a class.
A solution is to initialize questions lazily
lazy var questions: [Question] = {
return [Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2),
Question(question: questionTest.question, answers: [randomAnswer(), randomAnswer(), questionAnswerBuilder().answer, randomAnswer()], correctAnswer: 2)]
}()
answered Nov 24 '18 at 16:29
vadianvadian
149k14160181
149k14160181
Excellent it worked! Thank you very much for your help!
– evorg88
Nov 24 '18 at 16:46
add a comment |
Excellent it worked! Thank you very much for your help!
– evorg88
Nov 24 '18 at 16:46
Excellent it worked! Thank you very much for your help!
– evorg88
Nov 24 '18 at 16:46
Excellent it worked! Thank you very much for your help!
– evorg88
Nov 24 '18 at 16:46
add a comment |
Oh, you can’t use instance variables inside other instance variables.
You could fix this by changing your questions array to lazy var questions.
This will not create the array at the point of instantiating it. It will only create the array the first time you access it.
Thanks for your answer. I had already seen the post you have marked as a duplicate - it makes more sense now! Thanks
– evorg88
Nov 24 '18 at 16:47
add a comment |
Oh, you can’t use instance variables inside other instance variables.
You could fix this by changing your questions array to lazy var questions.
This will not create the array at the point of instantiating it. It will only create the array the first time you access it.
Thanks for your answer. I had already seen the post you have marked as a duplicate - it makes more sense now! Thanks
– evorg88
Nov 24 '18 at 16:47
add a comment |
Oh, you can’t use instance variables inside other instance variables.
You could fix this by changing your questions array to lazy var questions.
This will not create the array at the point of instantiating it. It will only create the array the first time you access it.
Oh, you can’t use instance variables inside other instance variables.
You could fix this by changing your questions array to lazy var questions.
This will not create the array at the point of instantiating it. It will only create the array the first time you access it.
answered Nov 24 '18 at 16:29
FogmeisterFogmeister
56.3k27152242
56.3k27152242
Thanks for your answer. I had already seen the post you have marked as a duplicate - it makes more sense now! Thanks
– evorg88
Nov 24 '18 at 16:47
add a comment |
Thanks for your answer. I had already seen the post you have marked as a duplicate - it makes more sense now! Thanks
– evorg88
Nov 24 '18 at 16:47
Thanks for your answer. I had already seen the post you have marked as a duplicate - it makes more sense now! Thanks
– evorg88
Nov 24 '18 at 16:47
Thanks for your answer. I had already seen the post you have marked as a duplicate - it makes more sense now! Thanks
– evorg88
Nov 24 '18 at 16:47
add a comment |