DEV Community

Cover image for Starting To Code [Part 3]
Juergen
Juergen

Posted on • Updated on

Starting To Code [Part 3]

Finally, after describing my struggle with starting in the last post, it is time. Starting to code the application to solve my problem and learn a new language.

Project setup

Starting a project in Xcode is easy, you open the IDE, start a new project and you are ready to go.

I always start by adding a git-repository and link it to my own remote repository or my GitHub account, depending on the character of the project. Having source control from the start is so valuable. You can revert to a working copy or create a branch to test something out. Even if you only use a local git. It is worth it.

Finding A Name

Finding a name can be tricky, but for this project I just took the obvious. The application has to move files from one place to the other. It stores them in a different place. So StoreAway it is.

Coding

Where to start coding is always a big questions. As an embedded developer I tend to start at the backend. Implementing the file handling looked like a good first task for myself. To make life a little bit easier I setup my SwiftUI user interface to help me with the implementation.

First UserInterface Implementation

Yes, it is only a button which allows me to call a function. Start simple. It helps you a lot to call a function without restarting your application. It makes debugging so much easier.

First problems

My first goal was to get all files of one specific filetype in a folder. To achieve this goal I used the FileManager. This interface allows you to access the filesystem and do all the operations you know from finder.

But there was a problem. I could not access the filesystem. Projects in Xcode default to sandboxed environment. Every macOS user knows these dialogs from apps like Alfred that want you to enable full disk access for them. Do I really want that in an application for myself?

Not at the beginning. Don't let a problem like this stop you for a small project.
Do you need to implement sandboxing? Absolutely if you want to sell the application later.

I don't plan to sell it, I just want to learn the language. So I removed the entitlements file and started coding my file handler.

Syntax

One of the goals for this project was to use a swift-like syntax and not falling back to my C++ roots.

func getFilesInFolder( path: URL, filetype: String) -> [URL] {
    let enumerator = fm.enumerator(atPath: path.path)
    let files = (enumerator?.allObjects as! [String]).filter{$0.lowercased().contains(filetype.lowercased())}

    var file_url : [URL] = []
    for f in files{
      let path = URL(fileURLWithPath: path.path).appendingPathComponent(f)
      file_url.append(path)
    }
     return file_url
   }
Enter fullscreen mode Exit fullscreen mode

After looking at my code I wasn't sure, that my implementation is very Swift like. After another round of refactoring the result is as follows

func getFilesInFolder( path: URL, filetype: String) -> [URL] {

    let resourceKeys = Set<URLResourceKey>([.nameKey])
    let enumerator = fm.enumerator(at: path, includingPropertiesForKeys: Array(resourceKeys), options: .skipsHiddenFiles)
    let files = (enumerator?.allObjects as! [URL]).filter{$0.pathExtension.contains(filetype.lowercased())}

    return files

  }
Enter fullscreen mode Exit fullscreen mode

Refactoring is important. But if works - it works! If you are coding, especially as a beginner don't think too much about the form of your code. The more you use a language the better your style will be.

The first versions of the application are already done, getting more features over time. I want to continue this as a small series. Publishing more stories in the next weeks on my journey to learn a new programming language and solve my problem.

Image by StockSnap from Pixabay - Thank you!

Top comments (0)