SWIFT BASICS jhkim@dit.ac.kr www.facebook.com/jhkim3217 2014. 7. 19
Reference Swift Guide, 2014 AppCode.com Swift Tutorial: A Quick Start, Ray Wenderlich
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
PLAYGROUND
Variable, Constants, var let Type Inference 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 ;
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
var mymessage = No semicolon is needed
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
// Objective-C NSString *firstmessage = @ Swift is awesome. ; NSString *secondmessage = @ What do you think? NSString *message = [NSString stringwithformat:@ %@%@, firstmessage, secondmessage]; NSLog(@ %@, message);
var string1 = Hello var string2 = Hello if string1 == string2 { println( Both are the same ) else { println( Both are different )? // Obj-C isequaltostring: method
// 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
// 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)
// 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 ];
// 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
// 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()
// access or change the property variable recipeitem.name = recipeitem.duration = 30 recipeitem.ingredients = [ olive oil, salt, onion ]
// Objective-C @interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> // Swift class SimpleTableViewController : UIViewController, UITableViewDelegate, UITableViewDataSource
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];
// 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)
for loops for i in 0..5 { println( index = \(i) ) for var i=0; i<5; i++ { printf( index = \(i) )
for i in 0 5 { println( index = \(i) ) for var i=0; i<=5; i++ { printf( index = \(i) )
if-else var bookprice = 1000 if bookprice >= 999 { println( Hey, the book is expensive ) else { println( Okey, I can buy it )
switch // break swicth recipename { case : println( ) case : println( ) case : println( ) default: println( )
// 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 )
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) )
// dot notation let product = (id: AP234, name: iphone6, price:599) println( id = \(product.id) ) println( name = \(product.name) ) println( price = USD\(product.price) )
// 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) )
Enjoy Swift