23
Objective-C에서 Swift 조성규 - iOS Tech Talk

Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Embed Size (px)

Citation preview

Page 1: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Objective-C에서�Swift로

조성규

- iOS Tech Talk

Page 2: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Contents

•Naming�Style�

• Data�Types�-�Struct,�Enum,�Optional…�

• Constants�

• Properties�

• Closure

Page 3: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Naming�Style

•Objective-C와�Swift는�이름짓는�방법이�다릅니다�

•타입이름�

• 클래스�접두어는�이제�그만~�

•메서드,�열거형�등등

Page 4: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

타입�이름Objective-C Swift

CG.Point() CG.Size()

CGRect CGPoint CGSize

… …

struct CG { struct Point { } struct Size { } }

struct CL { struct Point { } struct Size { } }

struct Point { }

CL.Point() CL.Size()

Page 5: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

초기화�메서드Objective-C Swift

[[UIView alloc] initWithFrame:CGRectZero]; UIView(frame: CGRect.zero)

- (instancetype)initWithFrame:(CGRect)frame; init(frame: CGRect)

Page 6: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

메서드Objective-C Swift

[self presentViewController:vc animated:YES completion:nil];

[self dismissViewControllerAnimated:YES completion:nil];

self.present(vc, animated: true, completion: nil)

dismiss(animated: true, completion: nil)

Page 7: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

열거형�케이스Objective-C Swift

대문자�시작 소문자�시작

typedef NS_ENUM(NSInteger, WeekDays) { WeekDaysMon = 0, WeekDaysTue, WeekDaysWed, WeekDaysThu, WeekDaysFri };

WeekDaysMon;

enum WeekDays { case mon, tue, wed, thu, fri }

WeekDays.mon

Page 8: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

주의!

• Swift와�Objective-C를�혼용하여�사용할�경우네이밍�규칙에�따라�코딩하지�않으면 서로�번역이�잘못될�수�있습니다

Page 9: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

좋은�혼합의�예Objective-C Swift

[vc viewWithSomething:nil]; func view(withSomething sth: Any?) -> UIView?

- (UIView *)viewWithSomething:(id)sth; vc.view(withSomething: nil)

Page 10: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

잘못된�혼합의�예Objective-C Swift

??? func view(_ sth: Any?) -> UIView?

- (UIView *)view_with_sth:(id)sth; vc.view_(with_sth: nil)

Page 11: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Data�Types

•서로�다른�자료형�체계�

• 구조체,�열거형이�그�근간�

•Optional

Page 12: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

구조체Objective-C Swift

struct CGSize { CGFloat width; CGFloat height; }; typedef struct CGSize CGSize;

struct CGSize { var width: CGFloat var height: CGFloat func equalTo(_ size2: CGSize) -> Bool init() init(width: CGFloat, height: CGFloat) }

Page 13: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

구조체Swift

Swift의�기본�데이터�타입은�모두�구조체!

struct Int : SignedInteger, Comparable, Equatable { } struct Double { } struct String { } ……

Page 14: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

열거형Objective-C Swift

이름으로�묶은�상수 고유의�값

typedef NS_ENUM(NSInteger, WeekDays) { WeekDaysMon = 0, WeekDaysTue, WeekDaysWed, WeekDaysThu, WeekDaysFri };

enum WeekDays { case mon, tue, wed, thu, fri }

Page 15: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

옵셔널Objective-C Swift

UIView *some = nil; let some: UIView = nillet some: UIView? = nil

Page 16: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

옵셔널�/�열거형Swift

옵셔널�기능은�열거형으로�구현되어�있다!

enum Optional<Wrapped> : ExpressibleByNilLiteral {

case none

case some(Wrapped)

public init(_ some: Wrapped)

}

Page 17: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Constants

•달라진�상수의�정의와�사용

Page 18: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

타입�in�타입Objective-C Swift

static NSInteger const MaxInputValue = 100; static NSInteger const MinInputValue = 0;

static NSString *const InputMethodKeyboard = @“keyboard";

static NSString *const InputMethodMouse = @"mouse";

struct Input { struct Value { static let max = 100 static let min = 0 } enum Method { case keyboard case mouse } }

Page 19: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

지역�상수의�생활화Objective-C Swift

const를�매번�붙이기�불편 var와�let�중에�선택

const CGSize size = CGSizeZero; let size: CGSize = CGSize.zero

Page 20: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Properties

•다양해진�프로퍼티의�형태�

•이제�프로퍼티�메서드�오버라이드는�그만!

Page 21: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Property�ObserverObjective-C Swift

분산된�코드 한�눈에�파악�가능

@interface SomeViewController () @property (nonatomic) NSInteger count; @end

@implementation SomeViewController - (void)setCount:(NSInteger)count { _count = count; NSLog(@"count %ld", count); } @end

class ViewController: UIViewController { var count: Int { didSet { print("count \(count)") } } }

Page 22: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Computed�PropertyObjective-C Swift

분산된�코드 한�눈에�파악�가능

@interface SomeViewController () @property (nonatomic) NSInteger dollar; @property (nonatomic) NSInteger won; @end

@implementation SomeViewController - (NSInteger)won { return _dollar * 1000; } @end

class ViewController: UIViewController {

var dollar: Int = 10 var won: Int { return dollar * 1000 } }

Page 23: Objective-C에서 Swift로 전향할 때 생각해 볼 거리들

Closure

• Swift의�클로저는�일급객체�

• 변수,�상수,�매개변수�등으로�전달할�수�있음�

• 함수는�클로저의�일종�

•map,�filter,�reduce�등의�고차함수