-1
我面对一个问题,我尝试了不同的解决方案,但是我还是无法解决。问题是Instance member 'decodeJson' cannot be used on type 'Utils'
在decodeJson
区域。正如你可以看到的Utils类具有不同的功能,其中getDataWithURLSession
,decodeJson
和getDataWithURLSession
需要使用decodeJson
。实例成员'decodeJson'不能在类型'Utils'上使用
public class Utils {
static var ApiURL = "http://xxx"
var categoryList = [MangaCategory]()
//MARK - JSON Searilization
@available(iOS 10.0, *)
static func getDataWithURLSession(withtoken: String){
guard let url = URL(string: Utils.ApiURL + "/Category/Get") else {return}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer " + withtoken, forHTTPHeaderField: "Authorization")
guard let httpBody = try? JSONSerialization.data(withJSONObject: [], options: []) else { return }
request.httpBody = httpBody
let task = URLSession.shared.dataTask(with: request) { (data, response, err) in
decodeJson(data: data)
}
task.resume()
}
@available(iOS 10.0, *)
private func decodeJson(data:Data?){
guard let data = data else {return}
dump(data)
do {
let json = try JSONDecoder().decode(Categorys.self, from: data)
for manga in json.data{
if json.data.count > categoryList.count
{
let cat = MangaCategory(context: CoreDataService.context)
cat.category_name = manga.name
cat.category_id = Int16(manga.id)!
CoreDataService.saveContext()
self.categoryList.append(cat)
}
else{
print("JSon Serialization Es Geçildi.")
}
}
} catch let jsonError {
print("JSON Serializing error : ", jsonError)
}
}
}
由于添加代码。我试过第一个解决方案,但没有工作所以,我改变了函数的名称 'static func getDataWithURLSession(withtoken:String){'到 'public func getDataWithURLSession(withtoken:String){'和 'private func decodeJson(data:Data?){'到 ' public func decodeJson(data:Data?) - > [Any]'并添加'return categoryList'。其实我的utils类问题已解决,但当我在AppDelegate.swift调用getDataWithURLSession函数时,我看到不同的问题,例如'实例成员'getDataWithURLSession'不能用于类型'Utils';你的意思是使用这种类型的值吗?' – Guray
这两个选项都取决于你将在哪里使用Utils对象和存储属性。为什么第一种方法不起作用?我认为你需要有一个对象,并通过调用getDataWithURLSession来填充它的categoryList。您能否在应用程序代理和其他任何地方显示您使用的代码?我的意思是你将如何使用它。我认为你将它称为类方法,而我们正在试图将它作为一个实例。 – Woof