1

Hi I am seeking some insight into storing text input into a variable. My task is to take five text inputs, store the numbers the user inputs, then do some fancy math with them to create a solution variable to display. However, within the SDK I am getting confused quickly on how to actually store these correctly. I can't find much tutorials on this online, seeking gurus to help!

2 Answers 2

1

Pretty straightforward stuff.

Declare the variable:

 NSString *aString;

Then assign the string:

aString = someTextField.text;

If you're going to need the string to stick around for any period of time beyond the method where you capture it, you might consider making it a property of the class:

@property(nonatomic, copy) NSString *aString;

That's just for a string. If the user is inputting text that you want to treat as a number, you can do:

NSInteger userInputInteger = [someTextField.text intValue];
Sign up to request clarification or add additional context in comments.

2 Comments

TY Danilo. I really appreciate your help. I'm going to muck around with this and see what I can break :)
You got it! Good luck. The docs in Xcode are pretty thorough. Very useful once you learn to get around in them. A quick read through the Objective-C 2.0 Programming Language guide and you'll be all set.
1

The question is a little vague… depending on what you want to do there are many ways:

  • Temporary storage. Set up a NSString variable to hold the input, then do whatever you need to do to validate/convert etc

NSString *input1 = textField.text;


  • Storage Over one run of the app. Setup a global variable in a custom class, model, or view controller that will not be dealloc'ed

[MyStorageClass setInput1:textField.text];

  • Save over multiple launches of the app. Put the vars in NSUserDefaults

[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"input1"];

1 Comment

sorry. store the input into a variable that will be used only during run of app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.