๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Programming

[iOS] Objective-C <-> Swift ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ธฐ(1. ํ”„๋กœ์ ํŠธ์—์„œ 2. framework์—์„œ) + @objc attribute ์ •ํ™•ํžˆ ์‚ฌ์šฉํ•˜๊ธฐ

์ œํ’ˆ ๊ฐœ๋ฐœ ์ค‘ ๊ณ ๊ฐ์‚ฌ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ Swift ์–ธ์–ด์— ๋Œ€์‘ํ•˜๋Š” ์ผ์ด ๋งŽ์•„์ง€๋ฉด์„œ,

๊ธฐ์กด Objective-C๋กœ ๊ตฌํ˜„๋œ ์ œํ’ˆ ์ž์ฒด๋ฅผ Swift๋กœ Converting ํ•˜๋Š” ์ž‘์—…์„ ํ•˜๊ณ  ์žˆ๋‹ค.

 

์ด ๊ณผ์ •์—์„œ ํ•œ ํ”„๋กœ์ ํŠธ์—์„œ Objective-C์™€ Swift๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ณ , 

Swift๋กœ ๋งŒ๋“  Framework๋ฅผ Objective-C ํ”„๋กœ์ ํŠธ์—์„œ

Objective-C๋กœ ๋งŒ๋“  Framework๋ฅผ Swift ํ”„๋กœ์ ํŠธ์—์„œ ํ…Œ์ŠคํŠธํ•ด์•ผ ํ–ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ Swift์™€ Objective-C ๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ •ํ•  ๊ฒƒ๋“ค๊ณผ

๋‘ ์–ธ์–ด์˜ ์ฐจ์ด์ ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์ ์„ ๊ณต์œ ํ•˜๊ณ ์ž ํ•œ๋‹ค.


[Contents]

    1. Swift ํ”„๋กœ์ ํŠธ์—์„œ Objective-C ์‚ฌ์šฉํ•˜๊ธฐ
      1. Swift ํ”„๋กœ์ ํŠธ์—์„œ Objective-C ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• (Import Objective-C into Swift)
    2. Objective-C ํ”„๋กœ์ ํŠธ์—์„œ Swift ์‚ฌ์šฉํ•˜๊ธฐ
      1. Objective-C ํ”„๋กœ์ ํŠธ์—์„œ Swift ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•(Import Swift into Objective-C)
      2. Objective-C ํ”„๋กœ์ ํŠธ์—์„œ Swift Framework๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ
    3. Swift, Objective-C๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ 
      1. enums in Swift and Objective-C

โญ๏ธ 1. Swift ํ”„๋กœ์ ํŠธ์—์„œ Objective-C ์‚ฌ์šฉํ•˜๊ธฐ

๊ณต์‹ ๋ฌธ์„œ : https://developer.apple.com/documentation/swift/importing-objective-c-into-swift

 

Importing Objective-C into Swift | Apple Developer Documentation

Access classes and other declarations from your Objective-C code in Swift.

developer.apple.com

 

1-1. Swift ํ”„๋กœ์ ํŠธ์—์„œ Objective-C ์†Œ์Šค ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• (Import Objective-C into Swift)

1.  ๋ธŒ๋ฆฌ์ง• ํ—ค๋” ํŒŒ์ผ(Bridging header) ์ƒ์„ฑ

Swift code์—์„œ Objective-C ์ฝ”๋“œ๋ฅผ ์“ฐ๋ ค๋ฉด, Objcective-C Bridging header ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

Xcode์—์„œ ์ด ํ—ค๋”๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ž๋™, ์ˆ˜๋™ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

๋จผ์ €, ์Šค์œ„ํ”„ํŠธ ํ”„๋กœ์ ํŠธ์—์„œ ์˜ค๋ธŒ์ ํ‹ฐ๋ธŒ-C ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํŒ์—…์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

1-1) ์ด๋•Œ, Create Bridging Header๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ(์ž๋™)
ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๊ณณ์— AppName-Bridging-Header.h ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

1-2) Don't Create๋ฅผ ์„ ํƒํ•˜๊ฑฐ๋‚˜ ํŒ์—…์ด ๋ณด์ด์ง€ ์•Š์„ ๊ฒฝ์šฐ(์ˆ˜๋™)

