SlideShare a Scribd company logo
1 of 27
Presenter: Neha Sinha, Mindfire Solutions
Date: 10/06/2015
Swift vs Objective C
Presenter: Neha Sinha, Mindfire Solutions
One of the goals of the new Swift programming language was
to be interoperable with the older Objective-C
What we will deal with in today’s seminar?
•Compare Language Syntax
•Compare Data Types
•Use Swift & Objective C in the same project.
Presenter: Neha Sinha, Mindfire Solutions
Why Care about This?
Many years of Objective-C code
Patterns & practices developed in Objective-C
Swift provides features Objective-C can’t.
Interoperability is not equal
Demand is greater for new Swift to existing Objective-C
Swift understands Objective-C better than Objective-C
understands Swift.
Swift provides features Objective-C doesn’t recognize.
5
Swift:
func getNameAndAge() -> (String, Int) {
return (“Neha”, 25)
}
Objective-C:
?
Tuples
Instantiating Objects
Swift:
var myDate = NSDate()
Objective-C:
NSDate* myDate = [NSDate alloc] init];
Using Initializers
Swift:
let myTableView: UITableView = UITableView(frame:
CGRectZero, style: Grouped)
Objective-C:
UITableView* myTableView = [[UITableView alloc]
initWithFrame:CGRectZero style:UITableViewStyleGrouped];
Mapping Factory Methods
Swift:
let color = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha:
1.0)
Objective-C:
UIColor* color = [UIColor colorWithRed:0.5 green:0.0
blue:0.5 alpha:1.0];
Factory methods & Objective-C classes are typically just
mapped to their equivalent normal Swift initializers.
Failable Initialization
Objective-C:
UIImage* myImage = [UIImage
imageWithContentsOfFile:@“”];
An Objective-C initializer can return ‘nil’.
Swift demands that variables & constants always exist in a
valid state.
Failable Initialization
Swift:
var image: UIImage? = UIImage(contentsOfFile: filePath)
if image != nil {
return image!
}
Calling Methods
Swift:
myTbView.layoutIfNeeded()
myTbView.insertSubView(mySubView, atIndex:2)
Objective-C:
[myTbView layoutIfNeeded];
[myTbView insertSubView:mySubView atIndex:0];
id & AnyObject Reference
Swift includes a protocol type named AnyObject that
represents any kind of object, just as id does in Objective-C.
var myObject: AnyObject = UITableViewCell()
myObject = NSDate()
let futureDate = myObject.dateByAddingTimeInterval(10)
let timeSinceNow = myObject.timeIntervalSinceNow
id & AnyObject Reference
myObject.characterAtIndex(5)
// crash, myObject doesn't respond to that method
let myChar = myObject.characterAtIndex?(5)
if let fifthCharacter = myObject.characterAtIndex?(5) {
println("Found (fifthCharacter) at index 5")
}
Swift & Objective C Strings
Swift automatically bridges between the String type and the
NSString class.
import Foundation
let greeting = "hello, world!"
let capitalizedGreeting = greeting.capitalizedString
import Foundation
let myString: NSString = "123"
if let integerValue = (myString as String).toInt() {
println("(myString) is the integer (integerValue)")
}
Arrays, Dictionaries & Numbers
Arrays: When you bridge from an NSArray object to a Swift
array, the resulting array is of type [AnyObject].
let swiftArray = foundationArray as [AnyObject]
if let downcastedSwiftArray = swiftArray as? [NSView] {
// downcastedSwiftArray contains only NSView objects
}
for aView in foundationArray as! [NSView] {
// aView is of type UIView
}
This cast is a forced cast, and results in a
runtime error if the cast does not succeed.
Arrays, Dictionaries & Numbers
Arrays: When you bridge from a Swift array to an NSArray
object, the elements in the Swift array must be AnyObject
compatible.
let schoolSupplies: NSArray = ["Pencil", "Eraser", "Notebook"]
// schoolSupplies is an NSArray object containing NSString
objects
Arrays, Dictionaries & Numbers
Dictionaries: When you bridge from an NSDictionary object to
a Swift dictionary, the resulting dictionary is of type
[NSObject: AnyObject].
The Swift compiler replaces the NSDictionary class with
[NSObject: AnyObject] when it imports Objective-C APIs.
When you cast in the reverse direction, from a Swift
dictionary to an NSDictionary object —the keys and values
must be instances of a class or bridgeable to an instance of a
class.
Arrays, Dictionaries & Numbers
Numbers: Swift automatically bridges certain native number
types, such as Int and Float, to NSNumber
All of the following types are automatically bridged to
NSNumber:
• Int
• UInt
• Float
• Double
• Bool
let n = 42
let m: NSNumber = n
Working with NSError in Swift
Error reporting in Swift follows the same pattern it does in
Objective-C, with the added benefit of offering optional
return values.
var writeError: NSError?
let written = myString.writeToFile(path, atomically: false,
encoding: NSUTF8StringEncoding,
error: &writeError)
if !written {
if let error = writeError {
println("write failure: (error.localizedDescription)")
}}
Swift & Objective C: Mix and Match
You can ‘Mix n Match’ Objective-C & Swift in the same
project.
• Bridging header file — Swift & Objective-C need to be
informed about other classes in the same project in a
different way.
• We use 2 bridging headers — One lets our Objective-C
classes know about Swift & the other vice-versa.
Inheriting between Languages
Swift:
class MySwiftViewController: UIViewController {
// define the class
}
Objective-C:
@class MySwiftClass;
@interface MyObjcClass : NSObject{
// define the class
}
Migrating from Objective C to Swift
The most effective approach for migrating code to Swift is on
a per-file basis, that is, one class at a time.
Because you can’t subclass Swift classes in Objective-C, it’s
best to choose a class in your app that doesn’t have any
subclasses. You’ll replace the .m and .h files for that class
with a single .swift file.
The Future Favors Swift
Swift: The more approachable, full-featured language
• Easier to maintain
• Is Considered Safer & Faster
• Is Unified with Memory Management
• Encourages interactive coding
Presenter: Neha Sinha, Mindfire Solutions
Questions?
References
https://developer.apple.com/library
http://www.infoworld.com/article/2920333/mobile-
development/swift-vs-objective-c-10-reasons-the-future-
favors-swift.html?page=2
http://mobileoop.com/the-comparison-between-swift-and-
objective-c-programming-language
Presenter: Neha Sinha, Mindfire Solutions
Thank You
www.mindfiresolutions.com
https://www.facebook.com/MindfireSolutions
http://www.linkedin.com/company/mindfire-solutions
http://twitter.com/mindfires

