Int Variable won't let me add data from UI text field











up vote
-3
down vote

favorite












I'm currently working on making a savings app in Xcode 10. I'm working on a feature that lets users add the amount of money they have saved for something into the app through a UI text field. I can't find a way to return the text from the text field to an Integer and add that to the total sum of money that has been saved. Also whenever I tried to add a test variable I got plenty of errors.



var amountSavedSoFar += amountOfMoneySaved


Both I have set to be integers. I'm trying to set amountOfMoneySaved equal to the numbers in the text field, but it doesn't seem to work.



'+=' is not a prefix unary operator
Consecutive statements on a line must be separated by ';'
Type annotation missing in pattern
Unary operator cannot be separated from its operand










share|improve this question
























  • var amountSavedSoFar is undefined when you try to add amountOfMoneySaved to it - it just doesn't make sense
    – MadProgrammer
    Nov 11 at 21:32










  • Please post code as text, not as a picture.
    – rmaddy
    Nov 11 at 22:06










  • @MadProgrammer I do have it defined. Sorry for not posting it with the code. Its var amountOfMoneySaved = amountSaved.text (Amount saved is my text field)
    – Evan
    Nov 11 at 22:23












  • @Evan Based on the code you've provided, amountSavedSoFar is NOT defined when you attempt to add itself and something to it. You can only "assign" a value when you declare it. This var amountSavedSoFar += amountOfMoneySaved does not make sense
    – MadProgrammer
    Nov 11 at 22:25

















up vote
-3
down vote

favorite












I'm currently working on making a savings app in Xcode 10. I'm working on a feature that lets users add the amount of money they have saved for something into the app through a UI text field. I can't find a way to return the text from the text field to an Integer and add that to the total sum of money that has been saved. Also whenever I tried to add a test variable I got plenty of errors.



var amountSavedSoFar += amountOfMoneySaved


Both I have set to be integers. I'm trying to set amountOfMoneySaved equal to the numbers in the text field, but it doesn't seem to work.



'+=' is not a prefix unary operator
Consecutive statements on a line must be separated by ';'
Type annotation missing in pattern
Unary operator cannot be separated from its operand










share|improve this question
























  • var amountSavedSoFar is undefined when you try to add amountOfMoneySaved to it - it just doesn't make sense
    – MadProgrammer
    Nov 11 at 21:32










  • Please post code as text, not as a picture.
    – rmaddy
    Nov 11 at 22:06










  • @MadProgrammer I do have it defined. Sorry for not posting it with the code. Its var amountOfMoneySaved = amountSaved.text (Amount saved is my text field)
    – Evan
    Nov 11 at 22:23












  • @Evan Based on the code you've provided, amountSavedSoFar is NOT defined when you attempt to add itself and something to it. You can only "assign" a value when you declare it. This var amountSavedSoFar += amountOfMoneySaved does not make sense
    – MadProgrammer
    Nov 11 at 22:25















up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I'm currently working on making a savings app in Xcode 10. I'm working on a feature that lets users add the amount of money they have saved for something into the app through a UI text field. I can't find a way to return the text from the text field to an Integer and add that to the total sum of money that has been saved. Also whenever I tried to add a test variable I got plenty of errors.



var amountSavedSoFar += amountOfMoneySaved


Both I have set to be integers. I'm trying to set amountOfMoneySaved equal to the numbers in the text field, but it doesn't seem to work.



'+=' is not a prefix unary operator
Consecutive statements on a line must be separated by ';'
Type annotation missing in pattern
Unary operator cannot be separated from its operand










share|improve this question















I'm currently working on making a savings app in Xcode 10. I'm working on a feature that lets users add the amount of money they have saved for something into the app through a UI text field. I can't find a way to return the text from the text field to an Integer and add that to the total sum of money that has been saved. Also whenever I tried to add a test variable I got plenty of errors.



var amountSavedSoFar += amountOfMoneySaved


Both I have set to be integers. I'm trying to set amountOfMoneySaved equal to the numbers in the text field, but it doesn't seem to work.



'+=' is not a prefix unary operator
Consecutive statements on a line must be separated by ';'
Type annotation missing in pattern
Unary operator cannot be separated from its operand







ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 22:21

























asked Nov 11 at 21:31









Evan

83




83












  • var amountSavedSoFar is undefined when you try to add amountOfMoneySaved to it - it just doesn't make sense
    – MadProgrammer
    Nov 11 at 21:32










  • Please post code as text, not as a picture.
    – rmaddy
    Nov 11 at 22:06










  • @MadProgrammer I do have it defined. Sorry for not posting it with the code. Its var amountOfMoneySaved = amountSaved.text (Amount saved is my text field)
    – Evan
    Nov 11 at 22:23












  • @Evan Based on the code you've provided, amountSavedSoFar is NOT defined when you attempt to add itself and something to it. You can only "assign" a value when you declare it. This var amountSavedSoFar += amountOfMoneySaved does not make sense
    – MadProgrammer
    Nov 11 at 22:25




















  • var amountSavedSoFar is undefined when you try to add amountOfMoneySaved to it - it just doesn't make sense
    – MadProgrammer
    Nov 11 at 21:32










  • Please post code as text, not as a picture.
    – rmaddy
    Nov 11 at 22:06










  • @MadProgrammer I do have it defined. Sorry for not posting it with the code. Its var amountOfMoneySaved = amountSaved.text (Amount saved is my text field)
    – Evan
    Nov 11 at 22:23












  • @Evan Based on the code you've provided, amountSavedSoFar is NOT defined when you attempt to add itself and something to it. You can only "assign" a value when you declare it. This var amountSavedSoFar += amountOfMoneySaved does not make sense
    – MadProgrammer
    Nov 11 at 22:25


















