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

Mac Programming Lesson 6


4m read
·Nov 3, 2024

Hey guys, this is Mids And1. Today I'm going to be showing you how to get the listings of a directory through your application and how to display it in an NS table view.

NS table view in the Mac is very similar to UI table view on the iPhone, except that it has a very, um, subtle difference to it. So if you've done C before, you know that getting the contents or listing of a directory isn't that simple. But in Objective C, doing it is just one line away.

So I'm going to go into Xcode. I'm going to just demonstrate how to get the listings of a directory by making a terminal application that does it. I'm going to go to command line utility Foundation tool, and I'm going to make a new project called Liser. Liser is just going to have a list.m, and that's where we're going to put our simple code to list the directory.

So in here, we're going to declare a new NS array. I'll just call it listings, and we're going to assign it to be [NSFileManager defaultManager] directoryContentsAtPath:@"/". So that slash is the path; it's just the root directory. Then we're going to declare an int. We're just going to say for int i = 0; i < [listings count]; i++, and here's where we're going to NSLog the listings object in index i.

So here we go. If I look into the console and run this, it's going to print us every single thing in /, including hidden files. So as you see there by that little project, that's a terminal application. It's very easy to get listings of a directory.

But how do we basically put it into a great GUI application? I'm just going to create a new project that's going to be a Cocoa application, and I'm going to call it List D or GUI. So now under classes, I'm going to make a new file—Cocoa Objective CCL class—and I'll call it AppController. This is what I'm going to be using to talk to my interface building.

So under this, in the interface, we're going to add an IB Outlet for an NS table View, and I'll call it tableView1. We're going to have an IB Outlet for an NS text field, and I'll call it textField1. I'll have an NS mutable array, which I will call array1.

So tableView1 is going to be our table view that has a list of every file in the path. textField1 is going to be where the user types the path, and then NS mutable array array1 is going to be what has the contents in the current directory. Then we're going to add one IB action, and that will be listDirectory:(id)sender. So I'll just put this in the implementation down here.

So that way we can do this. First of all, this is a lot different than you might think it would work. It's not as simple as, say, [tableView1 setObjects:] or [tableView1 addObject:], [removeObject:] stuff like that. You have to do something similar to a delegate with tableView1 called a data source. When you set the data source on tableView1, it looks for certain methods in its data source.

For instance, it will look for this method in its data source. It will look for numberOfRowsInTableView:. So in this case, we're going to return [array1 count] because we want the contents of this table view to be whatever in array1. So this is how it knows how much stuff is in the table view and how much stuff to look for.

Then it's going to look for another method, not so short and sweet, called basically objectValueForTableColumn:row:. This is it asking what's at this place in the table view. So all we have to return here is, I'm going to cast it to be an NSString array1[indexPath.row] because we're giving it whatever is in array1.

So this is also very easy. Now in our awakeFromNib, we're going to set the data source of tableView1 to be self, so that way it looks here for all of them. So now we have it hooked up. Whenever it gets reloaded, it'll look in array1 for all its objects, and to refresh it, you just say reloadData right there, and that'll just make it try reloading data in this case based on array1.

So we're going to call that at the bottom of when they click the directory. First of all, we're going to remove all the objects that are already in our array1, and we're going to autorelease array1—just the memory management thing. Now we're going to set array1 to be the directory listing or the new directory listings of what they're trying to look at.

So say array1 = [NSMutableArray arrayWithArray:[NSFileManager defaultManager] directoryContentsAtPath:[textField1 stringValue]];. So that's easy.

Now array1 will be the listings of the directory. The reason we don't just assign it to be [NSFileManager …] is because that returns a regular array, but array1 in this case is an NS mutable array, not just an NSArray, so that's the distinguishable difference. Now we're going to do the tableView1 reload.

So that's it! Now if we just go in and edit the simple interface, we're practically done. The interface is pretty straightforward. It has a button, a text field, and a table view I believe is all the way up here. Here.

If I put this all together, it'll actually be a pretty cool application. There we go. I will decide to have one column, and I'll kind of drag this one over here and get rid of that. So all we have to do is drag over our friendly blue object and link it all up.

So we're going to call this AppController and link AppController with this, and we're going to link this button to AppController. Now we will be able to run it, type /, click the button, and see what's in /. We can do the same for /Applications and see what's in there.

So this is how to get the listings of a directory and put it in an NS table view. Hope you learned something! If you didn't, please ask the questions anyway. Thanks for watching, Mids And1. Expect great things and goodbye!

More Articles

View All
Millionaire Financial Advice For 18-35 Year Olds | Millennial Money
What’s up you guys, it’s Graham here! So for those who have not seen my second channel, The Graham Stefan Show, I regularly review a series by CNBC which covers the common everyday financial habits and financial mistakes of millennials, which is appropria…
Recognizing number pattern examples
We are asked which expression can we use to find the missing number in the pattern. So pause this video and see if you can try this before we do this together. All right, now let’s try this together, and the way I would tackle it is I’ll try to see, “Hey,…
Harj Taggar - How Startups can Compete with FAANG Companies when Hiring Employees
[Music] Hey everyone, I’m Harj. I’m a partner at Y Combinator and I’m gonna answer the question of how do I hire someone who has competing offers from the big tech companies. There’s two ways I think you can compete with someone who has an offer from a …
Bullets HITTING Bullets in Slow Motion - THE IMPOSSIBLE SHOT - Smarter Every Day 287
Three, two, one. Fire (BANG!) Hey, it’s me, Destin. Welcome back to Smarter Every Day. I am at the American Museum of Natural History. It’s a Smithsonian Museum. And this is something that I saw ten years ago, and it changed the way I think about bullets …
Why you shouldn't sell Real Estate part time
What’s up you guys? It’s Graham here. So a lot of people are asking me if they can sell real estate part-time, and my quick and short answer is probably not. It’s probably not a good idea, and here’s why. You compete with people who eat, breathe, sleep, …
Derivatives of inverse functions | Advanced derivatives | AP Calculus AB | Khan Academy
So let’s say I have two functions that are the inverse of each other. So I have f of x, and then I also have g of x, which is equal to the inverse of f of x, and f of x would be the inverse of g of x as well. If the notion of an inverse function is comple…