Saving Data in iOS - Part 8: Section 1: Copying Image | Ray Wenderlich

Images contain a whole lot of data. Let's practice saving it to locations that are more suitable for your app's needs.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4307-saving-data-in-ios/lessons/8

I’m a bit confused here. I understand what a Bundle is on my computer. Went I load my app, is there a Bundle there too? And where is it stored? I mean, is it not stored just like my string and data files in the user’s document directory so that I can look for it in the same way I looked for strings and data?

I’m trying to get what you want me to learn here. I think you want me to extract the files from the bundle and store them in the user directory. Presumably if I have them in the Bundle I don’t need to do this but it was your way of getting me some files to play with. In a real world situation I might obtain images from the web and store them as you describe. Have I understood correctly?

Or as you say in the post above, would you extract files that were bundled too as being “more suitable” in spite of using memory twice.

Many thanks for a great course.

Went I load my app, is there a Bundle there too? And where is it stored? I mean, is it not stored just like my string and data files in the user’s document directory so that I can look for it in the same way I looked for strings and data?
Interaction with Bundles is more like how you interact with a FileManager, to find directories.

I’m trying to get what you want me to learn here. I think you want me to extract the files from the bundle and store them in the user directory. Presumably if I have them in the Bundle I don’t need to do this but it was your way of getting me some files to play with. In a real world situation I might obtain images from the web and store them as you describe. Have I understood correctly?

I think you have. We wanted to avoid the complexity of common real-world use, for this course. This was the simplest way we thought of. An update to the URLSession course is coming this week; this course and that one work well together, when it comes to JSON-representable types in particular. Hopefully the combination of the two will help you out!

In video 8 when you copy the image files to the document directory you casually mention that the whole file manager method throws and add it to the declaration, but never say why this is necessary here, and why not for any other previous operation. Can you clarify please.

I believe you’re asking about what we did starting at 4:25. Is that correct?

If so, it’s because we introduced a try into the method body, when using the Data initializer, which itself is marked as throws. If that initializer were to throw an error, the copyPNGSubdirectoriesToDocumentDirectory method would stop execution at that point and also throw the error. This is called “error propagation”.

Let me know if you need any further clarification!

Hi and thank you for this tutorial.

I’dd like to know what are the 3 dots after String in the method:

static func copyPNGSubdirectoriesToDocumentDirectory(subdirectoryNames: String…)
???

It’s the first time I see this.

Thanks!

That’s known as a “variadic parameter”. Within the method body, subdirectoryNames will be an array, but you don’t pass it as an array. The notable differences are that you leave off the angle brackets, and you can only use one variadic parameter per-function. I tend to use variadic parameters when I know that I’ll only be creating an array in-place, for an argument. And then, if I ever decide to pass a preexisting array in, I’ll change the method signature to use an array, instead.

More info here; search the page for the “Variadic Parameters” section. In the future, we’ll be more diligent about covering this earlier in our video curriculum, so it should be clearer!

Thanks! It’s very clear.

I have another one. Can you tel me how does the urls are ordered in the subdirectory? I mean why is the space.png at sub[1] and not at [0] or [2]? Is it because it was the second to be registered? I don’t know if I’m clear.

let spaceSceneURL = Bundle.main.urls(forResourcesWithExtension: “png”, subdirectory: “Scenes”)![1]

Thanks again.

I think you have the right idea. The URLs are returned in the same (alphabetical) order you can see them in, in the Project navigator or Finder. Note how Space.png is the second file in the Scenes folder:


Section 8 Part 1 (minor update)
We needed to handle the throw exception by wrapping the data.write operation in a do catch block when copying the png files to their respective folders.


Section 8 Part 1 (minor update)
Added copyPNGSubdirectoriesToDocumentDirectory extension to FileManager.swift

Looks like you chose not to go with the throws we added starting at 4:28?