0

I am slowly understanding things in swift, I am coming for a javascript background so it is somewhat familiar.

However variables are urking me.

in JS a variable can be

var varName = 1;     //Number
var varName2 = "petey" //String

var conCat = varname + varName2; //  1petey

however in swift String vars and In var are troubling me. All I want to do is capture decimal number from user input from multiple "textField" sources and store that data to variable (which i already have setup) I need to concatinate a few of them with + then do basic math with the data in the variables with * and /.

How do I make the textFeld only capture?int or how do I convert text field strings to numbers, e.g. int?

Sebastian
  • 8,046
  • 2
  • 34
  • 58
Joe
  • 827
  • 2
  • 9
  • 13

3 Answers3

1

A UITextField contains a String, not an Int, so you have to convert it, e.g.

let number : Int? = textField.text.toInt()

So, there actually is a method to convert from String to Int built-in in Swift. Beware, that it returns an optional, because the conversion may fail. So you have to check for nil before you use the variable.

For your other question, take a look at e.g. UITextField - Allow only numbers and punctuation input/keypad. Basically, you will have to adapt UITextField and filter it, once there are new entries. On iOS it might be easier, because you can show a numbers-only keyboard.

Community
  • 1
  • 1
Sebastian
  • 8,046
  • 2
  • 34
  • 58
  • I'm a little confused where do I put that? as the var declaration/initialization? and im guessing "number" is any var/let name I chose right? – Joe Jan 15 '15 at 15:05
  • You have to put it there where your textfield is instantiated. – Sebastian Jan 15 '15 at 15:12
  • hmmm its not working. the way i was passing data was with prepare For Segue. i send it with this line: >subTotalVC.numOfGuests = numOfGuestsData.text the second view controller grabs it with: >var numOfGuests = "" and then that data is sent again to the third View Controlelr with taxAmountVC.numOfGuests = numOfGuests – Joe Jan 15 '15 at 15:18
  • This is not mentioned in your original question. So maybe you want to adapt it to this. Also, I'm not really able to read the code you provided in your last comment or to re-construct its meaning. – Sebastian Jan 15 '15 at 15:20
  • thanks I am still learning so I may not know how to correctly word it, my apologies. – Joe Jan 15 '15 at 15:22
  • I'd propose, try formulating a question that fits your exact problem and try being as precise as possible. – Sebastian Jan 15 '15 at 15:24
  • Ok well I am tryin gto initilize with that let. it is the first line of code in my first view controller. this is how I wrote it: let number : Int? = numOfGuestsData.text.toInt() but i get an error or numOfGuestsViewController.Type does not havea member named numOfGuestsData. However the textfield is numOfGuestsData.... – Joe Jan 15 '15 at 15:28
  • i will pst a new question with exact flow – Joe Jan 15 '15 at 15:31
  • Ok. Please make sure that you've read and understood [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Sebastian Jan 15 '15 at 15:32
  • Ok again my apologies, didn't realize that. I will do so next time, however I got your example to work. but now all i get is "nil" on the screen. I researched about optionals but no idea what is happening. the link you provided also didn't cover anything about "nil" – Joe Jan 15 '15 at 16:28
  • nil just means that the conversion was not successful. Or your textfield is not properly allocated. – Sebastian Jan 15 '15 at 16:32
  • is there a way to concatenate: string1.toInt() + string2.toInt() + string3.toInt() – Joe Jan 15 '15 at 16:35
  • What do you mean by concatenate? Strings can be concatenated. The operation you showed is adding integers. I strongly recommend to read more about Swift and how it is used with Cocoa. Apple provides this documentation. Please understand, that I'm not able to comment any longer on this, since it is already out of topic and IMO you need to gain more knowledge on Swift basics first. – Sebastian Jan 15 '15 at 16:40
0

The data send from the textField is String. There are multiple ways to convert an Int to a String.

var a:String="\(2+3)" //"5"

And to concatenate a String to Int :

var b:String="Hello "+"\(3*4)" //"Hello 12"

And to Convert a String to an Int from a textField:

var b:Int=textField.text.toInt()
Dhruv Ramani
  • 2,633
  • 2
  • 22
  • 29
0

Use the function in swift inputmethod.intValue(); if any text is entered then it returns with a 0.

Phiter
  • 14,570
  • 14
  • 50
  • 84