Working With Files
Part of the Object Oriented Programming section of Coddy's Swift journey. Lesson 1 of 57.
Real programs are split into several files. In Swift every .swift file of an app is compiled together into one module, so a function or type declared in one file can be used in any other file without an import:
// Greeting.swift
func greeting(for name: String) -> String {
"Hello, \(name)!"
}
// main.swift
print(greeting(for: "Ada"))Running this program prints Hello, Ada!: main.swift calls the function that lives in Greeting.swift.
Only one file may contain top-level statements, code that runs on its own outside any function or type. That file is main.swift, and it is where the program starts. The other files hold declarations only:
// Greeting.swift
func greeting(for name: String) -> String { "Hello, \(name)!" }
// print("hi") // error: statements are not allowed at the top level here
// main.swift
let name = readLine() ?? "friend" // top-level code is fine in main.swift
print(greeting(for: name))The usual layout is one type per file, named after the type: Dog.swift holds Dog, Account.swift holds Account. In this section's challenges, your code goes in those files and main.swift holds the supplied input/output code, which you usually cannot edit. The editor shows each file as a tab.
Dog.swift // class Dog { ... }
Account.swift // struct Account { ... }
main.swift // reads the input and prints the resultsAccess control follows files too. private and fileprivate hide a declaration from other files, while the default, internal, makes it visible in every file of the module:
// Secret.swift
fileprivate let salt = "x7" // only this file can see it
func hashed(_ s: String) -> String { s + salt }
// main.swift
print(hashed("pw")) // fine: hashed is internal
// print(salt) // error: salt is fileprivateChallenge
EasyThe program has two files. main.swift reads a name and prints the result of greeting(for:). Complete greeting(for:) in Greeting.swift so it returns Hello, Ada! for the name Ada, or Hello, friend! when the name is empty.
Your code goes in Greeting.swift. main.swift holds the supplied input/output code and cannot be edited.
Try it yourself
// Supplied input/output code: keep it as it is
var input: [String] = []
while let line = readLine() { input.append(line) }
for name in input { print(greeting(for: name)) }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Classes and Objects
Working With FilesDefining ClassesMethods and selfInitializersClasses Are ReferencesRecap - Library Card4Encapsulation
Access ControlRead-Only From OutsideGuarding StateFailable InitializersRecap - Bank Account7Polymorphism and Generics
PolymorphismType CastingGeneric FunctionsGeneric TypesRecap - Shape Calculator5Inheritance
SubclassesOverriding MethodsInitializers and superfinal and HierarchiesRecap - Employee Hierarchy11Project: Library Management
Books and MembersBorrowing BooksPractice on your own: Swift playground