AppName-Bridging-Header.h ํŒŒ์ผ์„ ์ง์ ‘ ์ƒ์„ฑํ•ด์ค€๋‹ค.
๋‹ค๋งŒ, ์ด ํ”„๋กœ์ ํŠธ์—์„œ ๋ธŒ๋ฆฟ์ง• ํ—ค๋” ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๋ช…์‹œํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด์„œ
'Project - Target - Build Settings - Objective-C Bridging Header'์— ํŒŒ์ผ์˜ ์œ„์น˜/AppName-Bridging-Header.h ๋ฅผ ์ถ”๊ฐ€

*์ด๋•Œ, ๋ธŒ๋ฆฌ์ง• ํ—ค๋” ํŒŒ์ผ๋ช…์€ "Project name"-Bridging-Header.h ์ด์–ด์•ผ ํ•จ!

 

์ƒ์„ฑ๋œ ํ—ค๋”ํŒŒ์ผ์„ Build Settings์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ปดํŒŒ์ผํ•  ๋•Œ ์ฐธ์กฐ๋˜๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.

 

*์—ฌ๋Ÿฌ ๊ฐœ target ์ด ์žˆ์–ด ์—ฌ๋Ÿฌ ๊ฐœ ๋ธŒ๋ฆฟ์ง• ํ—ค๋” ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋Š” ๊ฒฝ์šฐ!

"Project name"-Bridging-Header.h 1๊ฐœ๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋‹ค ์‚ญ์ œ ํ›„ Build Setting์— ํ•ด๋‹น ํŒŒ์ผ์˜ ๊ฒฝ๋กœ(์ƒ๋Œ€๊ฒฝ๋กœ)๋ฅผ ๋„ฃ์–ด์ฃผ์–ด

๋ชจ๋“  ํƒ€๊นƒ์ด ๊ฐ™์€ ๋ธŒ๋ฆฟ์ง• ํ—ค๋”๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

2. ์‚ฌ์šฉํ•˜๊ธฐ

์ด ํŒŒ์ผ ์•ˆ์— ์•„๋ž˜์™€ ๊ฐ™์ด Swift์—์„œ ์‚ฌ์šฉํ•  ์˜ค๋ธŒ์ ํ‹ฐ๋ธŒ-C ํŒŒ์ผ์˜ ํ—ค๋” ํŒŒ์ผ์„ import ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

#import "NewObjectiveCFile.h"
// MARK: - Models -
#import "User.h"
#import "Group.h"

// MARK: - Managers -
#import "AnalyticsManager.h"
#import "AlertManager.h"

+Swift์—์„œ Objective-C ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, NS_SWIFT_NAME ๋งคํฌ๋กœ(preprocessor)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปค์Šคํ…€ ๊ฐ€๋Šฅ!

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜ ๋ฉ”์†Œ๋“œ๋ฅผ

+(void)SuperGoodAdditionFeature:(NSInteger)a (NSInteger)b;

์›๋ž˜ Swift์—์„œ ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ,

let result = SuperGoodAdditionFeature(1, 2)

NS_SWIFT_NAME ๋งคํฌ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ •์˜ํ•˜์—ฌ Swift ์šฉ ๋ณ„๋ช…์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

+(void)SuperGoodAdditionFeature:(NSInteger)a (NSInteger)b
NS_SWIFT_NAME(add(a:b:));

์žฌ์ •์˜ํ•œ ์ด๋ฆ„(renaming)์œผ๋กœ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋”ฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

let result = add(a: 1,b: 2)

๊ณต์‹ ๋ฌธ์„œ : https://developer.apple.com/documentation/swift/renaming-objective-c-apis-for-swift

 


โญ๏ธ 2. Objective-C ํ”„๋กœ์ ํŠธ์—์„œ Swift ์‚ฌ์šฉํ•˜๊ธฐ

๊ณต์‹ ๋ฌธ์„œ : https://developer.apple.com/documentation/swift/importing-swift-into-objective-c

 

Importing Swift into Objective-C | Apple Developer Documentation

Access Swift types and declarations from within your Objective-C codebase.

developer.apple.com

2-1. Objective-C ํ”„๋กœ์ ํŠธ์—์„œ Swift ์†Œ์Šค ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•(Import Swift into Objective-C)

1. 'Project - Target - Build Settings - Defines Module'์„  Yes๋กœ ์„ค์ •

2. @objc ์†์„ฑ(attribute) ์‚ฌ์šฉ

