这是我第一次将iOS应用程序连接到数据库,所以这对我来说都是新手。
所有代码终于正常运行,并且没有任何错误。但是当我运行应用程序然后尝试用正在读取信息的tableview打开VC时,它冻结了,我得到了这个:
0x100030610 <+0>: sub sp, sp, #0x20 ; =0x20
0x100030614 <+4>: stp x29, x30, [sp, #0x10]
0x100030618 <+8>: add x29, sp, #0x10 ; =0x10
0x10003061c <+12>: str x0, [sp, #0x8]
0x100030620 <+16>: mov x0, x1
0x100030624 <+20>: mov x1, x2
0x100030628 <+24>: bl 0x100041688 ; symbol stub for: swift_conformsToProtocol
0x10003062c <+28>: str x0, [sp]
0x100030630 <+32>: cbz x0, 0x100030648 ; <+56>
0x100030634 <+36>: ldr x0, [sp, #0x8]
0x100030638 <+40>: ldr x1, [sp]
0x10003063c <+44>: ldp x29, x30, [sp, #0x10]
0x100030640 <+48>: add sp, sp, #0x20 ; =0x20
0x100030644 <+52>: ret
-> 0x100030648 <+56>: brk #0x1
这是什么意思?
为什么阻止我加载tableview?
这是我要加载的VC:
override func viewDidLoad() {
super.viewDidLoad()
self.parksTable.delegate = self
self.parksTable.dataSource = self
// For Data
let homeModel = HomeModel()
homeModel.delegate = self as! HomeModelProtocol
homeModel.downloadItems()
}
// For Data
func itemsDownloaded(items: NSArray) {
feedItems = items
self.parksTable.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return feedItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier: String = "parkCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
// Get the location to be shown
let item: LocationModel = feedItems[indexPath.row] as! LocationModel
// Get references to labels of cell
myCell.textLabel!.text = item.parkName
return myCell
}
这是针对数据:
lass LocationModel: NSObject {
//properties
var parkID: Int?
var parkName: String?
var parkLatitude: String?
var parkLongitude: String?
var parkDescription: String?
//empty constructor
override init()
{
}
//construct with @name, @address, @latitude, and @longitude parameters
init(parkID: Int, parkName: String, parkLatitude: String, parkLongitude: String, parkDescription: String) {
self.parkID = parkID
self.parkName = parkName
self.parkLatitude = parkLatitude
self.parkLongitude = parkLongitude
self.parkDescription = parkDescription
}
//prints object's current state
override var description: String {
return "Park Name: \(parkName), Latitude: \(parkLatitude), Longitude: \(parkLongitude), Park Description: \(parkDescription)"
}
}
这也适用于数据:
class HomeModel: NSObject, URLSessionDataDelegate{
weak var delegate: HomeModelProtocol!
var data = Data()
let urlPath: String = "http://gamingkc.com/service.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("Failed to download data")
}else {
print("Data downloaded")
self.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 parks = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let park = LocationModel()
//the following insures none of the JsonElement values are nil through optional binding
if let parkName = jsonElement["Park Name"] as? String,
let parkLatitude = jsonElement["Latitude"] as? String,
let parkLongitude = jsonElement["Longitude"] as? String,
let parkDescription = jsonElement["Park Description"] as? String
{
park.parkName = parkName
park.parkLatitude = parkLatitude
park.parkLongitude = parkLongitude
park.parkDescription = parkDescription
}
parks.add(park)
}
DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items: parks)
})
}