ios nsagora #3: objective-c vs. swift
DESCRIPTION
A comparison between Objective-C and an early beta version of Swift. Presentation organised by iOS NSAgora, the iOS Developers Community of Iasi, Romania.TRANSCRIPT
Tuesday 29 July, meeting #3
3
70
23 Members
Objective-C vs. Swift
alexcristea!iOS Dev
January 9
1570
1768
1951
2007
2008 #42
2014 #3
Modern.Safe.Fast.Fun
2014 #16
DISCLAIMER
21
vs
Language
24
Data Types
Objective C Swift
NSInteger Int
double Double
BOOL Bool
NSString String
NSArray Array
id AnyObject
Class AnyClass
SEL Selector
25
Define new Data Types
Objective C Swift
@interface class
@protocol protocol
struct struct
enum enum
26
Extending existing Data Types
Objective C Swift
categories extension
@interface (categoryName) @end
class a{ } extension a { }
struct b { } extension b{ }
enum b { } extension b{ }
27
#if __LP64__typedef long NSInteger;typedef unsigned long NSUInteger;#elsetypedef int NSInteger;typedef unsigned int NSUInteger;#endif!#define NSIntegerMax LONG_MAX#define NSIntegerMin LONG_MIN#define NSUIntegerMax ULONG_MAX
Objective-C
Data type definitions
28
struct Int : SignedInteger { init() init(_ value: Int) .... static var max: Int { get } static var min: Int { get } } !extension Int : Printable { var description: String { get } }
Swift
Data type definitions
29
@property (nonatomic) NSMutableArray *items;@property (nonatomic, strong) NSArray *immutableItems;@property (nonatomic, weak) id<DelegateProtocol> delegate;
Objective-C
var items:Array<Int> = [] let immutableItems = [1, 2, 3, 4] weak var delegate:DelegateProtocol?
Swift
Properties
30
@property (nonatomic) DataProcessor *processor;....
- (DataProcessor *)getDataProcessor { if(!_processor) { _processor = [DataProcessor alloc] init]; } return _processor; }
Objective-C
Lazy Properties
31
@lazy var processor = DataProcessor() var processorPower:Int { get { return processor.power } set(newPower) { self.processor.power = new.power } }
Swift
Lazy Properties
32
// Algorithm.h+ (int)fibonacci:(int)n;!// Algorithm.m+ (int)fibonacci:(int)n { if (n < 2) return 1; return [self fibonacci:n-1] + [self fibonacci:n-2];}
Objective-C
Methods
33
class func fibonacci(n: Int) -> Int { if n < 2 { return 1 } return fibonacci(n-1) + fibonacci(n-2) }
Swift
Methods
34
// UIView.h+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;!// Usage[UIView animateWithDuration:2 animations:^{ ....}];
Objective-C
Closures
35
// UIView.swift class func animateWithDuration(duration: NSTimeInterval, animations: (() -> Void)!) !//Usage UIView.animateWithDuration(2.5) { .... }
Swift
Closures
36
NSArray *arrayOfStrings = @[@"First", @"Second", @2, @"Third"];
Objective-C
var array:Array<Strings> = ["First", "Second", 2, "Third"]
Swift
Generics
37
operator infix ^ {} func ^ (var base: Int, power: Int) -> Int { var answer = 1; for _ in 1...power { answer = answer * base } return answer } var x = 2^5 // x = 32
Swift
Defining operators
38
// C Functions func arc4random() -> UInt32
!// Core Graphics Classesfunc CGGradientCreateWithColorComponents( space: CGColorSpace!, components: ConstUnsafePointer<CGFloat>, locations: ConstUnsafePointer<CGFloat>, count: UInt) -> CGGradient!
Swift
C Bridging
Performance
40
Performance (seconds)
Objective-‐C Swift
fibonacci(10) 0,000 0,000
fibonacci(20) 0,000 0,000
fibonacci(30) 0,011 0,015
fibonacci(40) 1,430 1,535
fibonacci(50) 180,486 192,095
41
Performance (seconds)
Objective-‐C Swift
sum(5000000) 0,026 0,097
sum(8000000) 0,040 0,154
sum(1300000) 0,065 0,263
sum(2100000) 0,103 0,406
sum(3400000) 0,168 0,565
?
Open Networking
Tuesday 29 July, meeting #3