Introduction

Introduction

Details

This slide deck is based on Dr. Si Chen’s slides

Planning
  • Attendance is critical.
  • Weekly planning:
    • Swift Fundamental (usually on Thursday)
      • Basic syntax
      • Some new concepts
    • App Design and Development (usually on Tuesday)
      • SDK
      • Building specific features for iOS apps
      • Mini-project, in-class labs

Getting Started with App Development

History of Swift
  • At the Apple Worldwide Developers Conference 2014, Apple introduced Swift as a modern language for writing apps for iOS and macOS. Apple now has new platforms, including watchOS and tvOS, that also use Swift as the primary programming language.
  • Since the 1990s, most developers have written applications for Apple platforms in Objective-C, a language built on top of the C programming language. Objective-C is more than 30 years old, and C is more than 40 years old.
  • As you learn Swift, you may see the influence of its C and Objective-C heritage.

  • In 2020, the percentage of Swift was 1.88%.
Pros and Cons of Swift
Pros
  • Advanced functionality (frequent updates and commitment from Apple)
  • High speed of development
  • Cross-platform development
  • Open source
Cons
  • Constant changes (frequent updates)
  • No support for earlier versions
  • Need bridging to import C++ libraries into Swift
Apps built using Swift
Swift language features
  • Multi-paradigm language
    • Support object-oriented programming
    • Include functional programming features such as high-order function, functions as values, nested functions, closures, anonymous functions, etc.

Coding in Swift

Hello World
  • Swift code is written in plain text files with a .swift file extension. Each line in the file represents a statement, and a program is made up of one or more statements. These are the instructions you wish your app to run.
  • In Swift, the default file is called main.swift. Any Swift code included in the main.swift file will be executed from top to bottom.
Terminal and Swift REPL
  • macOS comes with a console app called Terminal, and Swift comes with a tool called a REPL, which stands for Read, Eval, Print Loop.
  • The REPL allows you to enter simple commands, evaluate them, and print the result.
  • Use the Swift REPL in the console to write your first “Hello, world!” program. – Open the Terminal application on your Mac. You can search Terminal in Spotlight or find the application in the system Applications/Utilities folder. – Enter the Swift REPL by typing swift repl and pressing Return. – Type the command print(”Hello, world!”) and press Return to execute it.
License
  • If you are using your personal computer, the first time that you run swift, you will be asked to accept the Xcode license.
    • Enter your account password (should be administrative account)
    • Type agree and press Enter
1
sudo xcodebuild -license
1
swift repl
User Interface design in Swift
Model-View-Controller

Coding Swift: Xcode IDE

Getting started
First project
  • Select Create a new Xcode project
Create a new Xcode project
  • Select iOS
  • Select App
  • Click Next
Choose the following options for your project
  • Product name: hello
  • Organization identifier: wcupa
  • Keep everything else as default
  • Click Next
  • Follow the instructions to identify the location of a directory where the new project directory will be placed.
  • Once everything is done, the Swift IDE will be displayed, with a default template project called hello generated.
Swift IDE
  • To run this project, clock on the Play icon on the tool bar.
    • A iPhone simulator will be display, with the hello app shown on the home screen.
    • Double-click on the app to launch.
Run the project
Default home screen
App launched

“Modify/build your first app”

App Icon
  • Download the AppIcons.zip file and double click to unzip
  • Inside the unzipped directory, navigate to Assets.xcassets/AppIcon.appiconset,
  • In the Xcode IDE navigator, go to Assets, select AppIcon.
  • In the Inspector area, select Single Size for iOS.
    • Start drag and drop the .png files into the boxes according to the dimension/file name.
    • The unzipped directory contains file sizes for others.
AppIcon
Local storage

The icon pictures will show up inside your app’s local directory.

New Icon!

Rerun the simulator and observe the new icon

Buttons and Images
Clickable button
  • Modify the ContentView: View struct code as follows.
ContentView: View

swift linenums="1" --8<-- "docs/csc496/lectures/codes/01-intro/button.swift"

  • Simulate the code and test out the clickable button.
More details …
  • Download the pokemon.zip and unzip
  • Drag the first four image files (1 through 4) into Assets
More images in Assets
  • Modify the ContentView: View struct code as follows to
    • Add a typeable text file that takes a user-input pokemon ID
    • Use this value to display a pokemon.
ContentView: View
1
--8<-- "docs/csc496/lectures/codes/01-intro/images.swift"
  • Simulate the code and test out the clickable button.