ModelMapper
模型映射的抽象层,写一次映射器,处处可使用。
用法
// declare Mapper
struct UserDtoMapper: Mapper {
typealias I = UserDto
typealias O = User
func map(_ input: UserDto) -> User {
User(name: input.name, email: input.email)
}
}
// mape UserDto to User
let user: User = UserDtoMapper().map(userDto)
// map [UserDto] to [User]
let input: [UserDto] = [userDto, userDto]
let users: [User] = ListMapper(UserDtoMapper()).map(input)
// map [UserDto]? to [User]
let input: [UserDto]? = nil
let users: [User] = OptionalInputListMapper(UserDtoMapper()).map(input)
// map [UserDto] to [User]?
let input: [UserDto] = []
let users: [User]? = OptionalOutputListMapper(UserDtoMapper()).map(input)
// map [UserDto]? to [User]?
let input: [UserDto]? = nil
let users: [User]? = OptionalListMapper(UserDtoMapper()).map(input)
安装
Swift 包管理器
要使用 Apple 的 Swift 包管理器进行集成,请在您的 Package.swift
中添加以下内容作为依赖项
.package(url: "https://github.com/ShabanKamell/ModelMapper.git", .upToNextMajor(from: "0.1.0"))
然后指定 "ModelMapper"
作为您希望在其中使用 ModelMapper 的目标的依赖项。以下是一个示例 PackageDescription
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(
name: "MyPackage",
targets: ["MyPackage"]),
],
dependencies: [
.package(url: "https://github.com/ShabanKamell/ModelMapper-Swift.git", .upToNextMajor(from: "0.1.0"))
],
targets: [
.target(
name: "MyPackage",
dependencies: ["ModelMapper"])
]
)
Accio
Accio 是基于 SwiftPM 的依赖管理器,可为您构建适用于 iOS/macOS/tvOS/watchOS 的框架。因此,RxRequester 的集成步骤与上述描述完全相同。一旦您配置了 Package.swift
文件,请运行 accio update
而不是 swift package update
。
CocoaPods
对于 RxRequester,请在其 Podfile 中使用以下条目
pod 'ModelMapper-Swift', '~> 0.1.0'
然后运行 pod install
。
在您想使用 RxRequester 的任何文件中,别忘了使用 CocoaPods 时通过 import ModelMapper
或 import ModelMapper_Swift
来导入框架。
Carthage
Carthage 用户可以使用此存储库中生成的 ModelMapper
框架。
请在您的 Cartfile 中添加以下条目
github "ShabanKamell/ModelMapper-Swift" ~> 0.1.0
然后运行 carthage update
。
如果您首次在项目中使用 Carthage,您需要按照 Carthage 中的说明进行一些额外操作。
许可证
点击此处显示许可证
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.