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
Why Top Investors are Warning of a 'Lost Decade' for Stocks
A few weeks ago, Goldman Sachs put out this note saying that they believe the S&amp;P 500 during the next 10 years will deliver a real return of just 1% annually. It’s a bit of a dire prediction. As you may have seen in the news over the past few weeks, i…
When Cities Were Cesspools of Disease | Nat Geo Explores
Imagine living in darkness. You’re in a roof the size of a closet with your entire family. I can’t see a thing, but you can hear and smell everything—every breath, every sneeze, every cough that hits your face. This is life in a 19th-century city. There’…
The Problem with Super Chickens | Podcast | Overheard at National Geographic
Let’s start with the riddle. Picture a long flat building in rural Indiana, surrounded by corn and soybean fields. There are thousands of chickens inside. Oh my goodness, it was a lot of noise! They’re calling under the rooster sounds and copper glue. Tha…
Summer of Soul | National Geographic
(Fast-paced drumming music) [Man] What time is it? ♪ This is the dawning of the age of Aquarius ♪ “Summer of Soul” is about the Harlem Cultural Festival in 1969. With so many greats of music in the day, Tony Lawrence and Hal Tulchin came up with an ide…
When Life Hurts, Care Less About It | The Philosophy of Marcus Aurelius
Once the most powerful man in the known world, Marcus Aurelius attempted to live virtuously, following Stoic principles. Unlike many Roman Emperors, he did not indulge in the many pleasures he had access to, like getting drunk on wine and watching sadisti…
Adding decimals with thousandths | Adding decimals | Grade 5 (TX TEKS) | Khan Academy
So what we have here are two questions where they’re asking us to add decimals. So pause this video and have a go at this before we do this together. All right, so let’s tackle this first question up here. What I like to do is line up these numbers based…