This commit is contained in:
Sindre Sorhus 2018-04-12 00:19:30 +07:00
commit c78c1b00b6
14 changed files with 1081 additions and 0 deletions

8
.editorconfig Normal file
View File

@ -0,0 +1,8 @@
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
* text=auto
*.swift text eol=lf

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/.build
/Packages
xcuserdata
project.xcworkspace

3
.travis.yml Normal file
View File

@ -0,0 +1,3 @@
language: swift
osx_image: xcode9.3
script: xcodebuild test -project Defaults.xcodeproj -scheme Defaults-Package -quiet

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.0.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>

View File

@ -0,0 +1,529 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objects = {
/* Begin PBXAggregateTarget section */
"Defaults::DefaultsPackageTests::ProductTarget" /* DefaultsPackageTests */ = {
isa = PBXAggregateTarget;
buildConfigurationList = OBJ_30 /* Build configuration list for PBXAggregateTarget "DefaultsPackageTests" */;
buildPhases = (
);
dependencies = (
OBJ_33 /* PBXTargetDependency */,
);
name = DefaultsPackageTests;
productName = DefaultsPackageTests;
};
/* End PBXAggregateTarget section */
/* Begin PBXBuildFile section */
OBJ_21 /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* Defaults.swift */; };
OBJ_28 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
OBJ_39 /* DefaultsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* DefaultsTests.swift */; };
OBJ_41 /* Defaults.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = "Defaults::Defaults::Product" /* Defaults.framework */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
E30557F5207E6D3E003401A1 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = OBJ_1 /* Project object */;
proxyType = 1;
remoteGlobalIDString = "Defaults::Defaults";
remoteInfo = Defaults;
};
E30557F6207E6D3F003401A1 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = OBJ_1 /* Project object */;
proxyType = 1;
remoteGlobalIDString = "Defaults::DefaultsTests";
remoteInfo = DefaultsTests;
};
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
"Defaults::Defaults::Product" /* Defaults.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Defaults.framework; sourceTree = BUILT_PRODUCTS_DIR; };
"Defaults::DefaultsTests::Product" /* DefaultsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = DefaultsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_12 /* DefaultsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = DefaultsTests.swift; sourceTree = "<group>"; usesTabs = 1; };
OBJ_6 /* Package.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; lineEnding = 0; path = Package.swift; sourceTree = "<group>"; usesTabs = 1; };
OBJ_9 /* Defaults.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Defaults.swift; sourceTree = "<group>"; usesTabs = 1; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
OBJ_22 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 0;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
OBJ_40 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 0;
files = (
OBJ_41 /* Defaults.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
OBJ_10 /* Tests */ = {
isa = PBXGroup;
children = (
OBJ_11 /* DefaultsTests */,
);
name = Tests;
sourceTree = SOURCE_ROOT;
};
OBJ_11 /* DefaultsTests */ = {
isa = PBXGroup;
children = (
OBJ_12 /* DefaultsTests.swift */,
);
name = DefaultsTests;
path = Tests/DefaultsTests;
sourceTree = SOURCE_ROOT;
};
OBJ_13 /* Products */ = {
isa = PBXGroup;
children = (
"Defaults::DefaultsTests::Product" /* DefaultsTests.xctest */,
"Defaults::Defaults::Product" /* Defaults.framework */,
);
name = Products;
sourceTree = BUILT_PRODUCTS_DIR;
};
OBJ_5 /* */ = {
isa = PBXGroup;
children = (
OBJ_6 /* Package.swift */,
OBJ_7 /* Sources */,
OBJ_10 /* Tests */,
OBJ_13 /* Products */,
);
name = "";
sourceTree = "<group>";
};
OBJ_7 /* Sources */ = {
isa = PBXGroup;
children = (
OBJ_8 /* Defaults */,
);
name = Sources;
sourceTree = SOURCE_ROOT;
};
OBJ_8 /* Defaults */ = {
isa = PBXGroup;
children = (
OBJ_9 /* Defaults.swift */,
);
name = Defaults;
path = Sources/Defaults;
sourceTree = SOURCE_ROOT;
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
"Defaults::Defaults" /* Defaults */ = {
isa = PBXNativeTarget;
buildConfigurationList = OBJ_17 /* Build configuration list for PBXNativeTarget "Defaults" */;
buildPhases = (
OBJ_20 /* Sources */,
OBJ_22 /* Frameworks */,
);
buildRules = (
);
dependencies = (
);
name = Defaults;
productName = Defaults;
productReference = "Defaults::Defaults::Product" /* Defaults.framework */;
productType = "com.apple.product-type.framework";
};
"Defaults::DefaultsTests" /* DefaultsTests */ = {
isa = PBXNativeTarget;
buildConfigurationList = OBJ_35 /* Build configuration list for PBXNativeTarget "DefaultsTests" */;
buildPhases = (
OBJ_38 /* Sources */,
OBJ_40 /* Frameworks */,
);
buildRules = (
);
dependencies = (
OBJ_42 /* PBXTargetDependency */,
);
name = DefaultsTests;
productName = DefaultsTests;
productReference = "Defaults::DefaultsTests::Product" /* DefaultsTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
"Defaults::SwiftPMPackageDescription" /* DefaultsPackageDescription */ = {
isa = PBXNativeTarget;
buildConfigurationList = OBJ_24 /* Build configuration list for PBXNativeTarget "DefaultsPackageDescription" */;
buildPhases = (
OBJ_27 /* Sources */,
);
buildRules = (
);
dependencies = (
);
name = DefaultsPackageDescription;
productName = DefaultsPackageDescription;
productType = "com.apple.product-type.framework";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
OBJ_1 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0930;
};
buildConfigurationList = OBJ_2 /* Build configuration list for PBXProject "Defaults" */;
compatibilityVersion = "Xcode 9.3";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = OBJ_5 /* */;
productRefGroup = OBJ_13 /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
"Defaults::Defaults" /* Defaults */,
"Defaults::SwiftPMPackageDescription" /* DefaultsPackageDescription */,
"Defaults::DefaultsPackageTests::ProductTarget" /* DefaultsPackageTests */,
"Defaults::DefaultsTests" /* DefaultsTests */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
OBJ_20 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 0;
files = (
OBJ_21 /* Defaults.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
OBJ_27 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 0;
files = (
OBJ_28 /* Package.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
OBJ_38 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 0;
files = (
OBJ_39 /* DefaultsTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin PBXTargetDependency section */
OBJ_33 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = "Defaults::DefaultsTests" /* DefaultsTests */;
targetProxy = E30557F6207E6D3F003401A1 /* PBXContainerItemProxy */;
};
OBJ_42 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = "Defaults::Defaults" /* Defaults */;
targetProxy = E30557F5207E6D3E003401A1 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
/* Begin XCBuildConfiguration section */
OBJ_18 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ENABLE_TESTABILITY = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Defaults.xcodeproj/Defaults_Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Defaults;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = macosx;
SWIFT_VERSION = 4.0;
TARGET_NAME = Defaults;
};
name = Debug;
};
OBJ_19 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ENABLE_TESTABILITY = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Defaults.xcodeproj/Defaults_Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"$(TOOLCHAIN_DIR)/usr/lib/swift/macosx",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = Defaults;
PRODUCT_MODULE_NAME = "$(TARGET_NAME:c99extidentifier)";
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = macosx;
SWIFT_VERSION = 4.0;
TARGET_NAME = Defaults;
};
name = Release;
};
OBJ_25 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
LD = /usr/bin/true;
OTHER_SWIFT_FLAGS = "-swift-version 4 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/4 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
OBJ_26 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
LD = /usr/bin/true;
OTHER_SWIFT_FLAGS = "-swift-version 4 -I $(TOOLCHAIN_DIR)/usr/lib/swift/pm/4 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk";
SWIFT_VERSION = 4.0;
};
name = Release;
};
OBJ_3 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_NS_ASSERTIONS = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.12;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "-DXcode";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = SWIFT_PACKAGE;
SWIFT_COMPILATION_MODE = singlefile;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
USE_HEADERMAP = NO;
VALID_ARCHS = x86_64;
};
name = Debug;
};
OBJ_31 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
SUPPORTED_PLATFORMS = macosx;
};
name = Debug;
};
OBJ_32 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
SUPPORTED_PLATFORMS = macosx;
};
name = Release;
};
OBJ_36 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Defaults.xcodeproj/DefaultsTests_Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@loader_path/../Frameworks",
"@loader_path/Frameworks",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
SUPPORTED_PLATFORMS = macosx;
SWIFT_VERSION = 4.0;
TARGET_NAME = DefaultsTests;
};
name = Debug;
};
OBJ_37 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PLATFORM_DIR)/Developer/Library/Frameworks",
);
HEADER_SEARCH_PATHS = "$(inherited)";
INFOPLIST_FILE = Defaults.xcodeproj/DefaultsTests_Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@loader_path/../Frameworks",
"@loader_path/Frameworks",
);
OTHER_CFLAGS = "$(inherited)";
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited)";
SUPPORTED_PLATFORMS = macosx;
SWIFT_VERSION = 4.0;
TARGET_NAME = DefaultsTests;
};
name = Release;
};
OBJ_4 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = s;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.12;
OTHER_SWIFT_FLAGS = "-DXcode";
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator";
SWIFT_ACTIVE_COMPILATION_CONDITIONS = SWIFT_PACKAGE;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
USE_HEADERMAP = NO;
VALID_ARCHS = x86_64;
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
OBJ_17 /* Build configuration list for PBXNativeTarget "Defaults" */ = {
isa = XCConfigurationList;
buildConfigurations = (
OBJ_18 /* Debug */,
OBJ_19 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
OBJ_2 /* Build configuration list for PBXProject "Defaults" */ = {
isa = XCConfigurationList;
buildConfigurations = (
OBJ_3 /* Debug */,
OBJ_4 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
OBJ_24 /* Build configuration list for PBXNativeTarget "DefaultsPackageDescription" */ = {
isa = XCConfigurationList;
buildConfigurations = (
OBJ_25 /* Debug */,
OBJ_26 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
OBJ_30 /* Build configuration list for PBXAggregateTarget "DefaultsPackageTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
OBJ_31 /* Debug */,
OBJ_32 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
OBJ_35 /* Build configuration list for PBXNativeTarget "DefaultsTests" */ = {
isa = XCConfigurationList;
buildConfigurations = (
OBJ_36 /* Debug */,
OBJ_37 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = OBJ_1 /* Project object */;
}

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Defaults::Defaults"
BuildableName = "Defaults.framework"
BlueprintName = "Defaults"
ReferencedContainer = "container:Defaults.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Defaults::DefaultsTests"
BuildableName = "DefaultsTests.xctest"
BlueprintName = "DefaultsTests"
ReferencedContainer = "container:Defaults.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Defaults::Defaults"
BuildableName = "Defaults.framework"
BlueprintName = "Defaults"
ReferencedContainer = "container:Defaults.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>Defaults-Package.xcscheme</key>
<dict></dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict></dict>
</dict>
</plist>

21
Package.swift Normal file
View File

@ -0,0 +1,21 @@
// swift-tools-version:4.1
import PackageDescription
let package = Package(
name: "Defaults",
products: [
.library(
name: "Defaults",
targets: ["Defaults"]
)
],
targets: [
.target(
name: "Defaults"
),
.testTarget(
name: "DefaultsTests",
dependencies: ["Defaults"]
)
]
)

View File

@ -0,0 +1,124 @@
// MIT License © Sindre Sorhus
import Cocoa
public final class Defaults {
public class Keys {}
public final class Key<T: Codable>: Keys {
fileprivate let name: String
fileprivate let defaultValue: T
init(_ key: String, default defaultValue: T) {
self.name = key
self.defaultValue = defaultValue
}
}
public final class OptionalKey<T: Codable>: Keys {
fileprivate let name: String
init(_ key: String) {
self.name = key
}
}
public subscript<T: Codable>(key: Defaults.Key<T>) -> T {
get {
return UserDefaults.standard[key]
}
set {
UserDefaults.standard[key] = newValue
}
}
public subscript<T: Codable>(key: Defaults.OptionalKey<T>) -> T? {
get {
return UserDefaults.standard[key]
}
set {
UserDefaults.standard[key] = newValue
}
}
public func clear() {
for key in UserDefaults.standard.dictionaryRepresentation().keys {
UserDefaults.standard.removeObject(forKey: key)
}
}
}
// Has to be `defaults` lowercase until Swift supports static subscripts
public let defaults = Defaults()
public extension UserDefaults {
private func _get<T: Codable>(_ key: String) -> T? {
if isNativelySupportedType(T.self) {
return object(forKey: key) as? T
}
guard let text = string(forKey: key),
let data = "[\(text)]".data(using: .utf8) else {
return nil
}
do {
return (try JSONDecoder().decode([T].self, from: data)).first
} catch {
print(error)
}
return nil
}
private func _set<T: Codable>(_ key: String, to value: T) {
if isNativelySupportedType(T.self) {
set(value, forKey: key)
return
}
do {
// Some codable values like URL and enum are encoded as a top-level
// string which JSON can't handle, so we need to wrap it in an array
// We need this: https://forums.swift.org/t/allowing-top-level-fragments-in-jsondecoder/11750
let data = try JSONEncoder().encode([value])
let string = String(data: data, encoding: .utf8)?.dropFirst().dropLast()
set(string, forKey: key)
} catch {
print(error)
}
}
public subscript<T: Codable>(key: Defaults.Key<T>) -> T {
get {
return _get(key.name) ?? key.defaultValue
}
set {
_set(key.name, to: newValue)
}
}
public subscript<T: Codable>(key: Defaults.OptionalKey<T>) -> T? {
get {
return _get(key.name)
}
set {
if let value = newValue {
_set(key.name, to: value)
}
}
}
private func isNativelySupportedType<T>(_ type: T.Type) -> Bool {
switch type {
case is Bool.Type,
is String.Type,
is Int.Type,
is Double.Type,
is Float.Type,
is Date.Type:
return true
default:
return false
}
}
}

View File

@ -0,0 +1,62 @@
import XCTest
@testable import Defaults
let fixtureUrl = URL(string: "httos://sindresorhus.com")!
enum FixtureEnum: String, Codable {
case tenMinutes = "10 Minutes"
case halfHour = "30 Minutes"
case oneHour = "1 Hour"
}
extension Defaults.Keys {
static let key = Defaults.Key<Bool>("key", default: false)
static let url = Defaults.Key<URL>("url", default: fixtureUrl)
static let `enum` = Defaults.Key<FixtureEnum>("enum", default: .oneHour)
}
final class DefaultsTests: XCTestCase {
override func setUp() {
super.setUp()
defaults.clear()
}
func testKey() {
let key = Defaults.Key<Bool>("key", default: false)
XCTAssertFalse(UserDefaults.standard[key])
UserDefaults.standard[key] = true
XCTAssertTrue(UserDefaults.standard[key])
}
func testOptionalKey() {
let key = Defaults.OptionalKey<Bool>("key")
XCTAssertNil(UserDefaults.standard[key])
UserDefaults.standard[key] = true
XCTAssertTrue(UserDefaults.standard[key]!)
}
func testKeys() {
XCTAssertFalse(defaults[.key])
defaults[.key] = true
XCTAssertTrue(defaults[.key])
}
func testUrlType() {
XCTAssertEqual(defaults[.url], fixtureUrl)
let newUrl = URL(string: "https://twitter.com")!
defaults[.url] = newUrl
XCTAssertEqual(defaults[.url], newUrl)
}
func testEnumType() {
XCTAssertEqual(defaults[.enum], FixtureEnum.oneHour)
}
func testClear() {
defaults[.key] = true
XCTAssertTrue(defaults[.key])
defaults.clear()
XCTAssertFalse(defaults[.key])
}
}

9
license Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

175
readme.md Normal file
View File

@ -0,0 +1,175 @@
# Defaults [![Build Status](https://travis-ci.org/sindresorhus/Defaults.svg?branch=master)](https://travis-ci.org/sindresorhus/Defaults)
> Swifty and modern [UserDefaults](https://developer.apple.com/documentation/foundation/userdefaults)
## Highlights
- **Strongly typed:** You declare the type and default value upfront.
- **Codable support:** You can store any [Codable](https://developer.apple.com/documentation/swift/codable) value, like an enum.
- **Debuggable:** The data is stored as JSON-serialized values.
- **Lightweight:** It's only ~100 lines of code.
## Requirements
- macOS 10.12+
- Xcode 9.3+
- Swift 4.1+
## Install
With [SPM](https://swift.org/package-manager/):
```swift
.package(url: "https://github.com/sindresorhus/Defaults", from: "0.1.0")
```
With [Carthage](https://github.com/Carthage/Carthage):
```
github "sindresorhus/Defaults"
```
<a href="https://www.patreon.com/sindresorhus">
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
</a>
## Usage
You declare the defaults keys upfront with type and default value.
```swift
import Cocoa
import Defaults
extension Defaults.Keys {
static let quality = Defaults.Key<Double>("quality", default: 0.8)
// ^ ^ ^ ^
// Key Type UserDefaults name Default value
}
```
You can then access it as a subscript on the `defaults` global (note lowercase):
```swift
defaults[.quality]
//=> 0.8
defaults[.quality] = 0.5
//=> 0.5
defaults[.quality] += 0.1
//=> 0.6
defaults[.quality] = "🦄"
//=> [Cannot assign value of type 'String' to type 'Double']
```
You can also declare optional keys for when you don't want to declare a default value upfront:
```swift
extension Defaults.Keys {
static let name = Defaults.OptionalKey<Double>("name")
}
if let name = defaults[.name] {
print(name)
}
```
### Enum example
```swift
enum DurationKeys: String, Codable {
case tenMinutes = "10 Minutes"
case halfHour = "30 Minutes"
case oneHour = "1 Hour"
}
extension Defaults.Keys {
static let defaultDuration = Defaults.Key<DurationKeys>("defaultDuration", default: .oneHour)
}
defaults[.defaultDuration].rawValue
//=> "1 Hour"
```
### It's just UserDefaults with sugar
This works too:
```swift
extension Defaults.Keys {
static let isUnicorn = Defaults.Key<Bool>("isUnicorn", default: true)
}
UserDefaults.standard[.isUnicorn]
//=> true
```
### Shared UserDefaults
```swift
extension Defaults.Keys {
static let isUnicorn = Defaults.Key<Bool>("isUnicorn", default: true)
}
let extensionDefaults = UserDefaults(suiteName: "com.unicorn.app")!
extensionDefaults[.isUnicorn]
//=> true
```
## API
### `let defaults = Defaults()`
#### Defaults.Keys
Type: `class`
Stores the keys.
#### Defaults.Key
Type: `class`
Create a key with a default value.
#### Defaults.OptionalKey
Type: `class`
Create a key with an optional value.
##### defaults.clear()
Type: `func`
Clear the user defaults.
## FAQ
### How is this different from [`SwiftyUserDefaults`](https://github.com/radex/SwiftyUserDefaults)?
It's inspired by it and other solutions. The main difference is that this module doesn't hardcode the default values and comes with Codable support.
## Related
- [LaunchAtLogin](https://github.com/sindresorhus/LaunchAtLogin) - Add "Launch at Login" functionality to your macOS app
- [DockProgress](https://github.com/sindresorhus/DockProgress) - Show progress in your app's Dock icon
- [Gifski](https://github.com/sindresorhus/gifski-app) - Convert videos to high-quality GIFs on your Mac
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)