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
What's Changed In The American Economy? | Montana On The Rise
[Applause] [Music] Thank you very much, I appreciate it. Um, I would like to talk a little bit about the changes in America that have occurred over the last two and a half years. Obviously, everybody’s gone through this pandemic, but it’s what it’s done t…
TIL: Choosing a Mars Landing Spot is Harder Than You Think | Today I Learned
If you have an entire planet to explore, where do you go? Mars is a place where we can get rovers on the ground, but what is the one site that will tell us the most about Mars? So first, can we land there? Is it safe? Second, do we want to land there, an…
My Tesla Model 3 Regrets | The TRUTH After 15,000 Miles
What’s up you guys? It’s Graham here. So, I guess you could say time flies when you’re having fun because it’s officially been over a year since I purchased my Tesla Model 3, and 15,000 miles later, quite a lot has happened. Now, even though I’ve had way…
Identifying force vectors for pendulum: Worked example | AP Physics 1 | Khan Academy
We’re told that a ball attached to a string swings in a horizontal circle at constant speed. As shown below, the string makes an angle theta with the horizontal. Which arrows show all the forces on the ball? So pause this video and see if you can figure t…
The World isn't Nearly as Terrible as We Think (or is it?)
As soon as we turn on the radio or television, or scroll through our social media feeds, a rush of tragic events scourges our minds. From pandemics to street violence, from clashes between countries to changes in climate: if we immerse ourselves in these …
LA92 Panel - Live | National Geographic
[Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Laughter] [Music] [Laughter] [Music] [Laughter] [Music] [Laughter] [Music] [Laughter] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] [Music] you you [Music] you [Mus…