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
America's native prisoners of war - Aaron Huey
[Music] [Music] I’m here today to show my photographs of the Lakota. How many of you may have heard of the Lakota or at least the larger group of tribes called the Sioux? The Lakota are one of many tribes that were moved off their land to prisoner of war …
With Grace | Short Film Showcase | National Geographic
[Music] [Music] Thank you, thank you. [Music] Come on, I’ve been happening. Okay, okay. [Music] You can even take overnight. Sometimes a day can pass or two. Okay. Foreign [Music] Grace, so I went home to catch up some rest. Around 23 hours, I had a knock…
Bird migration, a perilous journey - Alyssa Klavans
Each spring, nature lovers eagerly greet the signs that winter is finally coming to an end. As the days grow warmer, the flowers and trees start to bloom, and the world comes alive with the melodious call of birds in search of a new mate. But where have t…
Restoring a lost sense of touch | Podcast | Overheard at National Geographic
[Music] As a kid growing up in the late 70s, science fiction was all about bionic body parts. There was the six million dollar man with the whole “we can rebuild him better than he was before,” and then most famously in a galaxy far far away there was Luk…
Safari Live - Day 308 | National Geographic
This program features live coverage of an African safari and may include animal kills and caucuses. Viewer discretion is advised. Good afternoon everybody! Welcome to drama in the Sabi Sands, where it is 34 degrees Celsius. That’s about 87 Fahrenheit. I …
Your brain is biased by default. Here’s how to reset it. | David Eagleman
Why do we accept our reality as the uncontested truth? You are a data collection machine that moves through the world and you vacuum up your little bits of experience that you have, and in the end, whatever you have, that’s what you assume to be true. But…