XCSnippets

Build

用于提供与(用户自定义)Xcode 代码片段进行类型安全交互的 Swift 包

概述

import XCSnippets

let directory = PersistentCodeSnippetDirectory() // points to ~/Library/Developer/Xcode/UserData/CodeSnippets

// CREATE (or override)
let newSnippet = XCSnippet(title: "MyFirstCodeSnippet", content: "print(\"Hello World\")")
try directory.write(contents: [newSnippet]) // alternative: try newSnippet.write(to: URL.codeSnippetsUserDirectoryURL)

// READ
let existingSnippets: [XCSnippet] = try dir.readContents()

// DELETE
try dir.delete(contents: existingSnippets) // alternative:try dir.delete(contentWithId: newSnippet.id)

示例:如何将远程 .codesnippet 文件复制到本地计算机

try await URLSession.shared.data(from: URL(string: "https://raw.githubusercontent.com/burczyk/XcodeSwiftSnippets/master/swift-forin.codesnippet")!)
    .0
    .toXCSnippet()
    .write(to: .codeSnippetsUserDirectoryURL)

注意:在文件目录 ~/Library/Developer/Xcode/UserData/CodeSnippets 中的程序化更改将被正在运行的 Xcode 应用程序忽略。您需要重启 Xcode 才能在代码片段库中看到更改。