var amountSavedSoFar is undefined when you try to add amountOfMoneySaved to it - it just doesn't make sense
– MadProgrammer
Nov 11 at 21:32




var amountSavedSoFar is undefined when you try to add amountOfMoneySaved to it - it just doesn't make sense
– MadProgrammer
Nov 11 at 21:32












Please post code as text, not as a picture.
– rmaddy
Nov 11 at 22:06




Please post code as text, not as a picture.
– rmaddy
Nov 11 at 22:06












@MadProgrammer I do have it defined. Sorry for not posting it with the code. Its var amountOfMoneySaved = amountSaved.text (Amount saved is my text field)
– Evan
Nov 11 at 22:23






@MadProgrammer I do have it defined. Sorry for not posting it with the code. Its var amountOfMoneySaved = amountSaved.text (Amount saved is my text field)
– Evan
Nov 11 at 22:23














@Evan Based on the code you've provided, amountSavedSoFar is NOT defined when you attempt to add itself and something to it. You can only "assign" a value when you declare it. This var amountSavedSoFar += amountOfMoneySaved does not make sense
– MadProgrammer
Nov 11 at 22:25






@Evan Based on the code you've provided, amountSavedSoFar is NOT defined when you attempt to add itself and something to it. You can only "assign" a value when you declare it. This var amountSavedSoFar += amountOfMoneySaved does not make sense
– MadProgrammer
Nov 11 at 22:25














1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You've got a few issues as you mentioned:





  1. amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.


  2. amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)


  3. amountSavedSoFar isn't typed or initialized.


Try something like:



var amountSavedSoFar: Int = 0

@IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}





share|improve this answer





















  • Seemed to do trick. Will every time the code run will the amountSavedSofar reset back to 0 though?
    – Evan
    Nov 11 at 22:35










  • No, it will retain its value assuming you're talking about just calling saveAmount.
    – Mark Thormann
    Nov 12 at 15:58













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253432%2fint-variable-wont-let-me-add-data-from-ui-text-field%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You've got a few issues as you mentioned:





  1. amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.


  2. amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)


  3. amountSavedSoFar isn't typed or initialized.


Try something like:



var amountSavedSoFar: Int = 0

@IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}





share|improve this answer





















  • Seemed to do trick. Will every time the code run will the amountSavedSofar reset back to 0 though?
    – Evan
    Nov 11 at 22:35










  • No, it will retain its value assuming you're talking about just calling saveAmount.
    – Mark Thormann
    Nov 12 at 15:58

















up vote
0
down vote



accepted










You've got a few issues as you mentioned:





  1. amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.


  2. amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)


  3. amountSavedSoFar isn't typed or initialized.


Try something like:



var amountSavedSoFar: Int = 0

@IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}





share|improve this answer





















  • Seemed to do trick. Will every time the code run will the amountSavedSofar reset back to 0 though?
    – Evan
    Nov 11 at 22:35










  • No, it will retain its value assuming you're talking about just calling saveAmount.
    – Mark Thormann
    Nov 12 at 15:58















up vote
0
down vote



accepted







up vote
0
down vote



accepted






You've got a few issues as you mentioned:





  1. amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.


  2. amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)


  3. amountSavedSoFar isn't typed or initialized.


Try something like:



var amountSavedSoFar: Int = 0

@IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}





share|improve this answer












You've got a few issues as you mentioned:





  1. amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.


  2. amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)


  3. amountSavedSoFar isn't typed or initialized.


Try something like:



var amountSavedSoFar: Int = 0

@IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 22:22









Mark Thormann

1,036712




1,036712












  • Seemed to do trick. Will every time the code run will the amountSavedSofar reset back to 0 though?
    – Evan
    Nov 11 at 22:35










  • No, it will retain its value assuming you're talking about just calling saveAmount.
    – Mark Thormann
    Nov 12 at 15:58




















  • Seemed to do trick. Will every time the code run will the amountSavedSofar reset back to 0 though?
    – Evan
    Nov 11 at 22:35










  • No, it will retain its value assuming you're talking about just calling saveAmount.
    – Mark Thormann
    Nov 12 at 15:58


















Seemed to do trick. Will every time the code run will the amountSavedSofar reset back to 0 though?
– Evan
Nov 11 at 22:35




Seemed to do trick. Will every time the code run will the amountSavedSofar reset back to 0 though?
– Evan
Nov 11 at 22:35












No, it will retain its value assuming you're talking about just calling saveAmount.
– Mark Thormann
Nov 12 at 15:58






No, it will retain its value assuming you're talking about just calling saveAmount.
– Mark Thormann
Nov 12 at 15:58




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253432%2fint-variable-wont-let-me-add-data-from-ui-text-field%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma