Icon representing the OpenBytes o project.

o

输出和输入

MIT License Community Chat

什么是 o

o 是一个简单的框架,可以输出到文件、URL、控制台,甚至可以使用 UserNotifications 注册通知。 o 也可以从文件、URL 或控制台获取输入。

o 可以用在哪些地方?

目前,o 可以在 macOS、iOS 和 watchOS 上使用。

示例

o.console (控制台)

o.console.out("Value to print: ", terminator: "") //    (oTests/oTests.swift@7) [testExample()]: Value to print:
o.console.out(o.console.in()) // Type in "???";         (oTests/oTests.swift@8) [testExample()]: Optional("???")

o.file (文件)

let fileContents = 4
let folderPath = "./TestFolder"
let filename = "SomeNumber"

try o.file.createDirectory(path: folderPath)

// Write the value 4, an Int, to a file named `filename` in the `folderPath` directory. Files using o.file are base64Encoded by default.
try o.file.out(fileContents, path: folderPath, filename: filename, base64Encoded: false)

// Asserts
XCTAssertEqual(try o.file.in(path: folderPath, filename: filename), 4)

// Delete the File
try o.file.delete(path: folderPath, filename: filename)

// Assert deletion
XCTAssertThrowsError(try o.file.in(path: folderPath, filename: filename) as Int)

o.url (网址)

struct Post: Codable {
    let userId: Int
    let id: Int
    let title: String
    let body: String
}

// GET Request

let (data, response) = try await o.url.get(
    url: URL(string: "api/posts")!
)

// POST Request

let post = Post(userId: 1, id: 1, title: "First!", body: "")

let (_, response) = try await o.url.post(
    url: URL(string: "api/posts/\(post.id)")!,
    body: try? JSONEncoder().encode(post)
)

print(response)

o.notification (通知)

// Request Notification Authorization 
o.notification.requestAuthorization()

// Set UNUserNotificationCenter.current's delegate to `o.notification.delegate`
o.notification.registerDelegate()

// Schedule a Notification
o.notification.post(
    title: "Hello!",
    subtitle: "o.notification",
    body: "Woo Hoo!",
    trigger: UNTimeIntervalNotificationTrigger(
        timeInterval: 3,
        repeats: false
    )
)