More Related Content

What's hot

Jetpack Compose.pptx
Jetpack Compose.pptxJetpack Compose.pptx
Jetpack Compose.pptxGDSCVJTI
 
Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI Ajinkya Saswade
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin XPeppers
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUIBongwon Lee
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation MobileAcademy
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java ProgrammingMath-Circle
 
Android Development with Kotlin course
Android Development  with Kotlin courseAndroid Development  with Kotlin course
Android Development with Kotlin courseGoogleDevelopersLeba
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutinesNAVER Engineering
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming LanguageAnıl Sözeri
 
Swift programming language
Swift programming languageSwift programming language
Swift programming languageNijo Job
 
Introduction to iOS Apps Development
Introduction to iOS Apps DevelopmentIntroduction to iOS Apps Development
Introduction to iOS Apps DevelopmentProf. Erwin Globio
 
Layouts in android
Layouts in androidLayouts in android
Layouts in androidDurai S
 
Kotlin Jetpack Tutorial
Kotlin Jetpack TutorialKotlin Jetpack Tutorial
Kotlin Jetpack TutorialSimplilearn
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective cSunny Shaikh
 
Deep dive into swift UI
Deep dive into swift UIDeep dive into swift UI
Deep dive into swift UIOsamaGamal26
 
java 8 new features
java 8 new features java 8 new features
java 8 new features Rohit Verma
 
