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

**URL:** <https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404>\
**Category:** 🎓 Courses & Articles\
**Created:** [October 10, 2017, 6:35pm UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404 "2017-10-10T18:35:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jcatterwaul/32/94210_2.png) [@jcatterwaul](https://forums.kodeco.com/u/jcatterwaul)\
**Post date:** [October 10, 2017, 6:35pm UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/1 "2017-10-10T18:35:16Z")

</div>

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](https://www.raywenderlich.com/4307-saving-data-in-ios/lessons/8)

---

<div class="post-metadata">

**Author:** ![tchelyzt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tchelyzt/32/111572_2.png) [@tchelyzt](https://forums.kodeco.com/u/tchelyzt)\
**Post date:** [November 16, 2017, 1:26pm UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/2 "2017-11-16T13:26:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [November 18, 2017, 12:38am UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/3 "2017-11-18T00:38:58Z")

</div>

> 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](https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW1) 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!

---

<div class="post-metadata">

**Author:** ![flanker](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/flanker/32/109129_2.png) [@flanker](https://forums.kodeco.com/u/flanker)\
**Post date:** [December 7, 2017, 10:48am UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/4 "2017-12-07T10:48:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [December 7, 2017, 3:13pm UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/6 "2017-12-07T15:13:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jfl111](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jfl111/32/81494_2.png) [@jfl111](https://forums.kodeco.com/u/jfl111)\
**Post date:** [December 15, 2017, 7:04pm UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/7 "2017-12-15T19:04:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [December 15, 2017, 8:49pm UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/9 "2017-12-15T20:49:36Z")

</div>

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](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html); 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!

---

<div class="post-metadata">

**Author:** ![jfl111](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jfl111/32/81494_2.png) [@jfl111](https://forums.kodeco.com/u/jfl111)\
**Post date:** [December 16, 2017, 2:09am UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/10 "2017-12-16T02:09:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [December 16, 2017, 5:03am UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/11 "2017-12-16T05:03:04Z")

</div>

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:

 ![09 PM](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/3/7/37bb01f76f19863d98b6fe31abd19fb37e4a2cd2.png)

---

<div class="post-metadata">

**Author:** ![georgeshafik](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/georgeshafik/32/102075_2.png) [@georgeshafik](https://forums.kodeco.com/u/georgeshafik)\
**Post date:** [January 28, 2018, 12:09am UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/13 "2018-01-28T00:09:41Z")

</div>

![image](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/f/3/f337f31eaa0f95b12bffdf6880f8cb8849990957.png)  
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.

---

<div class="post-metadata">

**Author:** ![georgeshafik](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/georgeshafik/32/102075_2.png) [@georgeshafik](https://forums.kodeco.com/u/georgeshafik)\
**Post date:** [January 28, 2018, 12:13am UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/14 "2018-01-28T00:13:23Z")

</div>

![image](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/c/7/c7ae3867cfeaece6f59a42443bd5ae675cc8af1e.png)  
Section 8 Part 1 (minor update)  
Added copyPNGSubdirectoriesToDocumentDirectory extension to FileManager.swift

---

<div class="post-metadata">

**Author:** ![jessycatterwaul](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessycatterwaul/32/110937_2.png) [@jessycatterwaul](https://forums.kodeco.com/u/jessycatterwaul)\
**Post date:** [January 28, 2018, 12:29am UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/15 "2018-01-28T00:29:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 25, 2018, 10:02pm UTC](https://forums.kodeco.com/t/saving-data-in-ios-part-8-section-1-copying-image-ray-wenderlich/35404/17 "2018-07-25T22:02:16Z")

</div>

Opened on July 25, 2018, 10:02pm UTC


