29
SWIFT BASICS 동의과학대학교 컴퓨터정보계열 [email protected] www.facebook.com/jhkim3217 2014. 7. 19

SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

SWIFT BASICS동의과학대학교 컴퓨터정보계열

김 종 현 [email protected]

www.facebook.com/jhkim3217 2014. 7. 19

Page 2: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Reference

• Swift Guide, 2014 AppCode.com

• Swift Tutorial: A Quick Start, Ray Wenderlich

!

Page 3: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

background• new programming language for iOS, OS X Apps

• Ruby, Python, Go… v.s. Objective-C

• Fast, Modern, Safe, Interactive

• Shorter, Cleaner, Easier to read

• 문법이 웹 개발자에게 친근(특히 Javascript 경험자)

• user friendly to new programmers

Page 4: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

PLAYGROUND

Page 5: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Variable, Constants, Type Inference

• var : 변수 • let : 상수 ! var numberOfRow = 30 let maxNumberOfRows = 100 var 😄🐸🎅😂 = “Have fun!” // emoji character ! const int count = 10; // 항상 명시적으로 형 지정 double price = 23.55; NSString *myMessage = @“Obj-C is not dead yet!”;

!

Page 6: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

let count = 10 // count is inferred to be type Int !var price = 23.55 // price is inferred to be type Double !var myMessage = “Swift is the future” // myMessage is inferred to be type String !var myMessage : String = “Swift is the future”

Page 7: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

No Semicolons

var myMessage = “No semicolon is needed”

!

!

Page 8: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Basic String Manipulation• String type is fully Unicode-compliant ! // immutable String

let dontModifyMe = “You can’t modify this string”

! // mutable String

var modifyMe = “You can modify this string” !!• String manipulation !

let firstMessage = “Swift is awesome.”

let secondMessage = “What do you think?”

var message = firstMessage + secondMessage

Page 9: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// Objective-C NSString *firstMessage = @“Swift is awesome.”; NSString *secondMessage = @“What do you think?” NSString *message = [NSString stringWithFormat:@“%@%@“, firstMessage, secondMessage]; NSLog(@“%@“, message); !!!

Page 10: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

• String Comparison ! var string1 = “Hello” var string2 = “Hello” if string1 == string2 { println(“Both are the same”) } else { println(”Both are different”) } ! 결과 값은? ! // Obj-C isEqualToString: method !

Page 11: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Array!// Objective-C, store any type of objects NSArray *recipes = @[@“짜장면”, @“짬뽕”, @“탕수육”, @“군만두”, @“라면”]; !// Swift, store items of the same type var recipes = ["짜장면", "짬뽕", "탕수육", "군만두", "라면", “우동”] !var recipes : String[] = ["짜장면", "짬뽕", “탕수육”, "군만두", "라면"] !// recipes.count will return 5 var numberOfItems = recipes.count !

Page 12: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// add items recipes += “깐풍기” !// add multiple items recipes += [“깐풍기”, “라조기”, “김밥 “] !// access or change a item in an array var recipeItem = recipes[0] recipes[1] = “떡뽁기” !// change a range of values recipes[1…3] = [“우동”, “오향장육”, “팔보채”] !println(recipes) !!

Page 13: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Dictionary// Objective-C NSDictionary *companies = @{@“APPL” : @”Apple”, @“GOOG” : @“Google”, @“FB” : @“Facebook”}; !// Swift var companies = [“APPL” : “Apple”, “GOOG” : “Google”, “FB” : “Facebook”]; !var companies: Dictionary<String, String> = [“APPL” : “Apple”, “GOOG” : “Google”, “FB” : “Facebook”]; !

Page 14: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// Iteration for (stockCode, name) in companies { println(“\(stockCode) = \(name)” } !for stockCode in companies.keys { println(“Stock code = \(stockCode)”) } !for name in companies.values { println(“Companies name = \(name)”) } !// add a new key-value pair to Dictionary companies[“TWTR”] = “Twitter”

Page 15: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,
Page 16: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Classes// define a class class Recipe { var name: String = “” var duration: Int = 10 var ingredients: String[] = [“egg”] } !// optional : ? class Recipe { var name:String? // assign default value of nil var duration: Int = 10 var ingredients: String[]? !// create a instance var recipeItem = Recipe()

Page 17: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// access or change the property variable recipeItem.name = “짜장면” recipeItem.duration = 30 recipeItem.ingredients = [“olive oil”, “salt”, “onion”] !!!!!!

Page 18: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Obj-C subclass, protocol Integration

// Objective-C @interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> !!// Swift class SimpleTableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource !!

Page 19: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Methods• define methods in class, structure or enumeration • func keyword ! class TodoManager { func printWlecomeMessage() { println(“Welcome to My ToDo List”) } } ! // Swift method call toDoManager.printWelcomeMessage() ! // Objective-C [toDoManager printWelcomeMessage];

Page 20: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// method arguments, return value class ToDoManager { func printWelcomeMessage(name:String) -> Int { println(“Welcome to \(name)’s toDo List”) ! return 10 } } !var toDoManager = TodoManager() let numberOfTodoItem = todoManager.printWelcomeMessage(“Superman) println(numberOfTodoItem) ! !!

Page 21: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Control Flow• for loops ! // .. for i in 0..5 { println(“index = \(i)”) } ! for var i=0; i<5; i++ { printf(“index = \(i)”) }

Page 22: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// … for i in 0…5 { println(“index = \(i)”) } ! for var i=0; i<=5; i++ { printf(“index = \(i)”) } !

Page 23: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

• if-else ! var bookPrice = 1000 if bookPrice >= 999 { println(“Hey, the book is expensive”) } else { println(“Okey, I can buy it”) } !!

Page 24: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

• switch ! // break 문이 없음 swicth recipeName { case “짜장면”: println(“나의 주식!”) case “떡뽁기”: println(“나의 간사!”) case “김밥”: println(“제일 좋아하는 것!”) default: println(“모두 좋아해!”) } !

Page 25: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// range matching(.., …) var speed = 50 switch speed { case 0: println(“stop”) case 0…40: println(“slow”) case 41…70: println(“normal”) case 71..101 println(“fast”) default: println(“not classified yet!”) }

Page 26: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Tuples• multiple values as a single compound value • any value of any type in the tuple !! // create tuple let company = (“AAPL”, “Apple”, 93.5) ! // decomposing let (stockCode, companyName, stockPrice) = company println(“stock code = \(stockCode)”) println(“company name = \(companyName)”) println(“stock price = \(stockPrice)”) !

Page 27: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// dot notation let product = (id:”AP234”, name:”iPhone6”, price:599) !println(“id = \(product.id)”) println(“name = \(product.name)”) println(“price = USD\(product.price)”) !!!!!!

Page 28: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

// return multiple values in a method class Store { func getProduct(number: Int)->(id: String, name: String, price: Int) { var id = “IP435”, name = “iMac”, price = 1399 switch number { case 1: id = “AP234” name = “iPhone 6” price = 599 case 2: id = “PE645” name = “iPad Air” price = 499 default: break } return(id, name, price) } } ! //call a method let store = Store() let product = store.getProduct(2) println(“id = \(product.id)”) println(“name = \(product.name)”) println(“price = USD\(product.price)”)

Page 29: SWIFT BASICS - ditapps.files.wordpress.com · • new programming language for iOS, OS X Apps • Ruby, Python, Go… v.s. Objective-C • Fast, Modern, Safe, Interactive • Shorter,

Enjoy Swift!