advertisement

Swift Basics

75 %
25 %
advertisement
Information about Swift Basics
Education

Published on October 6, 2014

Author: WeAreDIT

Source: slideshare.net

Description

2014 부산지역 SW개발자 컨퍼런스에서 발표한 애플의 새로운 프로그래밍 언어 Swif 기초
advertisement

1. SWIFT BASICS 동의과학대학교 컴퓨터정보계열 김 종 현 jhkim@dit.ac.kr www.facebook.com/jhkim3217 2014. 7. 19

2. Reference • Swift Guide, 2014 AppCode.com • Swift Tutorial: A Quick Start, Ray Wenderlich !

3. 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

4. PLAYGROUND

5. 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!”; !

6. 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”

7. No Semicolons var myMessage = “No semicolon is needed” ! !

8. 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

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

10. • 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 !

11. 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 !

12. // 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) ! !

13. 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”]; !

14. // 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”

15. 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()

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

17. Obj-C subclass, protocol Integration // Objective-C @interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> ! ! // Swift class SimpleTableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource ! !

18. 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];

19. // 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) ! ! !

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

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

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

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

24. // 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!”) }

25. 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)”) !

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

27. // 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)”)

28. Enjoy Swift!

Add a comment

Related presentations

Related pages

Swift Basics Course - Treehouse

Swift is a new programming language created by Apple to program iOS apps. If you are new to programming or to Swift then this course is for you. Learn ...
Read more

The Swift Programming Language: The Basics

Hier sollte eine Beschreibung angezeigt werden, diese Seite lässt dies jedoch nicht zu.
Read more

Swift basics - Swift Tutorial

Swift Tutorial for beginners - Learn Swift programming on iOS in simple and easy steps starting from its overview, basic syntax, environment setup, data ...
Read more

Swift Basic | Free WordPress theme

Swift Basic is the free version of Swift. The fastest loading, developer friendly and most advanced SEO WordPress theme with loads of intuitive ...
Read more

Learn Swift 2: The Basics | Lynda.com

Learn Swift 2. These tutorials take you through the basics of Swift, the fast, interactive codebase for programming iOS, Mac OS X, tvOS, and even Watch OS ...
Read more

1. SWIFT BASICS+MT103 - dbds.de

SWIFT BASICS+MT103 SWIFT BASICS+MT103: ... der eine generelle Einführung in SWIFT benötigt – insbesondere für Mitarbeiter aus dem Zahlungsverkehr,
Read more

Swift Basics - Gumroad

Learn the basics of Swift through our free email course. Download the first tutorial to get started, every few days a new tutorial will be sent to your email.
Read more

1. SWIFT BASICS+MT103 - dbds Consulting

SWIFT BASICS+MT103 SWIFT BASICS+MT103: place: language: Date: duration: price: * * * 3 days 850 EUR* The ideal preparation course for users who plan ...
Read more

Swift Basics · codepath/ios_guides Wiki · GitHub

Introduction. This is quick guide to Swift Basics. Anything that can be built with Objective-C can be built with these core parts of the language.
Read more

Learn Swift Programming on Treehouse

Learn Swift Basics Next, let's put the fun in Swift Functions. A function is a fundamental building block of a computer program and allows us to write ...
Read more