objective-c에서 swift로 전향할 때 생각해 볼 거리들
TRANSCRIPT
Objective-C에서�Swift로
조성규
- iOS Tech Talk
Contents
•Naming�Style�
• Data�Types�-�Struct,�Enum,�Optional…�
• Constants�
• Properties�
• Closure
Naming�Style
•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()
초기화�메서드Objective-C Swift
[[UIView alloc] initWithFrame:CGRectZero]; UIView(frame: CGRect.zero)
- (instancetype)initWithFrame:(CGRect)frame; init(frame: CGRect)
메서드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)
열거형�케이스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
주의!
• Swift와�Objective-C를�혼용하여�사용할�경우네이밍�규칙에�따라�코딩하지�않으면 서로�번역이�잘못될�수�있습니다
좋은�혼합의�예Objective-C Swift
[vc viewWithSomething:nil]; func view(withSomething sth: Any?) -> UIView?
- (UIView *)viewWithSomething:(id)sth; vc.view(withSomething: nil)
잘못된�혼합의�예Objective-C Swift
??? func view(_ sth: Any?) -> UIView?
- (UIView *)view_with_sth:(id)sth; vc.view_(with_sth: nil)
Data�Types
•서로�다른�자료형�체계�
• 구조체,�열거형이�그�근간�
•Optional
구조체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) }
구조체Swift
Swift의�기본�데이터�타입은�모두�구조체!
struct Int : SignedInteger, Comparable, Equatable { } struct Double { } struct String { } ……
열거형Objective-C Swift
이름으로�묶은�상수 고유의�값
typedef NS_ENUM(NSInteger, WeekDays) { WeekDaysMon = 0, WeekDaysTue, WeekDaysWed, WeekDaysThu, WeekDaysFri };
enum WeekDays { case mon, tue, wed, thu, fri }
옵셔널Objective-C Swift
UIView *some = nil; let some: UIView = nillet some: UIView? = nil
옵셔널�/�열거형Swift
옵셔널�기능은�열거형으로�구현되어�있다!
enum Optional<Wrapped> : ExpressibleByNilLiteral {
case none
case some(Wrapped)
public init(_ some: Wrapped)
}
Constants
•달라진�상수의�정의와�사용
타입�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 } }
지역�상수의�생활화Objective-C Swift
const를�매번�붙이기�불편 var와�let�중에�선택
const CGSize size = CGSizeZero; let size: CGSize = CGSize.zero
Properties
•다양해진�프로퍼티의�형태�
•이제�프로퍼티�메서드�오버라이드는�그만!
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)") } } }
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 } }
Closure
• Swift의�클로저는�일급객체�
• 변수,�상수,�매개변수�등으로�전달할�수�있음�
• 함수는�클로저의�일종�
•map,�filter,�reduce�등의�고차함수