Java Presentation
Java PresentationJava Presentation
Java Presentationaitrichtech
 

What's hot (20)

Jetpack Compose.pptx
Jetpack Compose.pptxJetpack Compose.pptx
Jetpack Compose.pptx
 
Kotlin
KotlinKotlin
Kotlin
 
Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI Android jetpack compose | Declarative UI
Android jetpack compose | Declarative UI
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
Swift in SwiftUI
Swift in SwiftUISwift in SwiftUI
Swift in SwiftUI
 
Kotlin presentation
Kotlin presentation Kotlin presentation
Kotlin presentation
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Android Development with Kotlin course
Android Development  with Kotlin courseAndroid Development  with Kotlin course
Android Development with Kotlin course
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 
Swift programming language
Swift programming languageSwift programming language
Swift programming language
 
Introduction to iOS Apps Development
Introduction to iOS Apps DevelopmentIntroduction to iOS Apps Development
Introduction to iOS Apps Development
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
 
Kotlin Jetpack Tutorial
Kotlin Jetpack TutorialKotlin Jetpack Tutorial
Kotlin Jetpack Tutorial
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
Deep dive into swift UI
Deep dive into swift UIDeep dive into swift UI
Deep dive into swift UI
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 

Viewers also liked

A Journey From Objective C to Swift - Chromeinfotech
A Journey From Objective C to Swift - ChromeinfotechA Journey From Objective C to Swift - Chromeinfotech
A Journey From Objective C to Swift - ChromeinfotechChromeInfo Technologies
 
Migration Objective-C to Swift
Migration Objective-C to SwiftMigration Objective-C to Swift
Migration Objective-C to SwiftNattapon Nimakul
 
iOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. SwiftiOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. SwiftAlex Cristea
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to SwiftGiordano Scalzo
 
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...Ahmed Ali
 
Thinking in swift ppt
Thinking in swift pptThinking in swift ppt
Thinking in swift pptKeith Moon
 
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03) iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03) Jonathan Engelsma
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.Icalia Labs
 
An Introduction into the design of business using business architecture
An Introduction into the design of business using business architectureAn Introduction into the design of business using business architecture
An Introduction into the design of business using business architectureCraig Martin
 

Viewers also liked (12)

A Journey From Objective C to Swift - Chromeinfotech
A Journey From Objective C to Swift - ChromeinfotechA Journey From Objective C to Swift - Chromeinfotech
A Journey From Objective C to Swift - Chromeinfotech
 
Migration Objective-C to Swift
Migration Objective-C to SwiftMigration Objective-C to Swift
Migration Objective-C to Swift
 
iOS (7) Workshop
iOS (7) WorkshopiOS (7) Workshop
iOS (7) Workshop
 
iOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. SwiftiOS NSAgora #3: Objective-C vs. Swift
iOS NSAgora #3: Objective-C vs. Swift
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
iOS Development using Swift: Enums, ARC, Delegation, Closures, Table View and...
 
Thinking in swift ppt
Thinking in swift pptThinking in swift ppt
Thinking in swift ppt
 
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03) iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
iOS Bootcamp: learning to create awesome apps on iOS using Swift (Lecture 03)
 
API Testing
API TestingAPI Testing
API Testing
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.
 
Api testing
Api testingApi testing
Api testing
 
An Introduction into the design of business using business architecture
An Introduction into the design of business using business architectureAn Introduction into the design of business using business architecture
An Introduction into the design of business using business architecture
 

Similar to Swift vs Objective-C

Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-CKazunobu Tasaka
 
iPhone Development Intro
iPhone Development IntroiPhone Development Intro
iPhone Development IntroLuis Azevedo
 
