Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Placing a number variable in a string

From: Programming for Non-Programmers: iOS 7

Video: Placing a number variable in a string

Let's say we want to put a variables value inside of a text field. An example of this would be if you're creating a game. So, in order to do that, we're going to need So again I'm declaring a score variable, and since this is not Now when I type that you will see an error message.

Placing a number variable in a string

Let's say we want to put a variables value inside of a text field. An example of this would be if you're creating a game. And there's a text field that holds the score of the game. And every time the player scores a point, you want to update that text field with the player's current score. So, in order to do that, we're going to need to put a number variable inside of a text field. So, let's go to ViewController.h. And right below the line where I create the IBOutlet, going to go to the next line by creating a new line and type int score.

So again I'm declaring a score variable, and since this is not an IBOutlet, there is no circle to the left of my variable declaration. So save this file and go over to viewcontroller.m. Now right above label.text, I'm going to create a new line of code, and I'll type score. Again, we don't type int before it because we already declared it in the h file, and I'll set that equal to 10. So now you'll see, if I want to put the score in the text field, let's try deleting the string and typing the word score.

Now when I type that you will see an error message. That's that red sign with the exclamation point inside of it. When I click the error message, it says implicit conversion of int to NSString is disallowed. So what we need to do is convert that integer into a string. And here's how to do that. I'm going to change the word score to an open and close square braces. Open square brace, then a closed square brace. And put your cursor in the middle.

Then I'm going to type NSString. You might remember that NSString is the type of data that we use for a string value, or in other words, text. In addition to being a data type, NSString is also a class, which means it has commands that we can do. And, values inside of square brackets enable us to run commands. These kind of commands are called functions or methods, which we'll talk about later on. So, after NSString I'm going to type a space. And then I'm going to start to type string, and once I see stringWithFormat highlighted in the code hinting window, I'm going to press Return on my keyboard.

This command is very similar to the NSlog command, where we use a placeholder value to put a variable inside of a string. So let's look at how that works. Type an at symbol and two quotes. Inside the quotes, type score is and then the placeholder value. Remember, the placeholder is percent i. And again, that's a placeholder for some type of integer value. The area that we place the integer value is after the closed quote, we type a comma.

In the comma, we type the integer value. So here I could type 100 or whatever number I want and it'll show up in that string. I'm going to delete 100 and type score. Now we'll test the application in the simulator and we'll see score is 10. And there we go. I'm going to click the Stop button now, and now we can manipulate the score any way we want. For example, after the score is 10 line, I'm going to go to the next line and type score plus plus. When I test the application in the simulator, I see score is 11.

So the line of code where we set the value of the text field doesn't care what the value of score is. It's putting the value of the score variable, whatever it happens to be currently, inside the text field. So again, if you want to convert an integer value into a string so you can put it in a text field, use the NSString, stringWithFormat command. After the colon, type in NSString, which is an @ symbol and two quotes. Type in any text you want.

And then a placeholder value for your integer, which is percent i. After the close quote, type a comma, then the name of your variable, or an integer value. And then you'll get your integer value inside of a text field.

Show transcript

This video is part of

Image for Programming for Non-Programmers: iOS 7
Programming for Non-Programmers: iOS 7

42 video lessons · 9439 viewers

Todd Perkins
Author

 
Expand all | Collapse all
  1. 5m 18s
    1. Welcome
      44s
    2. What you should know before starting this course
      1m 17s
    3. Related courses
      1m 8s
    4. Using the exercise files
      1m 19s
    5. Viewing the finished app
      50s
  2. 10m 24s
    1. Finding and installing Xcode
      35s
    2. Creating an Xcode project
      1m 55s
    3. Understanding the Xcode interface
      3m 54s
    4. Configuring Xcode for app development
      2m 14s
    5. Configuring the iOS Simulator for app development
      1m 46s
  3. 43m 43s
    1. Understanding how programming works
      2m 34s
    2. Understanding variables
      2m 56s
    3. Using primitive variables
      9m 10s
    4. Using pointer variables
      4m 51s
    5. Using instance variables
      5m 19s
    6. Connecting visual objects to variables
      8m 12s
    7. Placing a number variable in a string
      4m 33s
    8. Challenge: Create two variables
      54s
    9. Solution: Create two variables
      5m 14s
  4. 27m 14s
    1. Understanding functions, methods, and selectors
      4m 43s
    2. Using functions, methods, and selectors
      7m 1s
    3. Understanding parameter functions
      2m 10s
    4. Connecting a button to a function
      7m 47s
    5. Challenge: Create a counter app
      43s
    6. Solution: Create a counter app
      4m 50s
  5. 13m 38s
    1. Understanding conditional statements
      2m 35s
    2. Using conditional statements
      5m 21s
    3. Challenge: Build an on/off button app
      39s
    4. Solution: Build an on/off button app
      5m 3s
  6. 44m 59s
    1. Viewing the app's code structure
      2m 11s
    2. Setting up the user interface
      7m 9s
    3. Setting up variables and functions
      5m 8s
    4. Connecting all of the visual elements to code
      2m 59s
    5. Displaying tapped numbers in the calculator
      4m 47s
    6. Controlling when tapped numbers should not appear in the calculator
      3m 27s
    7. Making the Clear button clear all values
      1m 4s
    8. Setting the calculator to add or subtract
      4m 29s
    9. Showing the total when the equals button is tapped
      3m 40s
    10. Formatting a number with commas
      5m 25s
    11. Challenge: Add a multiplication button
      1m 5s
    12. Solution: Add a multiplication button
      3m 35s
  7. 24s
    1. Next steps
      24s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Programming for Non-Programmers: iOS 7.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.