Objective-C์—์„œ ์‚ฌ์šฉํ•  Swift ํŒŒ์ผ์˜ ํด๋ž˜์Šค๋‚˜ ํ•จ์ˆ˜๋ช… ์•ž์—๋Š” @objc ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ Swift ํด๋ž˜์Šค, ๋ฉ”์„œ๋“œ, ์†์„ฑ์„ Objective-C์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. 

์ž์„ธํ•œ ๋‚ด์šฉ : https://lxxyeon.tistory.com/95

์ด ์†์„ฑ์€ compiler์—๊ฒŒ Objective-C์—์„œ ์†์„ฑ์„ ๋ถ™์ธ ๊ฒƒ์— ์ ‘๊ทผํ•  ๊ฒƒ์„ ์•Œ๋ฆฌ๊ณ , ๋งŒ์•ฝ Swift class๊ฐ€ Objective-C๋ฅผ ์ƒ์†๋˜์—ˆ๋‹ค๋ฉด, compiler๊ฐ€ ์ž๋™์œผ๋กœ @objc๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค. ์ด ์†์„ฑ์€ 3๊ฐ€์ง€๋กœ ์‚ฌ์šฉ๋˜๋Š”๋ฐ,

    • @objc : ๋ฉ”์„œ๋“œ, ์†์„ฑ
    • @objc(name) : ์‚ฌ์šฉํ•  ๋ฉ”์„œ๋“œ๋‚˜ ์†์„ฑ์˜ ์ด๋ฆ„์„ ์žฌ์ •์˜(custom)ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
@objc(sharedManager)
static let shared = MyManager()

์œ„ ๋ณ€์ˆ˜๋ฅผ Objective-C์—์„œ ํ˜ธ์ถœํ•  ๋•Œ,

[MyManager sharedManager]

๋ฉ”์†Œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋„ ์žฌ์ •์˜ ๊ฐ€๋Šฅ!

@objc(processNotificationPayload:notificationType:)
static func processNotification(with payload: String, notificationType: NotificationType) {
  ...
}
    • @objcMembers : ํด๋ž˜์Šค์˜ ๋ชจ๋“  ๋ฉค๋ฒ„, ํ™•์žฅ, ํ•˜์œ„ ํด๋ž˜์Šค, ํด๋ž˜์Šค์˜ extensions 
@objcMembers class MyController: UIViewController {
    func login() {

    }
}

๊ณต์‹๋ฌธ์„œ : https://docs.swift.org/swift-book/documentation/the-swift-programming-language/attributes/#objc

 

์ด๋•Œ, ํด๋ž˜์Šค๋Š” Objective-C ํด๋ž˜์Šค(์˜ˆ: NSObject)๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

@objc(TestService)
class TestService: NSObject {
    @objc static let shared = TestService()
    
    @objc func sayHello() {
    	print("Hello!")
    }
}

์ด์ œ Swift ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ Objective-C ํŒŒ์ผ์—์„œ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋ณด๋ฉด, ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

[[TestService shared] sayHello];

// error
Use of undeclared identifier 'TestService'

์ด ์˜ค๋ฅ˜๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ—ค๋” ํŒŒ์ผ์„ ์ž„ํฌํŠธ ํ•˜์ง€ ์•Š์•„์„œ ์ƒ๊ธด ์˜ค๋ฅ˜!!


๊ทธ๋ ‡๋‹ค๋ฉด ์šฐ๋ฆฌ๋Š” ํ—ค๋” ํŒŒ์ผ์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š๋ƒ!

 

๊ทธ๋Ÿด ํ•„์š” ์—†์ด ์šฐ๋ฆฌ๊ฐ€ ์œ„์—์„œ Defines Module์„ Yes๋กœ ์„ค์ •ํ•ด ๋†“์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผํ•˜๋Š” ๊ณผ์ •์—์„œ XCode๊ฐ€ ํ—ค๋” ํŒŒ์ผ์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด ์ค€๋‹ค.

 

3. ์‚ฌ์šฉํ•  Objective-C์— #import "{module name}-Swift.h" ์ถ”๊ฐ€

. h์—์„œ๋Š” ์ˆœํ™˜์ฐธ์กฐ(cyclical reference)๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด Swift code๋ฅผ import ํ•˜์ง€ ์•Š๊ณ  @class, @protocol๋กœ ๋งˆํ‚นํ•œ๋‹ค.

// MyObjcClass.h
@class MySwiftClass;
@protocol MySwiftProtocol;

