MySql Connection中未解决的自我

时间:2018-12-06 09:43:39

标签: ios swift

我试图通过swift连接到MySqlDB。我在互联网上找到了一个教程,并且逐步地进行了学习(我是Swift的新手),但我无法解决此错误。我尝试在Internet上搜索,但是每个“自我”错误都与另一个错误不同,而且我什么也找不到(也可能我没有能力-.-')。 webServices.php似乎正常工作。我的问题出在Xcode

ViewController.swift

import UIKit
 class ViewController: UIViewController, UITableViewDataSource,     UITableViewDelegate, HomeModelProtocol {

//downloadItems
func itemsDownloaded(items: NSArray) {
    feedItems = items
    self.listTable.reloadData()
}

//link tV
@IBOutlet weak var listTable: UITableView!
//Proprietà
var feedItems: NSArray = NSArray()
var selectList : ListModel = ListModel()



//tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return feedItems.count
}

//viewDidLoad
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //set delegates and initialize a homeModel
    self.listTable.delegate = self
    self.listTable.dataSource = self

    let homeModel = HomeModel ()
    homeModel.delegate = self
    homeModel.downloadItems()
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {
    let cellIdentifier : String = "BasicCell"
    let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!

    //recupero food
    let item : ListModel = feedItems[indexPath.row] as! ListModel

    //reference alla label
    myCell.textLabel!.text = item.description

    return myCell
}}

ListModel.swift

import Foundation
class ListModel : NSObject {

//proprietà
var name: String?
var percentage: Int?
var other: String?
var id: Int?


override init(){
}

init(name: String, percentage: Int, other: String, id: Int){
    self.name = name
    self.percentage = percentage
    self.other = other
    self.id = id
}

//stampa oggetti
override var description: String{
    return "Name: \(name), Percentuale: \(String(describing: percentage)), Altro: \(other)"
}}

HomeModel.swift

import Foundation

protocol HomeModelProtocol: class {
    func itemsDownloaded(items: NSArray)
} 

class HomeModel: NSObject, URLSessionDataDelegate{
//proprietà
weak var delegate: HomeModelProtocol!
var data = Data()
let urlPath: String = "http://www.sake-house.net/webServices.php"

func downloadItems(){
    let url : URL = URL(string: urlPath)!
    let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
    let task = defaultSession.dataTask(with: url) {(data, response, error) in
        if error != nil{
            print("downloasd fallito")
        }else{
            print("Dati scaricati")
            parseJSON(data!)
        }
    }
    task.resume()
}} 
func parseJSON(_ data:Data){
var jsonResult = NSArray()

do{
    jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError{
    print (error)
}

var jsonElement = NSDictionary()
let listFoods = NSMutableArray()

for i in 0..<jsonResult.count {
    jsonElement = jsonResult[i] as! NSDictionary

    let listF = ListModel()

    if let name = jsonElement["Name"] as? String, let percentage = jsonElement["Percentage"] as? Int, let other = jsonElement["other"] as? String, let id = jsonElement["id"] as? Int{
        listF.name = name
        listF.percentage = percentage
        listF.other = other
        listF.id = id
    }
    listFoods.add(listF)

    //check listFoods
    DispatchQueue.main.async(execute: { () -> Void in self.delegate.itemsDownload(items: listFoods)}) <--------HERE

}}//fine func parseJSON

我的问题是最后一个文件的最后一行。 “使用未解决的标识符'self'”

您能帮助我更好地理解我的错误是什么吗?

更新

感谢Joakim,我解决了第一个问题。但这给了我线程1:致命错误:在展开myCell时展开Optional值时意外发现nil

let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!

1 个答案:

答案 0 :(得分:0)

您在班级外面有功能parseJSON,因此self未定义

class HomeModel: NSObject, URLSessionDataDelegate {
    ...

    func downloadItems() {
        ...
    }
} 

func parseJSON(_ data:Data) {
    ...
}

将其移动到HomeModel类定义中

class HomeModel: NSObject, URLSessionDataDelegate {
    ...

    func downloadItems() {
        ...
    }


    func parseJSON(_ data:Data) {
        ...
    }
}