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
while loops | Intro to CS - Python | Khan Academy
What if you want your program to repeat a block of code? You could copy and paste those lines of code. But what if you wanted to repeat it 100 times or a thousand times? Or maybe you don’t even know upfront how many times you need it to repeat. To solve t…
EconTalk Host Russ Roberts on Key Economic Concepts for Founders
Russ Roberts: Welcome to the podcast! Hey, correct, great to be here! So, you, for those who don’t know, are the host of EconTalk, a research fellow at Stanford’s Hoover Institution, and the author of several books including “How Adam Smith Can Change Yo…
Your Family Tree Explained
This is you, this is your family tree and this is your family tree explained. You have parents, and your parents have parents. These are your grandparents, who also have parents - your great grandparents. Keep adding parents, keep adding “greats.” For eve…
A Nat Geo Earth Day Adventure | Branching Out | Trailer
April is Earth Month, a time to celebrate our commitment to the natural world. Our behavior impacts so many species on this planet, and it’s even threatening the habitats that we humans call home. Believe me, I’ve seen it. This island and this beach went …
You Can't Win Until You Overcome These Obstacles
It’s just a matter of time until you have to overcome these, so you might as well get ready. Here are 15 obstacles you will be facing in life. Welcome to alux.com, the place where future billionaires come to get inspired. Number one: your parents’ limite…
Commas in dialogue | Punctuation | Grammar | Khan Academy
Hello, grammarians, and hello, Paige. Hi, David! So, we’re going to talk about using commas in dialogue. I’ve got these two sentences here that I have removed all the punctuation from because I recognize that figuring out where to put commas when you are…