Skip to content

Commit

Permalink
Added interface with variadic arguments instead of a set of values.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmkskn committed Nov 14, 2022
1 parent 42bc484 commit d601d89
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SwiftUI
import PlatformSpecificValue

struct ContentView: View {
private let shapeRadius: Int = platformSpecific([.macOS(15), .iOS(20)])
private let shapeRadius: Int = platformSpecific(.macOS(15), .iOS(20))

var body: some View {
RoundedRectangle(cornerRadius: shapeRadius)
Expand Down
6 changes: 6 additions & 0 deletions Sources/PlatformSpecificValue/PlatformSpecificValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ public func platformSpecific<Value>(_ platformValues: Set<PlatformSpecificValue<
fatalError("No value was provided.")
}
}


/// Returns a platform specific value.
public func platformSpecific<Value>(_ platformValues: PlatformSpecificValue<Value>..., `default` defaultValue: Value? = nil) -> Value {
platformSpecific(Set(platformValues), default: defaultValue)
}
16 changes: 16 additions & 0 deletions Tests/PlatformSpecificValueTests/PlatformSpecificValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,20 @@ final class PlatformSpecificValueTests: XCTestCase {
#error("Testing on an unsupported platform")
#endif
}

func testPassingVariadicArguments() throws {
let value: String = platformSpecific(.macOS("macOS"), .iOS("iOS"), .watchOS("watchOS"), .tvOS("tvOS"))

#if os(macOS)
XCTAssertEqual(value, "macOS")
#elseif os(iOS)
XCTAssertEqual(value, "iOS")
#elseif os(watchOS)
XCTAssertEqual(value, "watchOS")
#elseif os(tvOS)
XCTAssertEqual(value, "tvOS")
#else
#error("Testing on an unsupported platform")
#endif
}
}

0 comments on commit d601d89

Please sign in to comment.