iOS Development Tutorial: Creating a Demo Project, Implementing Views, Buttons, and Images with Swift and Comparing to Objective‑C
This tutorial walks through setting up an iOS demo project in Xcode, creating a yellow rectangle view, adding a clickable button and a centered image using Swift code, and then compares the Swift syntax with equivalent Objective‑C snippets, while also covering useful comment tags.
The article introduces a series of iOS development tutorials that cover the entire workflow from tool usage to app store release, beginning with a brief statement of the learning goal.
Requirements: display an orange‑yellow rectangle, add a clickable button with an event handler, and show an image at the center of the screen.
System versions: macOS 10.15.5, Xcode 11.5.
1. Create Demo Project
1.1 Choose Xcode Project
Start a new Xcode project and select Single View App .
1.2 Select Single View App
Proceed with the default settings to generate the project skeleton.
2. Code Implementation
2.1 Application Entry Point
The @UIApplication annotation in AppDelegate.swift marks the class as the app’s entry point.
2.2 Code Structure
All methods are placed inside a pair of braces with indentation, similar to Java.
2.3 Feature Implementation
2.3.1 Create a View and Add a Yellow Rectangle
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 1. Create a view
let v = UIView(frame: CGRect(x:0, y:0, width:100, height:100))
// 2. Set background color
v.backgroundColor = UIColor.yellow
// 3. Add to current view
view.addSubview(v)
}
}The result is a yellow rectangle positioned at the top‑left corner.
2.3.2 Add a Button with Click Event
// 2. Create a button
let btn = UIButton(type: .contactAdd)
// 2.1 Add to current view
v.addSubview(btn)
// 2.2 Add click event
btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
// 2.3 btnClick method logs outputA button appears at the top‑left; tapping it prints a log message.
2.3.3 Add an Image
// 3.1 Add an image view
let iv = UIImageView(image: #imageLiteral(resourceName: "2.jpeg"))
// 3.2 Center the image
iv.center = view.centerThe image is displayed in the center of the screen.
3. Swift vs. Objective‑C Syntax Comparison
3.1 Creating a View
Swift: UIView(XXX:)
OC: [[UIView alloc] initWithXXX:]3.2 Class Method
Swift: UIColor.yellow
OC: [UIColor yellow]3.3 Accessing Self
It is recommended to omit self. unless required by the compiler, such as inside closures.
3.4 Semicolon Usage
Semicolons are unnecessary unless multiple statements share a line.
3.5 Enum Types
Swift: type: .contactAdd
OC: UIButtonType.ContactAdd3.6 Selector Syntax
Swift uses #selector (no colon for parameter‑less methods); Objective‑C uses @selector .
3.7 Debugging
Swift: print() (fast, no timestamp, use #function for method name). Objective‑C: NSLog (use __FUNCTION__ for method name).
4. Effective Use of Comments
Special comment tags like TODO , MARK , and FIXME create anchor points in the navigation bar, allowing quick jumps to annotated sections.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.