我开始学习Netwroking的Moya
在实施TargetType
是否有办法不为每个终点提供样本数据
而且,我无法找到启用/禁用样本数据的方法。
var sampleData: Data {
switch self {
case .zen:
return "Half measures are as bad as nothing at all.".utf8Encoded
case .showUser(let id):
return "{\"id\": \(id), \"first_name\": \"Harry\", \"last_name\": \"Potter\"}".utf8Encoded
case .createUser(let firstName, let lastName):
return "{\"id\": 100, \"first_name\": \"\(firstName)\", \"last_name\": \"\(lastName)\"}".utf8Encoded
case .updateUser(let id, let firstName, let lastName):
return "{\"id\": \(id), \"first_name\": \"\(firstName)\", \"last_name\": \"\(lastName)\"}".utf8Encoded
case .showAccounts:
// Provided you have a file named accounts.json in your bundle.
guard let url = Bundle.main.url(forResource: "accounts", withExtension: "json"),
let data = try? Data(contentsOf: url) else {
return Data()
}
return data
}
}
我怎么做?
答案
static let provider = MoyaProvider<VMAPI>.init(stubClosure: MoyaProvider<VMAPI>.immediatelyStub)
答案 0 :(得分:2)
要在Moya中禁用或启用示例数据,我们应该使用必需状态初始化提供程序。
static let provider = MoyaProvider<VMAPI>.init(stubClosure: MoyaProvider<VMAPI>.immediatelyStub)
答案 1 :(得分:0)
如果你想忽略简单数据
var sampleData: Data {
return "There is No smaple Data".data(using: String.Encoding.utf8)!
}
或者如果你想使用简单的数据就像你在问题中那样做
答案 2 :(得分:0)
在moya中为sampleData尝试此代码
var sampleData: Data {
switch self {
case .login(_,_):
return "Half measures are as bad as nothing at all.".utf8Encoded
default:
return Data()
}
}