Introducing JCSwiftCommon, JCSwiftRestful, and JCSwiftUIWedgets

infilachen

James Chen

Posted on September 27, 2024

Introducing JCSwiftCommon, JCSwiftRestful, and JCSwiftUIWedgets

For developers just starting with Swift development, improving development efficiency and writing more standardized and maintainable code is crucial. Therefore, we recommend three excellent Swift framework libraries: JCSwiftCommon, JCSwiftRestful, and JCSwiftUIWedgets. These frameworks are designed to help developers handle common development tasks more easily and simplify the complexity in projects.

JCSwiftCommon
JCSwiftCommon is a foundational library that includes a variety of extension tools commonly used in project development, making the development process more convenient and efficient. It provides multiple extensions for frequently used functionalities, helping developers reduce redundant code while improving code readability and maintainability. Additionally, JCSwiftCommon includes a lightweight local storage tool based on file system I/O, which is especially useful for projects requiring simple data persistence without the need for a full database system.
For example, if we need to store a Person object called me locally, it only takes three steps:

  1. Implement the JCPersistentObject protocol in the Person class or struct and override the persistentId() method:
class Person: JCPersistentObject
Enter fullscreen mode Exit fullscreen mode
persistentId() -> String {
    return self.id
}
Enter fullscreen mode Exit fullscreen mode
  1. Instantiate an object. Here, I define a me object:
let me = Person(name: "James", age: 28)
Enter fullscreen mode Exit fullscreen mode
  1. Store the me object locally:
me.save()
Enter fullscreen mode Exit fullscreen mode

JCSwiftRestful
JCSwiftRestful is designed for network requests in modern application development. This library simplifies the network request process, allowing developers to focus more on handling object-oriented and structured data without worrying too much about the underlying request details. With JCSwiftRestful, you can easily interact with RESTful APIs, making response data handling more intuitive and efficient. (To use this framework, both iOS and server-side code must follow standard RESTful principles.)
You can easily send information to the server in three steps:

  1. There is a default implementation of the JCRequestDataprotocol:
extension JCRequestData {
 var method: JCHttpMethod {
   return .get
 }

 var parameter: Codable? {
   return nil
 }

 var header: [String: String] {
   var header = [String: String]()
   header["Accept"] = "application/json, text/plain, */*"
   header["Accept-Language"] = "en-US,en;q=0.9"
   header["Content-Type"] = "application/json"
   header["source"] = "iOS"
//    if let token = UserManager.shared.userToken, token.count != 0 {
//      header["Authorization"] = "userToken"
//    }
   return header
 }
}

Enter fullscreen mode Exit fullscreen mode
  1. Write client-side code based on the JSON format returned by the server. For example, if the server returns the following JSON string:
{"ip": "24.84.236.255"}

Enter fullscreen mode Exit fullscreen mode

Define a RequestData class that implements the JCRequestData protocol. Here, I define an IpTestRequestData:

private struct IpTestRequestData: JCRequestData {
  struct Response: Codable {
    var ip: String
  }

  var apiPath: String {
    "http://ip.jsontest.com"
  }
}

Enter fullscreen mode Exit fullscreen mode
  1. Send the request and get the response:
Task {
    let result = try? await JCRequestCenter.shared.sendRequest(IpTestRequestData(), decodeType: IpTestRequestData.Response.self)
    print(result?.ip ?? "Error")
}
Enter fullscreen mode Exit fullscreen mode

JCSwiftUIWedgets
JCSwiftUIWedgets includes a set of custom components. Many native SwiftUI methods are not supported on iOS 13 or 14, and these components address compatibility issues with older versions. Additionally, all components support "theme mode," meaning that by changing just one configuration, the entire appearance of the app will adapt accordingly. More details on how to use each component will be introduced later.

💖 💪 🙅 🚩
infilachen
James Chen

Posted on September 27, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related