yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Objective-C iPhone Programming Lesson 2 - Basic Variables


5m read
·Nov 3, 2024

Hey guys, this is Matt Kid101 with our second iPhone Objective-C programming tutorial.

In the previous tutorial, I taught you how to set the text of a label to the text of a text field and how to add to that text. Today, I'm going to be showing you how to set the text of a label to a number, and I'm also going to be teaching you about different types of variables and strings.

So let's get started by making a new program, and I'll call it Counter. What this is going to do is there's going to be a label on the screen and a button on the screen. When you press the button, a number in the label will go up. So we're going to obviously have an IBOutlet for UILabel, and I'll call that myLabel.

We're also going to want to have an IBAction for button press. All right, and so now let's just implement this. All right, so let's do the stuff we already know. First, let's set this up in Interface Builder. That's really the first step I always do. Occasionally it's last, and then I build and go, and I haven't set anything up; it's beside the point.

So, all right, I'm button, and we'll make it say "Increase." Insulin, there. And here we'll link myLabel back to this, and if we run it now, nothing will happen, but we know it's set up.

All right, so let me just refresh your memory here. If you don't remember, we can say myLabel setText: and then after the colon is something that represents text. You can do @"a piece of text" and close bracket, semicolon. So we can do something like this. All right, and if you see here, it changes to one because I set the text to one.

Now, there's a universal key we have to understand before we can make this number continue to change. All you have to understand is that while the program is running, this code will keep on getting run again and again.

All right, now if we look back here, this is a variable myLabel. We can access myLabel, and it stays on that on the interface even when that function, even when we're not inside of there. Right? So, even while the button is not being pressed, myLabel still exists.

So we want something like that that always exists, and we want something like that that always exists that represents a number. So I'm going to say int myNumber right here. This is something called a variable; the type is an int, and that means that it's a number. The name of it is myNumber.

All right, there are several things you can do with numbers. You can say myNumber equals a constant, which is just a number. You can say it equals a constant plus a constant. You can say it equals another variable plus a constant, another variable plus another variable. You can do different mathematical arithmetic operations like time, divide, et cetera.

We're going to say myNumber plus 1 because we want to add on to this number. So now if we run, nothing will be different, but under the hood, myNumber will keep on getting bigger and bigger; we just won't know yet.

So the question now is, how do we get a piece of text that represents myNumber? You might think we could just say myNumber here, and it would work. Well, it's not going to work because myNumber represents a number, not a piece of text. You run this, it's probably going to get us an EXC_BAD_ACCESS, and if we take a look, that didn't even tell us the error we got.

Anyway, that's not something you want to do because if you're a computer guy, you'll understand it tries to read a pointer that's the integer, and it won't work.

Anyway, the way we get a piece of text that represents this number is this: we can have a piece of text variable as well, and this is a variable that will keep on getting that'll only exist inside of this function right here. We're going to call that myNumberString, and right now, NSString * means this is a text variable—meaning this represents a piece of text.

We can say this equals another piece of text, and that's all we can do. So we can say equals @"1," and that would work. But there's a way to get a special piece of text that already exists in memory that has a numbering, and that's [NSString stringWithFormat:].

I'll explain this to you in a second. MyNumber, and this represents a string, and therefore this represents this, and that represents this. So if we run this now, it will work.

Let's go back here and explain to you what this code is. This whole thing represents a string. This right here – first of all, it's left bracket and a string. Now remember how we did left bracket, the name of something, space, text, right bracket? That was a function that returned a string.

This is also a function that returns a string; it's just that you give it something with this colon, and what you give it is another something that represents a string.

So we got a lot of strings here. We're saying something that represents a string equals string, and then we're giving that a string. And if we take a look at what's inside this, this @"%d," you can see there's a percent d there. This is a magic thing that when it's for a format, so stringWithFormat, for instance, it means I want you to put a number here later.

Then we have to tell it what this number is, so we do a comma, and then the number that we're giving it. So one more time, let's review it: it replaces %d with myNumber, and that works.

What would happen if we wanted multiple numbers here? Say we wanted to say myNumber dash and then myNumber. We can do another comma to another thing that represents a number, and this would be the second number. So I don't know why you want to do this specifically, but yet you can say 3 here.

And if we do 3, it will say myNumber dash 3. Look at that! You know, I don't know why that would be useful in any way, but later on, you're going to want to have multiple things in these formats. It's just an important thing to know.

But there's one more thing. If this represents a string, and we're putting it there, but this also represents text, can we just put it there? And the answer to that is yes, we can, and this will work the same.

Also, if we want to say "Number: ", we can just do @"Number: " right there, and "Number: " doesn't mean anything magic, so we'll try to replace that. That is the "Number: ".

So that was just a brief tutorial on some variables, data types, stuff like that. You can go into more depth of this by yourself. I just wanted to expose you guys to numbers and variables and stuff.

We're definitely going to be using this in the future, so I would make sure you know it. I am going through this pretty quickly, hoping you guys will learn it. But if you can't, which is completely understandable, just ask the question comments on the video. I'll try to respond to all your comments as fast as possible.

But you can also Google it; that's what I actually do if I can't figure something out. But, um, yeah, so anyway, thanks for watching my kids, and unsubscribe, and goodbye.

More Articles

View All
Why You'll Regret Buying A Home In 2022
What’s gram up! It’s guys, you here. So, the other day, I was minding my own business, reading the internet while sipping on some coffee from bankrollcoffee.com. And all of a sudden, this headline hit me like a ton of bricks: A brand new survey just repor…
Every Mathematical Theory Is Held Inside a Physical Substrate
There goes my solution for Zeno’s paradox, which is: before you can get all the way somewhere, you have to get halfway there. And before you can get halfway there, you have to get a quarter of the way there. And therefore, you’ll never get there. One way…
Remove the Dams to Save the Salmon? | Short Film Showcase
[Music] [Music] If you think about the way a river works in a landscape, it essentially functions as the circulatory system. It drains the waste products off of the land, and that sediment is the stuff that basically structures habitat in rivers. Then, on…
Life of Muhammad and beginnings of Islam part 1 | World History | Khan Academy
Now going to give an overview on the beginnings of Islam. Regardless of whether you are part of an Islamic culture, you are a practicing Muslim, or you believe in the Islamic Traditions, it’s valuable to learn about the beginnings of Islam because today n…
City So Real | Official Trailer
[Music] Hello, yes, I’m doing a documentary right now. Could you please give me a call back? What do I think about the city? I actually love it and hate it. That’s Chicago, though. It’s our one big happy family. The cries and the complaints of the people …
What causes seizures, and how can we treat them? - Christopher E. Gaw
Nearly three millennia ago, a Babylonian tablet described a curious illness called miqtu. Said to cause symptoms ranging from facial twitching to full body convulsions, the Babylonians believed those afflicted were possessed by evil spirits, and the only …