iOS NSAgora #3: Objective-C vs. Swift

1,070 views
809 views

Published on

A comparison between Objective-C and an early beta version of Swift.

Presentation organised by iOS NSAgora, the iOS Developers Community of Iasi, Romania.

Published in: Engineering
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,070
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

iOS NSAgora #3: Objective-C vs. Swift

  1. 1. Tuesday 29 July, meeting #3
  2. 2. 3 70
  3. 3. 23 Members
  4. 4. Objective-C vs. Swift alexcristea! iOS Dev
  5. 5. January 9
  6. 6. 1570
  7. 7. 1768
  8. 8. 1951
  9. 9. 2007
  10. 10. 2008 #42
  11. 11. 2014 #3
  12. 12. Modern.Safe.Fast.Fun
  13. 13. 2014 #16
  14. 14. DISCLAIMER
  15. 15. 21
  16. 16. vs
  17. 17. Language
  18. 18. 24 Data  Types Objective  C Swift NSInteger Int double Double BOOL Bool NSString String NSArray Array id AnyObject Class AnyClass SEL Selector
  19. 19. 25 Define  new  Data  Types Objective  C Swift @interface class @protocol protocol struct struct enum enum
  20. 20. 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{ }
  21. 21. 27 #if __LP64__ typedef long NSInteger; typedef unsigned long NSUInteger; #else typedef int NSInteger; typedef unsigned int NSUInteger; #endif ! #define NSIntegerMax LONG_MAX #define NSIntegerMin LONG_MIN #define NSUIntegerMax ULONG_MAX Objective-C Data type definitions
  22. 22. 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
  23. 23. 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
  24. 24. 30 @property (nonatomic) DataProcessor *processor; .... - (DataProcessor *)getDataProcessor {
 if(!_processor) {
 _processor = [DataProcessor alloc] init];
 }
 return _processor;
 } Objective-C Lazy Properties
  25. 25. 31 @lazy var processor = DataProcessor() var processorPower:Int { get { return processor.power } set(newPower) { self.processor.power = new.power } } Swift Lazy Properties
  26. 26. 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
  27. 27. 33 class func fibonacci(n: Int) -> Int { if n < 2 { return 1 } return fibonacci(n-1) + fibonacci(n-2) } Swift Methods
  28. 28. 34 // UIView.h + (void)animateWithDuration:(NSTimeInterval)duration 
 animations:(void (^)(void))animations; ! // Usage [UIView animateWithDuration:2 animations:^{ .... }]; Objective-C Closures
  29. 29. 35 // UIView.swift class func animateWithDuration(duration: NSTimeInterval, animations: (() -> Void)!) ! //Usage UIView.animateWithDuration(2.5) { .... } Swift Closures
  30. 30. 36 NSArray *arrayOfStrings = @[@"First", @"Second", @2, @"Third"]; Objective-C var array:Array<Strings> = ["First", "Second", 2, "Third"] Swift Generics
  31. 31. 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
  32. 32. 38 // C Functions func arc4random() -> UInt32 ! // Core Graphics Classes func CGGradientCreateWithColorComponents( space: CGColorSpace!, components: ConstUnsafePointer<CGFloat>, locations: ConstUnsafePointer<CGFloat>, count: UInt) -> CGGradient! Swift C Bridging
  33. 33. Performance
  34. 34. 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
  35. 35. 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
  36. 36. ?
  37. 37. Open Networking
  38. 38. Tuesday 29 July, meeting #3

×