[Swift 1] Day 15 - Code Exercise Solution


#1

Post your solution code, related links, images, or videos for the Day 15 Code Exercise and Notebook Activity.


#2

Hey Paul,

I don’t understand whether everything should be saved in a .cvs file, or the text and the image should be saved in a .txt and .jpg file respectively


#3

Still figuring out how to do tap gesture. Thanks paul!


#4

Question: I am trying to use a CSV to store an array. I figured how to delimit by comma on saving, but can’t do the opposite while loading.

So I have a CSV file that contains an array, something like: asd,asdll,akjsdk

let text = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: &error)

but when I parse the CSV to a String var and then append it to an array, instead of getting
3 items I get 1 item that is “asd,asdll,akjsdk”

Ideas ?


#5

Ok, lets break the silence :smile:

I think I have to submit my app as I keep adding this and that and it might become too complex for myself or anyone else that might wish to use the code :smile:

Here is a short video of the app ( Task Troll ) :

Here you can find more info about it, including all the code.


#6

Save the .txt (.csv) and .jpg files separately using the code that is demonstrated from the lessons.


#7

Add a tap gesture to the UIImageView and then make sure to make it userInteractionEnabled

imageView.userInteractionEnabled = true

#8
  1. Separate a string with the method componentsSeparatedByString method.
  2. You can separate objects on separate lines (using “\n” for newline … i.e. return key)
  3. You can separate objects by commas using “,”
  4. You may want to trim whitespace (spaces, tabs, etc)

Example:

    // Separate by a comma symbol ","
    var str = "blue, red, 322"
    var arrayOfStrings = str.componentsSeparatedByString(",")
    println("array: \(arrayOfStrings)")

    // There is going to be extra spaces if there are spaces between commas
    
    // trim whitespace like this example
    var textWithSpaces = " text with spaces before and after      "
    var trimmed = textWithSpaces.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

    println("textWithSpaces |\(textWithSpaces)|")
    println("trimmed |\(trimmed)|")
    
    // Output
    //array: [blue,  red,  322]
    //textWithSpaces | text with spaces before and after      |
    //trimmed |text with spaces before and after|

#9

Awesome work Vigo!

Great use of the UITableView to display multiple pieces of content. I’ll be getting into that soon.


#10

Thanks ! I ended up using CoreData, which turned to be a huge challenge :smiley:

Btw, have you noticed that on every run the iOS simulator creates a new folder ? So if you are using The LoadImageByPath thing, you won’t be able to find the images on the next rerun :smiley:


#11

Really? I don’t know about the folders… maybe something changed since I looked at it.

It seemed to be the same folder because I was able to manually modify files. I’ll have to double check again when I work on Day 16.

What resources did you use for Core Data?


#12

Definitely changes. I started printing the document path and found out, but it took me a few hours to figure it out. I was going crazy about why the files won’t load :smile:
About the Core Data I just followed some tutorials on YouTube :smiley:


#13

Btw, I found out other people raging about this new “feature” in Xcode 6 :slight_smile:


#14

Here is the appetize.io demo :smile:


#15

Ok, you’re right. The folder path does change each time.

Somehow when you open the folder in finder, it’ll auto switch to the new folder. I didn’t notice that it was changing paths, since all my files were still in the same Finder window. If you look in the screenshots, only the top level string changes between subsequent runs of the app (under the Documents path).

versus

You can open the folder quickly by going to Finder > Go > Go To Folder > paste in link from Xcode document path


#16

Hi Paul

Find the code here: https://gist.github.com/5748945a5a30dce6dd23.git

And here are some screenshots:



#17

Looks awesome! Did you use Core Graphics for the image mask?


#18

Hi Paul, I simulated the mask rounding the image layer corners. UIImage.layer.cornerRadius


#19

Great, I’ll be demoing the corner radius with the Day 17 materials for custom UIViews.


#20

Could you help as I want to have either a camera or the image picker but I can only have one and not both (I am doing this on an iOS device). So at the moment it always comes up with image picker unless I comment out the image picker code then the camera works.

  // MARK: Button for uiimage picker
  @IBAction func AddProductButton(sender: UIButton) {
    var imagePicker = UIImagePickerController() // method to get the image picker
    var sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum // camera
    println("Button uiimage picker")

    if UIImagePickerController.isSourceTypeAvailable(sourceType){
      imagePicker.sourceType = sourceType
      imagePicker.delegate = self
      
      // show a screen
      presentViewController(imagePicker, animated: true, completion: nil) // shows the image picker screen
      newMedia = false
    }
  }
  
  func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { // gets delegate method on images
    var image = info[UIImagePickerControllerOriginalImage] as UIImage
    imageView.image = image
    dismissViewControllerAnimated(true, completion: nil) // allows view to dismiss
  }
  
  func imagePickerControllerDidCancel(picker: UIImagePickerController) { // allows image cancel to go back to menu
    println("cancel")
    dismissViewControllerAnimated(true, completion: nil)
  }

  // MARK: Button for camera
  @IBAction func addCameraPhotoButton(sender: UIButton) {
    var  cameraPicker = UIImagePickerController() // method to get the image picker
    var sourceTypeCamera = UIImagePickerControllerSourceType.Camera// camera
    println("Button camera")
    
    if UIImagePickerController.isSourceTypeAvailable(sourceTypeCamera){
        cameraPicker.sourceType = sourceTypeCamera
        cameraPicker.delegate = self
        
      // show a screen
      presentViewController(cameraPicker, animated: true, completion: nil) // shows the image picker screen
      newMedia = true
        }
    }
    
    func cameraPickerController(cameraPicker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { // gets delegate method on images
        var image = info[UIImagePickerControllerOriginalImage] as UIImage
        imageView.image = image
        dismissViewControllerAnimated(true, completion: nil) // allows view to dismiss
    }
    
    func cameraPickerControllerDidCancel(cameraPicker: UIImagePickerController) { // allows image cancel to go back to menu
        println("cancel")
        dismissViewControllerAnimated(true, completion: nil)