NSOperation
和 NSOperationQueue
,如 此 WWDC2015 讲座中所述。创建管理器对象,并为其提供 WaniKani API 密钥(可在 WaniKani 个人资料的“设置”中找到)
let manager = WaniApiManager()
manager.setApiKey("0123abc0123abc0123abc") // Pass key here
获取学习队列数据,以下是一个 API 调用示例
manager.fetchStudyQueue { (result) -> Void in
switch result {
case .Error(let error):
print(error())
case .Response(let response):
let resp = response()
if let userInfo = resp.userInfo {
print("userInfo: \(userInfo)")
}
if let studyQueueInfo = resp.studyQInfo {
print("studyQInfo: \(studyQInfo)")
}
}
}
获取水平进度
manager.fetchLevelProgression { (result) -> Void in
switch result {
case .Error(let error):
print(error())
case .Response(let response):
let resp = response()
if let userInfo = resp.userInfo {
print("userInfo: \(userInfo)")
}
if let levelProgressInfo = resp.levelProgression {
print("levelProgression: \(levelProgressInfo)")
}
}
}
如果您需要用户信息
manager.fetchUserInfo { (result) -> Void in
switch result {
case .Error(let error):
print(error())
case .Response(let response):
let resp = response()
if let userInfo = resp {
print(userInfo)
}
}
}
特定级别的部首
manager.fetchRadicalsList(7) { (result) -> Void in
switch result {
case .Error(let error):
print(error())
case .Response(let response):
let resp = response()
if let userInfo = resp.userInfo {
print(userInfo)
}
if let radicals = resp.radicals {
print(radicals)
}
}
}
特定级别的汉字
manager.fetchKanjiList(8) { (result) -> Void in
switch result {
case .Error(let error):
print(error())
case .Response(let response):
let resp = response()
if let userInfo = resp.userInfo {
print(userInfo)
}
if let kanji = resp.kanji {
print(kanji)
}
}
}
swift manager.fetchVocabList(9) { (result) -> Void in switch result { case .Error(let error): print(error()) case .Response(let response): let resp = response() if let userInfo = resp.userInfo { print(userInfo) } if let vocab = resp.vocab { print(vocab) } } }
swift manager.fetchCriticalItems(85) { (result) -> Void in switch result { case .Error(let error): print(error()) case .Response(let response): let resp = response() if let userInfo = resp.userInfo { print(userInfo) } if let criticalItems = resp.criticalItems { print(criticalItems) } } }
管理器使用串行队列进行操作,因此请求将按您调用的顺序执行,并且直到前一个请求完成,没有人会开始。
队列中只能存在同一类型的单个操作,因此如果您对例如三次调用 fetchStudyQueue
,它将只执行一次。
如果您需要同时发送多个相同请求,可以使用两个管理器或在这家公司上提交问题和请求。
WaniKit是在MIT许可证下提供的。有关更多信息,请参阅LICENSE文件。