swift @_spi (System Programming Interfaces)
swift @_spi (System Programming Interfaces)
api와 spi 정의
- api란?
- swift에서 사용할 수 있는 Application Programming Interface의 약자.
- spi란?
- api 중 일부는 특정 클라이언트에게만 제공.
spi는 실험적.
- apple에서 밑줄 있는 속성은 사용하지 말라고 권장. 밑줄 있는 속성의 의미는 변경될 수 있고 안정화 전에 Swift의 진화 과정을 거칠 확률이 높음.
- spi의 경우 기존 접근제어자보다 우선순위가 높음.
spi 선언
public class PaymentFeature {
public func pay() async {}
@_spi(bitcoin) public func payWithBitCoin() async {}
@_spi(apple) public func payWithApple() async {}
@_spi_available(watchOS 9, *)
@available(tvOS, unavailable)
public private(set) var items = [String]()
public init() {}
}
spi 사용
import SwiftUI
import PaymentFeature
class ViewModel {
private let feature = PaymentFeature()
func pay() async {
await feature.pay()
}
}
@_spi 가 붙은 함수에 접근하기 위해서는 import 부분을 수정해야 함.
다중으로 사용할 수도 있음.
프로젝트 적용 예시
아래의 형태로 사용하거나, 테스트 코드를 작성할 때 프로퍼티르 private으로 되어 있어서 난감한 경우 이를 @_spi로 해결할 수 있어 보임.
public struct MyFeature {
var add: () -> ()
var revmoe: () -> ()
@MainActor
public static let liveValue: Self = .init {
return ()
} revmoe: {
return ()
}
@MainActor
@_spi(test) public static let testableValue: Self = .init {
return ()
} revmoe: {
return ()
}
}
(참고)
https://blog.eidinger.info/system-programming-interfaces-spi-in-swift-explained
System Programming Interfaces (SPI) in Swift Explained
System Programming Interfaces are declarations available only for certain clients. Experimental attribute @_spi (and others) in the Swift language
blog.eidinger.info
https://ios-development.tistory.com/1452
[iOS - swift] @_spi(Private) import 개념 (#@_api, Swift Private Interface, 프레임워크, 모듈)
@_spi 개념 swift의 접근 제어자는 open, public, internal, fileprivate, private이 있지만 프레임워크 사이의 또 다른 접근 제어자가 존재 SPI(Swift Private Interface)도 외부에 노출할 인터페이스 제어가 가능 @_spi(P
ios-development.tistory.com