The Future of Node - @rvagg - NodeConf Christchurch 2015
The Future of Node - @rvagg - NodeConf Christchurch 2015The Future of Node - @rvagg - NodeConf Christchurch 2015
The Future of Node - @rvagg - NodeConf Christchurch 2015rvagg
 
Tech breakfast 18
Tech breakfast 18Tech breakfast 18
Tech breakfast 18James Leone
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptEPAM Systems
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxMalla Reddy University
 
Migrating from Objective-C to Swift
Migrating from Objective-C to SwiftMigrating from Objective-C to Swift
Migrating from Objective-C to SwiftDominique Stranz
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talkbradringel
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java DevelopersMuhammad Abdullah
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)Shoaib Ghachi
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
Android_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_EngineeringAndroid_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_EngineeringShivanshSeth6
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsSubhransu Behera
 

Similar to Swift vs Objective-C (20)

Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Start with swift
Start with swiftStart with swift
Start with swift
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
 
iPhone Development Intro
iPhone Development IntroiPhone Development Intro
iPhone Development Intro
 
The Future of Node - @rvagg - NodeConf Christchurch 2015
The Future of Node - @rvagg - NodeConf Christchurch 2015The Future of Node - @rvagg - NodeConf Christchurch 2015
The Future of Node - @rvagg - NodeConf Christchurch 2015
 
Swift for-rubyists
Swift for-rubyistsSwift for-rubyists
Swift for-rubyists
 
Tech breakfast 18
Tech breakfast 18Tech breakfast 18
Tech breakfast 18
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptxUnit 1 - TypeScript & Introduction to Angular CLI.pptx
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
 
Migrating from Objective-C to Swift
Migrating from Objective-C to SwiftMigrating from Objective-C to Swift
Migrating from Objective-C to Swift
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talk
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
Objective c
Objective cObjective c
Objective c
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
Android_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_EngineeringAndroid_Bootcamp_PPT_GDSC_ITS_Engineering
Android_Bootcamp_PPT_GDSC_ITS_Engineering
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 

More from Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 
Angular In Depth
Angular In DepthAngular In Depth
Angular In Depth
 

Recently uploaded

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 

Recently uploaded (20)

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 

