새소식

iOS/Swift

[Swift] Dynamic Library vs Static Library

  • -

회사에서 API를 만들 때 Dynamic Library?, Static Library? 개념에 대하여 이해하기 위해 포스팅해 보았다.

Dynamic과 Static의 차이는 여기서 확인할 수 있다.

 

Dynamic Library


처음 Xcode에서 Framework를 선택하면 Dynamic Library로 생성 된다.

 

아래와 같은 간단한 클래스를 만들었다.

open class DynamicClass{
    
    public init(){}
    
    open func test1(_ a: Int, _ b: Int) -> Int{
        return a + b
    }
    
}

 

init 사용하지 않으면 사용하려는 프로젝트에서 

initializer is inaccessible due to 'internal' protection level

위와 같은 에러가 발생하여 추가 하였다.

 

이제 사용하려고 프로젝트에 추가한다. 처음에는 [Embed & Sign]으로 설정하기!

코드를 아래처럼 작성하고 

let dynamicLib = DynamicClass().test1(1, 2)
print(dynamicLib) // 3

결과값이 정상적으로 나왔다!

 

이번에는 [Do Not Embed]로 설정해보았는데

 

오류가 발생하였다.

 

왜 안되는 지 몰라서 찾아보았다. 애플 문서에서 제공된 사진이다.

 

Dynamic Library

 

여기서 Dynamic Library를 개체 파일의 모음 또는 아카이브라고 정의하였다. 
하지만 Do Not Embed를 사용하면 개체파일들을 사용하지 못하기 때문에 오류가 발생한다.

 

 

 

 

Static Library


Static Library도 만들어 보았다. XCode에서 처음 프로젝트에서 [Static Library]로 설정하거나,

Build Setting - [Mach -O type]에서 변경할 수 있다.

 

간단하게 코드를 만들었다.

import Foundation

open class StaticTest{
    
    public init(){}
    
    open func test2(_ a: Int, _ b: Int) -> Int{
        return a - b
    }
}

 

위랑 똑같이 적용하기

let staticLib = StaticTest().test2(2, 1)
print(staticLib) // 1

 

그런데 Static Library는 [Embed & Sign]과 [Do Not Embed] 둘 다 설정해도 에러 없이 실행이 된다.

 

 

Static Library는 라이브러리의 코드를 포함하는 앱의 코드가 앱의 주소에 로드되기 때문에 [Embed & Sign]과 [Do Not Embed] 둘 다 문제 없이 실행이 된다.

 

 

 

참고

 

Overview of Dynamic Libraries

Overview of Dynamic Libraries Two important factors that determine the performance of apps are their launch times and their memory footprints. Reducing the size of an app’s executable file and minimizing its use of memory once it’s launched make the ap

developer.apple.com

 

728x90

'iOS > Swift' 카테고리의 다른 글

[Swift] frame vs bounds  (1) 2023.10.28
[Swift] BackgroundTasks  (0) 2023.09.03
[Swift] Core Data  (0) 2023.08.21
[Swift] User Notifications  (0) 2023.08.06
[Swift] @discardableResult  (0) 2023.07.26
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.