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 70% of Millennials Are About to Quit Their Jobs
What’s up, guys? It’s Graham here. So, one year ago, we were introduced to a topic that most people never expected. It was called the Great Resignation, where forty percent of workers thought about quitting their jobs, with “I quit” being the sign of an e…
REAL CYCLOPS SHARK and more great images -- IMG! #46
Rober De Niro and Rober De Faro and shoelace love. It’s episode 46 of IMG! Here’s a great infographic about New Year’s resolutions. For instance, 88% of them fail. But you see these images of cats? They are cat scans. For something sweet, check out SCAND…
Proof: Matrix determinant gives area of image of unit square under mapping | Matrices | Khan Academy
The goal of this video is to feel good about the connection that we’ve talked about between the absolute value of the determinant of a two by two matrix and the area of the parallelogram that’s defined by the two column vectors of that matrix. So, for ex…
Introduction to powers of 10
In this video, I’m going to introduce you to a new type of mathematical notation that will seem fancy at first, but hopefully, you’ll appreciate is pretty useful and also pretty straightforward. So let’s just start with some things that we already know. …
THE FED JUST CRUSHED THE MARKET | Urgent Changes Explained
What’s up, Grandma’s guys? Here, and welp, it happened. As of a few hours ago, the Federal Reserve yet again raised their Benchmark interest rates by another 50 basis points, officially bringing us to the highest rates that we’ve seen since 2007, right be…
THE AMERICAN DREAM 🇺🇸 IS NO LONGER ATTAINABLE?
So many young Americans feel that the American dream is unattainable. You’ve got to remember these young people, this cohort of individuals, men and women, never lived in a time of rising interest rates or inflation. Obviously, these are great cycles, but…