Swift vs Objective-C

  • 1. Presenter: Neha Sinha, Mindfire Solutions Date: 10/06/2015 Swift vs Objective C
  • 2. Presenter: Neha Sinha, Mindfire Solutions One of the goals of the new Swift programming language was to be interoperable with the older Objective-C What we will deal with in today’s seminar? •Compare Language Syntax •Compare Data Types •Use Swift & Objective C in the same project.
  • 3. Presenter: Neha Sinha, Mindfire Solutions Why Care about This? Many years of Objective-C code Patterns & practices developed in Objective-C Swift provides features Objective-C can’t.
  • 4. Interoperability is not equal Demand is greater for new Swift to existing Objective-C Swift understands Objective-C better than Objective-C understands Swift. Swift provides features Objective-C doesn’t recognize.
  • 5. 5 Swift: func getNameAndAge() -> (String, Int) { return (“Neha”, 25) } Objective-C: ? Tuples
  • 6. Instantiating Objects Swift: var myDate = NSDate() Objective-C: NSDate* myDate = [NSDate alloc] init];
  • 7. Using Initializers Swift: let myTableView: UITableView = UITableView(frame: CGRectZero, style: Grouped) Objective-C: UITableView* myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  • 8. Mapping Factory Methods Swift: let color = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0) Objective-C: UIColor* color = [UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0]; Factory methods & Objective-C classes are typically just mapped to their equivalent normal Swift initializers.
  • 9. Failable Initialization Objective-C: UIImage* myImage = [UIImage imageWithContentsOfFile:@“”]; An Objective-C initializer can return ‘nil’. Swift demands that variables & constants always exist in a valid state.
  • 10. Failable Initialization Swift: var image: UIImage? = UIImage(contentsOfFile: filePath) if image != nil { return image! }
  • 12. id & AnyObject Reference Swift includes a protocol type named AnyObject that represents any kind of object, just as id does in Objective-C. var myObject: AnyObject = UITableViewCell() myObject = NSDate() let futureDate = myObject.dateByAddingTimeInterval(10) let timeSinceNow = myObject.timeIntervalSinceNow
  • 13. id & AnyObject Reference myObject.characterAtIndex(5) // crash, myObject doesn't respond to that method let myChar = myObject.characterAtIndex?(5) if let fifthCharacter = myObject.characterAtIndex?(5) { println("Found (fifthCharacter) at index 5") }
  • 14. Swift & Objective C Strings Swift automatically bridges between the String type and the NSString class. import Foundation let greeting = "hello, world!" let capitalizedGreeting = greeting.capitalizedString import Foundation let myString: NSString = "123" if let integerValue = (myString as String).toInt() { println("(myString) is the integer (integerValue)") }
  • 15. Arrays, Dictionaries & Numbers Arrays: When you bridge from an NSArray object to a Swift array, the resulting array is of type [AnyObject]. let swiftArray = foundationArray as [AnyObject] if let downcastedSwiftArray = swiftArray as? [NSView] { // downcastedSwiftArray contains only NSView objects } for aView in foundationArray as! [NSView] { // aView is of type UIView } This cast is a forced cast, and results in a runtime error if the cast does not succeed.
  • 16. Arrays, Dictionaries & Numbers Arrays: When you bridge from a Swift array to an NSArray object, the elements in the Swift array must be AnyObject compatible. let schoolSupplies: NSArray = ["Pencil", "Eraser", "Notebook"] // schoolSupplies is an NSArray object containing NSString objects
  • 17. Arrays, Dictionaries & Numbers Dictionaries: When you bridge from an NSDictionary object to a Swift dictionary, the resulting dictionary is of type [NSObject: AnyObject]. The Swift compiler replaces the NSDictionary class with [NSObject: AnyObject] when it imports Objective-C APIs. When you cast in the reverse direction, from a Swift dictionary to an NSDictionary object —the keys and values must be instances of a class or bridgeable to an instance of a class.
  • 18. Arrays, Dictionaries & Numbers Numbers: Swift automatically bridges certain native number types, such as Int and Float, to NSNumber All of the following types are automatically bridged to NSNumber: • Int • UInt • Float • Double • Bool let n = 42 let m: NSNumber = n
  • 19. Working with NSError in Swift Error reporting in Swift follows the same pattern it does in Objective-C, with the added benefit of offering optional return values. var writeError: NSError? let written = myString.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: &writeError) if !written { if let error = writeError { println("write failure: (error.localizedDescription)") }}
  • 20. Swift & Objective C: Mix and Match You can ‘Mix n Match’ Objective-C & Swift in the same project. • Bridging header file — Swift & Objective-C need to be informed about other classes in the same project in a different way. • We use 2 bridging headers — One lets our Objective-C classes know about Swift & the other vice-versa.
  • 21. Inheriting between Languages Swift: class MySwiftViewController: UIViewController { // define the class } Objective-C: @class MySwiftClass; @interface MyObjcClass : NSObject{ // define the class }
  • 22. Migrating from Objective C to Swift The most effective approach for migrating code to Swift is on a per-file basis, that is, one class at a time. Because you can’t subclass Swift classes in Objective-C, it’s best to choose a class in your app that doesn’t have any subclasses. You’ll replace the .m and .h files for that class with a single .swift file.
  • 23. The Future Favors Swift Swift: The more approachable, full-featured language • Easier to maintain • Is Considered Safer & Faster • Is Unified with Memory Management • Encourages interactive coding
  • 24. Presenter: Neha Sinha, Mindfire Solutions Questions?
  • 26. Presenter: Neha Sinha, Mindfire Solutions Thank You