Moya - 如何启用样本数据?
Posted
技术标签:
【中文标题】Moya - 如何启用样本数据?【英文标题】:Moya - How enable sample data? 【发布时间】:2018-06-06 10:49:28 【问题描述】:我开始学习Moya for Netwroking
在实现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)
【问题讨论】:
【参考方案1】:为了在 Moya 中禁用或启用示例数据,我们应该使用所需状态初始化提供程序。
static let provider = MoyaProvider<VMAPI>.init(stubClosure: MoyaProvider<VMAPI>.immediatelyStub)
【讨论】:
【参考方案2】:在 moya 中为 sampleData 尝试此代码
var sampleData: Data
switch self
case .login(_,_):
return "Half measures are as bad as nothing at all.".utf8Encoded
default:
return Data()
【讨论】:
【参考方案3】:如果你想忽略简单数据
var sampleData: Data
return "There is No smaple Data".data(using: String.Encoding.utf8)!
或者如果您想使用简单的数据,只需按照您在问题中所做的操作
【讨论】:
return Data() 对于这种情况就足够了以上是关于Moya - 如何启用样本数据?的主要内容,如果未能解决你的问题,请参考以下文章