0

I have several UITextFields and the principle of my app is to add them together. The user can type either numbers or words into the UITextField. I haven't got problems with the number calculations, it's calculating with the words.

My question is: if let's say the users types "mvg", and I want that word to have the value 20, how can I make Xcode register the value and calculate with that?

WendiKidd
  • 4,333
  • 4
  • 33
  • 50
Pasod
  • 13
  • 2
  • Sorry but I dont realy get your question. You said you calculate by words, but then you have to do a conversion from "real Language" to a normal Number and (if I correctly understood your question) you ask how to asociate "mvg" with the number 20, but if I am right than you have already done it ;) – Dennis Stritzke Jan 03 '12 at 20:45
  • Not an answer to your question but for information on determining whether or not your `UITextField`'s value is a number or not see this [question](http://stackoverflow.com/questions/7349316/determine-if-a-string-is-a-number-with-nsscanner) – Thomas Nadin Jan 03 '12 at 20:46
  • @DennisS. I know the question is a bit fuzzy, but you understood it properly. The calculations is correct as long as you put in numbers instead of words. But i desire that when the user type in "mvg", the app will translate the word in to the number 20 and that the calculations still come out correct. – Pasod Jan 03 '12 at 21:14

2 Answers2

1

Short answer: Xcode won't. Your program must access the textfield, and it is responsible for looking up any word in there to determine what it means (which could be as simple as using a dictionary).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Ok, got it, i will try to make a dictionary that translates my words into numbers. Appreciate your answer! :) – Pasod Jan 03 '12 at 21:15
0

I wish you would have said something like "the user types 20 into the text field and I want the value to be 20".

But what you're describing sounds like something very application specific.

What I'd recommend doing is building a dictionary of words (as keys) and NSNumbers (objects) for their equivalents. If you're doing more than just numbers, you might want to store custom Objective C objects for key words.

Something like:

NSDictionary * myPhraseToNumberDictionary = 
    [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt: 20], @"mvg", [NSNumber numberWithInt: 40], @"dmvg", [NSNumber numberWithInt: 40], @"tmvg", nil]

So then when the user types in mvg, parse through the text field and for each keyword hit in your dictionary, you'll get a NSNumber object containing the expected number back at you, which you can then use for calculating.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • That´s exactly what I'm looking for! I'm very pleased with your answer and I hate to bother again by asking you where I should put that (a). I'm very new to this and I'm reading a couple of objective c books. And if I want to continue the dictionary should I just make the code longer like this; [NSNumber numberWithInt: 15], @"vg", [NSNumber numberWithInt: 10], @"g",[NSNumber numberWithInt: nil], @"ig", like you did?, depending on how many words I want to have. :) – Pasod Jan 03 '12 at 21:20
  • Yes. If you have more than few dozen words, you may want to consider using an actual CoreData database. But hopefully it won't come to that. It sounds like you are on the right track now. Good luck! – Michael Dautermann Jan 03 '12 at 21:41
  • I'm sorry, but I can't manage to get it to work :( When I run the app it still comes out as nothing when I type in my words, and when I look at the code it says "unused variable". Any idea? I typed in: NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt: 20], @"mvg", [NSNumber numberWithInt: 15], @"vg", [NSNumber numberWithInt: 10], @"g", nil]; – Pasod Jan 04 '12 at 12:26
  • That sounds like a separate question that you can ask. I'm guessing there's something missing in your implementation, so when you post your new question, post some code too! :-) – Michael Dautermann Jan 04 '12 at 18:44
  • Oops, my bad :) I'm new to this site too and I can't put it in a fancy grey box like you did. But, anyways, under my @implementation I have written NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: [NSNumber numberWithInt: 20], @"mvg", [NSNumber numberWithInt: 15], @"vg", [NSNumber numberWithInt: 10], @"g", nil]; – Pasod Jan 04 '12 at 18:51
  • [dict objectForKey:textField3]; // This to say that the dictionary should respond with one of my textfields, is that right so far? NSLog(@"%@", [dict objectForKey:textField3.text]); //This was to see if it worked, which it did, when I typed in "mvg" the log said 20, but the program didn't count with that and gave me the answer 0. :/ – Pasod Jan 04 '12 at 18:54
  • ummmm... post a separate question with this code (you can't put formatted code in StackOverflow answer comments) and let's see how fast I or somebody else can answer. – Michael Dautermann Jan 04 '12 at 19:24
  • I just wanted to let you know that I managed to get it to work! Thank you very much, you have been the greatest help yet and I appreciate it so much! :) – Pasod Jan 05 '12 at 12:12