Best way to store large number of files in Swift - swift

I currently have around 4,000 SVG files in the Project directory of my macOS app.
And Fetching them using the line below, and it's working as expected.
Bundle.main.url(forResource: "icon1", withExtension: "svg")
But due to the large number of files I'm having issues such as slow searches in the project directory in XCode.
I'm looking for a better approach to storing these files.

Put them in an asset catalog.
If the only issue is in Xcode, you can also make the directory a folder reference (drag the folder into Xcode and select "folder references" rather than groups. This will keep Xcode from managing the folder directly. It'll treat the entire folder as one "unit."
If the main issue is searches, you can create a scope that excludes your images.

Related

subfolders in iPhone localization folders

I would like to localize some images in my iPhone project. So I created files:
en.lproj/Images/iPad/btn-check-pressed~ipad.png
en.lproj/Images/iPadRetina/btn-check-pressed#2x~ipad.png
ru.lproj/Images/iPad/btn-check-pressed~ipad.png
ru.lproj/Images/iPadRetina/btn-check-pressed#2x~ipad.png
and so on and added them to my project. But Xcode shows "English 0 files localized", "Russian 0 files localized" in Localizations list.
It also shows a warning
Warning: Multiple build commands for output file /Users/User/Library/Developer/Xcode/DerivedData/TestLocalizationDefaultPNG-ckplzmcjurofxrccjuvyzjaqketc/Build/Products/Debug-iphonesimulator/TestLocalizationDefaultPNG.app/btn-check-pressed~ipad.png
for each of my files when I try to build the project. So, as far as I understand, it copies all files in one folder, and since my files have the same names - only one of them can survive. But, if I remove my subfolders:
en.lproj/btn-check-pressed~ipad.png
en.lproj/btn-check-pressed#2x~ipad.png
ru.lproj/btn-check-pressed~ipad.png
ru.lproj/btn-check-pressed#2x~ipad.png
everything works fine.
Is there is a way to keep subfolders? Here http://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html in Listing 2-4 subfolder used for the audio files, so it should be.
It looks like the only way localization works in iOS is different. You base folders hierarchy should be by inverted: first goes actual folders structure and in each and every folder you want to localize, you put corresponding .lproj sub-folder with localized resources. So in your example it should be something like:
/Images
/iPad
/en.lproj
btn-check-pressed~ipad.png
/ru.lproj
btn-check-pressed~ipad.png
/iPadRetina
/en.lproj
btn-check-pressed#2x~ipad.png
/ru.lproj
btn-check-pressed#2x~ipad.png
This might be sub-optimal if you intended to have big hierarchy but this is the only way it works out of the box.
Of course you can always say that you don't need Xcode support and use some custom build rules to re-arrange files in you project in the way you like and then copy them into proper structure during build but I doubt it worths troubles.
Update: it looks like XCode (4.5) build script is the main villain.
According to my experiments build script should flattens resources structure at least for images. So the only way localized app can be inside is:
/YourApp
YouApp
info.plist
....
/en.lproj
btn-check-pressed~ipad.png
btn-check-pressed#2x~ipad.png
/ru.lproj
btn-check-pressed~ipad.png
btn-check-pressed#2x~ipad.png
The trick is that by default XCode (as of version 4.5) can flatten your project structure only if it is as described above and I don't see a standard way to change this behavior.
Of course, original comment about custom build scripts is still true.

Why in Xcode, Groups can be created, but not real folders?

When doing iOS programming, it is interesting that in Xcode (4.3.2), we can create groups, such as a group call Images, and add files to it (either as a link or choose "Copy items into group's folder"). So it looks like a real folder in the navigator, and it even mentions "copy into ... group's folder", but in fact there is no folder. All added files are in the same location as the .m and .h files.
Why would we want Groups, but not real folder? Is there advantage of Groups over the use of a folder?
This is a pet peeve of mine.
I recommend, making the folder in your file system, where you have your project files, then drag that folder into Xcode where you want the group. It will act like any other group, but now be linked to the folder on the file system. Adding files to that group in Xcode now adds them to the folder in the file system.
Much cleaner way of working and helps when locating files in big projects. Keeps git cleaner too.
Update for Xcode 9:
Once you have the groups in Xcode matching the directory structure on disk, moving a file from one group to another in Xcode will now move the file correctly on disk to match.
Note: In Xcode 10, the default behavior now creates a linked folder when you create a new group. The information below still applies otherwise.
The recommended way to organize files is via groups, because it's more flexible than creating directories, and allows you to create complex file hierarchies while maintaining a totally different file hierarchy on the file system. It's simply an organization tool. That said, it is possible to turn groups into folder references, or create them initially as folder references.
Furthermore, using groups instead of folder references gives you greater flexibility if you need to change where things are in the file system - say you have a common folder of code that you use in a bunch of different projects. Groups allow you to organize the files inside of projects as if they were in the projects, while still maintaining one copy of the code in a central location. And yes, you can do this with folder references as well, but groups are much more flexible if you later want to add other things to the same group but don't want them to be added to that common folder.
If you want to link a group to a folder, click on the group in the project file tree and hit command+option+1. Below the drop down that'll say something like "Path: Relative to Group" on the right, there's a little white square with a grey border around it kinda icon next to the word "none". Clicking on this and then selecting/creating a folder will bind the group to the folder.
Furthermore, when you drag a folder into Xcode, it will ask you if you want it to be a group or a folder reference:
There is a Command Line Tool - "synx"
available in github that do exactly what you need.
It reorganizes Xcode project folder in finder to match Xcode groups in project.
You can find it here:
https://github.com/venmo/synx
UPDATE: XCode 9 supports this feature by default. So, no need to use other tools anymore!

iPhone project - collect files into one place

Sorry if this has been answered before but all my searches do not return anything related to this.
Is there a way to collect all the files referenced in a project and save them in the procject folder automatically? Rather that having links to places where you may accidentally delete the files.
Thanks,
Eds
Xcode doesn't have a particular feature to support this, but when you add items to your Xcode project it does give you the option to copy those items to the project directory. Otherwise you need to manually copy the items to a common location.
What do you mean collect all the files? Do you mean your external files or class files. Your external files like images or audio/video files should be added into Resources directory, it does not matter where is the root directory of these files. Then, you can access with their name in the project.

XIB Files in Classes folder, is it a problem?

So in my XCode project I have a handful of XIB files that for one reason or another are physically in the Classes folder on the filesystem, but show in the Resources folder in XCode. Is there any problem with that? Should I bother trying to move them?
The code is working fine, runs on simulator and device, so my main concern is whether it will cause any problems when I submit to Apple, or anywhere else down the road.
It's absolutely meaningless - the only thing that affects your final product (and therefore what you submit to Apple) is which stage of your Target the different files appear under, and in my experience those rarely end up wrong.
However, for tidiness' sake if nothing else, it might be worth fixing the problem. Move the files to the right place*, which will turn the filename red in XCode. Choose Get Info on each red file and click the "Choose" button to locate it.
(* if you're using Subversion, use the svn move command for this, not the Finder)
All the resource files are stored in a flat directory in app bundle. You can control click on .app file and see the flat directory by selecting reveal in finder. It really does not matter where the file is physically present in source. If you don't have any problem with the management of the code by placing some resource files in class directory, then there will be no problem. That means Apple will not mind.
However if you are planning to maintain the project for a long time with multiple people then I think it is better to have well organized source.

iPhone SDK - Adding zipped content in resources and then unzipping into Documents folder

I have some resources (zipped) that needs to be shipped with my iphone application. When the app launches for the first time, this zipped file needs to be moved/copied to the Documents folder and unzip it there. User can then add more files to this path from the application. Can someone please suggest how can I achieve this?
Thanks!
Based on your comment above:
The reason I want to add a compressed
resource because there are multiple
files. If I don't compress then I'll
need to move files individually. I'll
also need to maintain a list of files
somewhere so that I can read the file
name and then move them. I thought
zipping and unzipping was a simpler
solution.
You could add all the files to a folder in your bundle. When the app launches for the first time use fast enumeration to run through the folder and what ever it finds in that folder, it copies into the Documents folder. Handling folders within folders is slightly more complex (add recursion maybe). This way you don't have to worry about zip or tar, nor to keep a directory of files to install.
Just place the folder of files you want into Xcode's resources folder and tell it to import as a folder not as a group. That way the files get installed in your resources inside a folder instead of just as individual files all over the place.
EDIT:
Better yet, do what I say about putting all the files you want in one folder, add to your project, but not as a "Group", and then at first launch use:
[[NSFileManager defaultManager] copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error];
and it will copy your whole directory from one place to another. EASY!
Add the libz.dylib Framework to your project, and include Deusty's NSData gzip category which will give you compression/decompression methods.
While this is available by using the libz.dylib, it really is unnecessary as it save you little (if any) space. You application bundle is already compressed when being transferred to the phone. Compression on top of compression usually yields little additional compression.
Try it out yourself. You may find that shipping your app with unzipped contents may take up just as much space as zipped contents.