SwiftHelpers

0.1.0

Use Swift to create some amazing things!
leoho0722/SwiftHelpers

What's New

SwiftHelpers v0.1.0

2024-04-19T15:27:18Z

New Features

HTTP

Add HTTP.ContentType.formData to support file upload.
Add HTTP.ContentType.octetStream to support file download.

Rename

HTTP

HTTP.HTTPContentType rename to HTTP.ContentType
HTTP.HTTPHeaderFields rename to HTTP.HeaderFields
HTTP.HTTPMethod rename to HTTP.Method
HTTP.HTTPStatus rename to HTTP.StatusCode

JSON

JSONEncodeError.dictToDataFailed rename to JSONEncodeError.encodingToJSONFailed

Remove

Remove all deprecated initializer、methods

SwiftHelpers

GitHub tag (with filter)

Use Swift to create some amazing things!

Minimum Requirement

  • iOS 15.0 or above
  • macOS 12.0 or above
  • Xcode 14.0 or above

Package Development Environment

  • macOS Sonoma 14.4.1 (23E224)
  • Xcode 15.3 (15E204a)
  • Swift 5.9.2 (5.9.2.2.56)

Install

Currently only installation using Swift Package Manager is supported.

https://github.com/leoho0722/SwiftHelpers.git

Install

How to use

Example: Convert to JSON Data

import Foundation
import SwiftHelpers

func createURLRequest<E>(with url: URL,
                         method: HTTP.Method,
                         parameters: E) -> URLRequest where E: Encodable {
    var request = URLRequest(url: url)
    request.httpMethod = method.rawValue
    request.allHTTPHeaderFields = [
        HTTP.HeaderFields.contentType.rawValue : HTTP.ContentType.json.rawValue
    ]
    
    if method != .get {
        request.httpBody = try? JSON.toJsonData(data: parameters)
    }
    
    return request
}

Example: Safely use SF Symbols with UIKit and AppKit and SwiftUI

UIKit

import SwiftHelpers
import UIKit

class ViewController: UIViewController {
    
    private let imageView = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = UIImage(symbols: .applelogo) // Use SwiftHelpers
        // imageView.image = UIImage(systemName: "applelogo") // Use UIKit
        view.addSubView(imageView)
        
        // ...
    }
}

AppKit

import Cocoa
import SwiftHelpers

class ViewController: NSViewController {
    
    private let imageView = NSImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.image = NSImage(symbols: .applelogo) // Use SwiftHelpers
        // imageView.image = NSImage(systemSymbolName: "applelogo", accessibilityDescription: nil) // Use AppKit
        view.addSubView(imageView)
        
        // ...
    }

    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }
}

SwiftUI

import SwiftHelpers
import SwiftUI

struct ContentView: View {
    
    var body: some View {
        Image(symbols: .applelogo) // Use SwiftHelpers
        // Image(systemName: "applelogo") // Use SwiftUI
        
        Label("Safely use SF Symobols with SwiftUI", symbols: .applelogo) // Use SwiftHelpers
        // Label("Use SF Symobols with SwiftUI", systemImage: "applelogo") // Use SwiftUI
        
        // ...
    }
}

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Nov 05 2024 11:16:57 GMT-1000 (Hawaii-Aleutian Standard Time)