@interface MyObjcClass : NSObject
- (MySwiftClass *)returnSwiftClassInstance;
- (void)test;
- (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
// ...
@end

.m ํŒŒ์ผ์—์„œ ์ž๋™ ์ƒ์„ฑ๋œ ๋ธŒ๋ฆฟ์ง€ ํ—ค๋” ํŒŒ์ผ(-Swift.h)์„ import ํ•ด์ค€๋‹ค.

// .m
#import "MyObjcClass.h"
#import "ProductModuleName-Swift.h"

@implementation MyObjcClass
- (void) test {
    MySwiftClass *module = [[MySwiftClass alloc] init];
    [module test];
}
@end

4. ์‚ฌ์šฉํ•˜๊ธฐ

 


2-2. Objective-C์—์„œ Swift Framework๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ

๋จผ์ € ๊ฒฐ๋ก ์€ Swift Framework ๋‚ด์— ๋ธŒ๋ฆฟ์ง• ํ—ค๋”๋ฅผ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ•  Objective-C ์ฝ”๋“œ ๋‚ด์— import๋ฅผ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ทผ๋ฐ, ์ด๋•Œ, ์ง์ ‘ ๋ธŒ๋ฆฟ์ง• ํ—ค๋”๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ์„ค์ •์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

HOW? Build Setting > SWIFT_INSTALL_OBJC_HEADER ๊ฒ€์ƒ‰ > install Generated Header > Yes๋กœ ์„ค์ •!!

 

 build ๋œ ํ”„๋ ˆ์ž„์›Œํฌ ๋ ˆํฌ์— ๊ฐ€๋ฉด ์ƒ์„ฑ๋œ ํ—ค๋” ํŒŒ์ผ์ด ์žˆ์Œ์„ ํ™•์ธ!

์ด ํ—ค๋”ํŒŒ์ผ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ณณ์— import ํ•ด์ฃผ๋ฉด ๋œ๋‹ค!

์ด ํ—ค๋”ํŒŒ์ผ ๋˜ํ•œ Swift์˜ interface๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ๊ณ , ์ƒ์„ฑ๋œ umbrella header๋Š” umbrella framework์— ๋Œ€ํ•œ master header ๊ฐ€ ๋œ๋‹ค.

#import <Framework/Framework-Swift.h>

 

*umbrella framework๋Š” ์—ฌ๋Ÿฌ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๊ทธ๋ฃนํ™”(subframework๋“ค์„ encapsulate) ํ•œ ๊ฒƒ์œผ๋กœ header file๋“ค์„ include ํ•˜๊ณ  ์žˆ๋‹ค.

Umbrella framework๋Š” ๋‚ด๋ถ€์˜ ๊ตฌํ˜„์„ ์ž˜ ์ˆจ๊ฒจ์„œ ํ•„์š”ํ•œ interface ๋งŒ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๊ณ ,

precompiled header๋ฅผ ์‚ฌ์šฉํ•ด build ๋„ ๋” ๋นจ๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Œ.


โญ๏ธ 3. Swift, Objective-C๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•  ์ !

  • Objective-C์—์„œ๋Š” ๊ตฌ์กฐ์ฒด์™€ ํŠœํ”Œ์ด ํ‘œ์‹œ๋˜์ง€ ์•Š๋Š”๋‹ค.
    • Swift์—์„œ Tuple์„ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐ’์„ ํ•œ ๋ฒˆ์— ๋„˜๊ธธ ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ Objective-C๋Š” ARC๊ฐ€ ์ ์šฉ๋œ ์ดํ›„์—” struct ๋‚ด๋ถ€์— ํด๋ž˜์Šค ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ๊ธˆ์ง€๋˜์–ด ์žˆ์Œ.
    • Objective-C์— ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ ๋ฐœ์ƒ >  unsafe_unretained ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ!

  • Swift Tuples, Generics์„ ํฌํ•จํ•œ ์•„๋ž˜ ์œ ํ˜•๋“ค์€ @objc๋ฅผ ๋ถ™์—ฌ๋„ Objective-C์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  • Objective-C๋Š” ํ•จ์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ ์ธ์ž์— default ๊ฐ’์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • Objective-C์—์„œ ์‚ฌ์šฉํ•  Swift ํด๋ž˜์Šค๋ฅผ Objective-C ํด๋ž˜์Šค(์˜ˆ: NSObject)๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.
  • Any in Swift = id in Objective-C
  • Objective-C์—์„œ๋Š” Swift ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ protocol์€ ๊ตฌํ˜„ ๊ฐ€๋Šฅ!
  • Objective-C ์†์„ฑ ์ค‘ null ์ง€์ •์ž๊ฐ€ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด, Swift์—์„œ๋Š” ์˜ต์…”๋„๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค ->  ์–ธ๋ž˜ํ•‘๋œ๋‹ค. -> ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

3-1.enums in Swift, Objective-C

IMPORTANT: Swift enums can't be used in Objective-C header files.

Objectiev-C์™€ Swift ์—ด๊ฑฐํ˜•์˜ ๋ถˆ์ผ์น˜ํ•จ์„ ๋งž์ถฐ๋ณด์ž.

๊ธฐ๋ณธ์ ์ธ Objectiev-C์˜ ์—ด๊ฑฐํ˜•์˜ ์˜ˆ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

typedef enum my_type {
    MyTypeDefault = 0,
    MyTypeSolo,
    MyType
}MyType;

 

์ด๋Ÿฐ ์‹์œผ๋กœ ์„ ์–ธํ•  ๊ฒฝ์šฐ MyType์ด๋ผ๋Š” ํƒ€์ž…์€ Swift์˜ enum๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ struct ํ˜•ํƒœ๋กœ ์„ ์–ธ๋˜์–ด ๋ฒ„๋ฆฐ๋‹ค.

๋”ฐ๋ผ์„œ, Swift์—์„œ ์‚ฌ์šฉํ•  ๋•Œ, enum ์Šค๋Ÿฝ์ง€ ์•Š์€ ํ˜•ํƒœ์ธ ๊ฐ ํ•„๋“œ์˜ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ๋œ๋‹ค.

let type: MyType = MyTypeDefault

 

์ด๋ฅผ Swift์— ์–ด์šธ๋ฆฌ๊ฒŒ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด NS_ENUM ๋งคํฌ๋กœ๋ฅผ ์ด์šฉํ•ด ์—ด๊ฑฐํ˜•์„ ์ •์˜ํ•˜์—ฌ ์ˆ˜์ •~!

typedef NS_ENUM(int, MyType) {
    MyTypeDefault = 0,
    MyTypeSolo,
    MyType
};

 

์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•˜๋ฉด MyType์€ Swift์˜ enumํ˜•์‹์œผ๋กœ ์žฌ์ •์˜๋˜์–ด MyType ํƒ€์ž… ์ธ์Šคํ„ด์Šค์˜ ๊ฐ’์„ .default, .solo ์™€ ๊ฐ™์ด prefix๋ฅผ ์ƒ๋žตํ•œ ํ˜•ํƒœ์˜ ์ด๋ฆ„์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค. 

let type: MyType = .default

 

์ถ”๊ฐ€๋กœ, ์•„๋ž˜์™€ ๊ฐ™์ด ํŠน์ดํ•œ ์ด๋ฆ„์„ ์ถ”๊ฐ€๋œ ์ƒํ™ฉ๋„ ์‚ดํŽด๋ณด์ž.

๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ•œ AnotherTypeSuperSolo๋ผ๋Š” ์ด๋ฆ„์€ ์–ด์šธ๋ฆฌ์ง€ ์•Š๊ฒŒ. AnotherTypeSuperSolo์™€ ๊ฐ™์€ ์‹ฌ๋ณผ๋กœ ๋ฐ–์— ์ ‘๊ทผํ•  ์ˆ˜๊ฐ€ ์—†์„ ๊ฒƒ์ด๋‹ค.

typedef NS_ENUM(int, MyType) {
    MyTypeDefault = 0,
    MyTypeSolo,
    MyType,
    AnotherTypeSuperSolo
};

 

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋˜ NS_SWIFT_NAME์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•˜๋‹ค.

typedef NS_ENUM(int, MyType) {
    MyTypeDefault = 0,
    MyTypeSolo,
    MyType,
    AnotherTypeSuperSolo NS_SWIFT_NAME(anotherTypeSuperSolo)
};

์ด์ œ ์ถ”๊ฐ€ํ–ˆ๋˜ ๋งˆ์ง€๋ง‰ ์ด๋ฆ„์„ Swift ์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

let firstType: MyType = .default
let secondType: MyType = .anotherTypeSuperSolo

 

์ฐธ๊ณ : https://infinum.com/handbook/ios/miscellaneous/swift-objective-c-interoperability-and-best-practices#make-swift-visible-to-objective-c

728x90
๋ฐ˜์‘ํ˜•