Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS project directory organisation

Just a quick question/opinion about how you arrange Xcode 4 directory for each of your iOS app you create.

Personally, I still add a directory called /Resources which contains sub-directories like /Fonts, /Images, /Icons, etc. I use the option "Copy items to destination group's folder (if needed)". If I need to add a new ressource, I simply add it to the right directory in finder.app in my project directory (i.e if it's an image, I add it to MyProject/Resources/Images/Foo.png) and then I simply go on my group "Images" in Xcode and add the file from there.

I once tried to use "Folder references" option to keep Xcode and Finder in sync, but IB was not able to get the images...

Also, is your Icon.png and Default.png at the root of your project? (i.e with the .xcodeproj file?) When you use Xcode 4 (in project info > summary) they add them there... Wondered if you keep them there, Cause personally I always prefer having all my images in a sample directory!

And what about photoshop files or any other files relative to your project, do you put them in the root directory too (again with the .xcodeproj file) ?

like image 870
allaire Avatar asked Jan 15 '12 17:01

allaire


2 Answers

I suggest keeping all your .m and .h and .xib files in a single folder because it simplifies localizing strings when you run ibtool to extract all the NSLocalizedString references, and it also makes it easier to move files around int the virtual folder structure within Xcode when you refactor your project.

Keep third party libraries or frameworks in a separate folder to avoid mixing them up with your own code.

Within Xcode itself, I organise these class files into separate virtual folders called Views, Controllers, Model and Nibs (and Libraries for 3rd party stuff). If it's a large project I break it into folders for each component and then break those into views, controllers, etc, but again that's only virtual folders, not physical ones.

Keeping resources in a separate physical folder is a good idea just to make it easier to browse them on disk. It may make sense to split these into images, sounds, xml, etc. Within Xcode you may want to break these down into virtual subfolders by component or category (interface, content, etc) but I wouldn't suggest doing that on disk as it makes it harder to refactor your folder structure as your project grows.

It's a really good idea to keep all your images in the same folder because it avoids you accidentally giving two images the same name. Xcode won't warn you if you import two images with the same name from different places, but when it builds the app you'll end up with just one or the other, chosen at random.

You don't have to keep Default.png and Icon.png in the root (or even call them Default.png and Icon.png) but sometimes Xcode gets confused if standard files aren't where it expects.

And yeah, whatever you do, don't try to use the blue folder references when you import images otherwise you can't access them from within your code with imageNamed: or within Interface Builder. Use the yellow virtual folder references instead. Basically blue folders get copied into the app bundle as actual folders when the app is built, whereas the yellow folders are ignored and their contents go directly into the root of the app bundle. To access content in blue folders, you'll need to include the blue folder names in the path when you load them, e.g.

NSString *path = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png" inDirectory:@"nameOfBlueFolderInXcode"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; 
like image 154
Nick Lockwood Avatar answered Sep 18 '22 13:09

Nick Lockwood


I've been thinking about this a lot lately; here's the structure I decided to go with:

ProjectName
ProjectName/Assets
ProjectName/Assets/Images
ProjectName/Frameworks
ProjectName/Logic
ProjectName/Logic/Models
ProjectName/Presentation
ProjectName/Presentation/ViewControllers
ProjectName/Presentation/Views

I think this gives a good structure for growth and provides a reasonable home for most anything. Of course, add additional subfolders (e.g. Assets/Fonts) as needed. For my full rationale for the structure check out this blog post.

I do keep things like Icon.png and Default.png at the root of the project as Xcode seems finicky about it, but otherwise things are neatly organized. I keep my corresponding .m, .h, and .xib files together, but I split up the view controllers logically into subfolders by feature (e.g. Initial, Settings).

like image 27
camdez Avatar answered Sep 21 '22 13:09

camdez