Skip to content

Aeastr/SwiftMacros

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftMacros

A collection of useful Swift macros.

Swift 6.0+ iOS 13+ macOS 10.15+ tvOS 13+ watchOS 6+ visionOS 1+ Tests

Overview

  • #Obfuscate — Compile-time string obfuscation to hide strings from static analysis
  • #previewOnly — Execute code only when running in Xcode Previews
  • #buildConfig — Different values for DEBUG vs RELEASE builds
  • @PublicInit — Generate public memberwise initializer for structs

Installation

dependencies: [
    .package(url: "https://github.com/Aeastr/SwiftMacros.git", from: "1.0.0")
]
import SwiftMacros

Note

Xcode will prompt you to trust macros from this package on first use. Click "Trust & Enable" to proceed.

Usage

#Obfuscate

Hide strings from static analysis tools (strings, hex editors, etc).

let secret = #Obfuscate("MySecretString")

With explicit method:

let secret = #Obfuscate("MySecretString", .xor)       // default, best
let secret = #Obfuscate("MySecretString", .bitShift)  // very good
let secret = #Obfuscate("MySecretString", .reversed)  // good
let secret = #Obfuscate("MySecretString", .base64)    // moderate
let secret = #Obfuscate("MySecretString", .bytes)     // minimal

Caution

This is obfuscation, not encryption. It raises the bar from "trivial" to "annoying" — a determined attacker with a debugger will always win. Never use for API keys or secrets.

#previewOnly

Execute code only when running in Xcode Previews.

#previewOnly {
    viewModel.loadMockData()
}

Expands to the ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" check.

#buildConfig

Different values based on build configuration.

let apiURL = #buildConfig(debug: "http://localhost:3000", release: "https://api.prod.com")
let timeout = #buildConfig(debug: 60, release: 10)

Expands to #if DEBUG / #else / #endif.

@PublicInit

Generate a public memberwise initializer for structs.

@PublicInit
public struct User {
    let id: UUID
    let name: String
    var isActive: Bool = true
}

// Generates:
// public init(id: UUID, name: String, isActive: Bool = true) { ... }

Contributing

Contributions welcome. Please feel free to submit a Pull Request.

License

MIT. See LICENSE for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages