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

Mac Programming Lesson 3 part 1


5m read
·Nov 3, 2024

Hey guys, this is Mat. Kids in one, today I'm going to be showing you how to make an application that I call Lottery. This application will come up with 10 dates that are seven days apart, starting from today's date, and then will come up with lottery numbers for each date.

So, um, in this tutorial, I'm going to show you the new things that you're going to learn, which are how to create a secondary class and use that class in other classes. I'm going to show you how to use NSDate, and I'm going to show you how to do a random number, as well as a few more math things.

So, I'm going to open up Xcode. I'm going to go to File, New Project, Cocoa Application, and I'm going to call it Lottery.

Okay, now under Classes, I'm going to want to create a new class: Cocoa Objective-C class, and I'm going to call it AppController. AppController is what we're going to link up with all our interface builder stuff; it's going to have the IB actions and the IB outlets.

Now we're going to create a new class called LotteryEntry. This is going to be what one entry is, so it's going to have a date in it and stuff like that. The way this is going to work is I'm going to make an int: firstNumber, secondNumber, and thirdNumber, and they're going to make an NSCalendar date.

Okay, so you guys probably know kind of more or less the syntax of this, especially if you watched our Java lessons a while ago. The way it works for functions is, um, a lot different than Java though, but the way it works for variables is the same. So every single one of these variables acts as a private variable, so there's no way for other external classes to set those. So we want a way for them to set it.

So here we're going to have another void, and we're going to call it setDate. The first parameter right here that we specify is going to be an NSCalendar date: newDate. Now you guys probably know the syntax of voids more or less, but how do you do multiple parameters?

Well, basically the equivalent of—let me just show you—void fu: fu1, fu2, and then you could refer to one and two. The way it works here is you would do the first parameter just as the function name colon and then the first parameter; the second parameter or you have to give it a whole new name.

So, I'm going to make it firstNumber, and then int first. Now I'm going to make another parameter: secondNumber. I'm going to make an int: second, and then I'm going to say thirdNumber, int third.

Okay, so this is a very wide function declaration, and the cool thing we can do with this is we can just hit enter a few times between each parameter to space it out a little bit, make it a little less wide and a little taller. So that's pretty cool, and so now it fits on one line.

So now we're going to make an NSString declaration that they will be able to call—that's a function called getStringVersion. Okay, and then the last void is going to be deallocate, and that's just going to be used for, um, not having memory leaks and stuff.

So we're going to copy all these three functions, and we're going to implement them in ourm, so paste this here and let's start it out like this—pretty cool! In here, we're going to do a few simple functions.

So for the setDate, you can probably guess what we're going to do; we're just going to say date equals newDate, firstNumber equals first, secondNumber equals second, thirdNumber equals third, because they’re passing in variables called first, second, and third.

Okay, so that's easy for that function. The next function, getStringVersion, we're just going to return an NSString that is like the date in text and then the entry in text. So I'm going to return right here because this is an NSString. The equivalent of this in Java would be something like NSString *f whatever, um, so this is like the equivalent of that.

So I'm going to want to return an NSString; there's a really easy way to make an NSString that has a format. You don't have to do NSString alloc init with format; you can just do left bracket NSString space stringWithFormat:format, and that prevents memory leaks as well.

So in the format, we're going to put a number and a slash, then a number, then a slash, then another number, and so the numbers are going to be like the month or the day, the month, and the year or whatever—the month, day, and year—and then hyphen, just to space it out, and then a number, a number, and a number, and those numbers are going to be firstNumber, secondNumber, thirdNumber.

So now, as the firstNumber before the first slash, we're going to say date monthOfYear; so left bracket date space monthOfYear right bracket, and that'll get the month of the date that has been set.

Then I'm going to say date dayOfMonth, because date is actually a variable we have declared up here that this function sets, so luckily we’ll have a date. It might not be today's date, but it’s going to be a date that we’re going to print.

Then we're going to do date yearOfCommonEra, then we're going to do firstNumber, secondNumber, thirdNumber, and this method is very, very too much wide, so we're going to space it out a little bit once again. Just going to do an enter here, the line here, and press enter.

So there we go; so now this is going to look something like like 91 2001, then numbers and so forth. So that's going to work well. And then the allocate is just going to be really easy: [date release], because date is an NSCalendar date and release just says, "Okay, I'm not going to use date anymore, so please, if it’s not being used, just get rid of it," and so that’s how I’m doing that.

So now in AppController, we're going to import LotteryEntry. So this way, we can declare a LotteryEntry.

Now another new thing we're going to learn in this lesson is arrays, and an array—in this case, we're going to do an NSMutableArray—which is an array that you can add stuff to, sub, you know, delete stuff from, and you know, mess around with and edit after it's been initialized.

So we're going to declare an NSMutableArray, and we're going to call it entries, and normally in that we are going to put something like 10 LotteryEntry classes that have different dates and different numbers.

And then we're going to do—let me just see, look at my notes—an IB outlet for, in this case, an NSTextField that, um, I'm going to be using, so this is going to be called entryText. I'll call it entryText for the—okay, there we go!

So now we're going to add one IB action: generateNewNumbers. Id sender, okay? And so we can just implement this one IB action; it’s pretty simple.

So in here is where all the magic's going to go, and everything's going to come together perfectly.

More Articles

View All
Anti-Natalism: The Argument To Stop Giving Birth
Suppose there is a couple, the Joneses, who just gave birth to a baby boy named Sammy. As they stand together in the hospital gazing down at their newborn, they share an awareness that the life ahead of Sammy will be filled with an indeterminable amount o…
Gas mixtures and partial pressures | AP Chemistry | Khan Academy
In this video, we’re going to introduce ourselves to the idea of partial pressure due to ideal gases. The way to think about it is to imagine some type of a container, and you don’t just have one type of gas in that container; you have more than one type …
Dream - Motivational Video
I don’t know what that dream is that you have. I don’t care how disappointing it might’ve been as you’ve been working toward that dream, but that dream that you’re holding in your mind, that it’s possible! That some of you already know. That it’s hard, i…
Never Ending Problems (Solution for Life)
We recently went through a series of unfortunate events that got us extremely annoyed. By the way, when we say “us,” we usually mean some of us from the team or all of us. In this case, it was some of us. But the point is, none of these events, taken indi…
Generational and lifecycle effects on political ideology | US government and civics | Khan Academy
What we’re going to talk about in this video is how political and cultural events might have an influence on someone’s political ideology. So let’s make this a little bit concrete. Political scientists will sometimes have various classifications for thes…
Hershey and Chase conclusively show DNA genetic material
In the last video, we began to see some pretty good evidence that DNA was the molecular basis for inheritance. We saw that from the work of Avery, McCarthy, and Mlead, where they tried to identify whether it was DNA or proteins that acted as a transformat…