© Yanis Zafirópulos 2019
Yanis ZafirópulosSwift 4 Recipeshttps://doi.org/10.1007/978-1-4842-4182-0_12

12. Web, Files, and System

Yanis Zafirópulos1 
Granada, Spain

There is virtually no solid application that doesn’t need some level of file access and/or web access nowadays.

In this chapter, we’ll be looking at what Swift offers us and how we can easily play with files, read/write to them, retrieve useful information from our user’s system, and even take it one step further and interact with the WWW.

12.1 Check if file exists


I want to check if a specific file exists.


import Foundation
Let’s set our file path.
let path = "tests/hello.swift"
And check if it exists.
if FileManager.default.fileExists(atPath: path) {
      print("Yes, the ...

Get Swift 4 Recipes: Hundreds of Useful Hand-picked Code Snippets now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.