Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 致命错误:索引超出范围(尝试将图像加载到表视图时)_Json_Swift - Fatal编程技术网

Json 致命错误:索引超出范围(尝试将图像加载到表视图时)

Json 致命错误:索引超出范围(尝试将图像加载到表视图时),json,swift,Json,Swift,我正在尝试从url加载图像以下是代码: import UIKit class TripsTableViewCell : UITableViewCell { @IBOutlet weak var tripName: UILabel! @IBOutlet weak var tripImage: UIImageView! } //let urlString = "http://127.0.0.1:8000/v1/trips/" class TripsTableViewCo

我正在尝试从url加载图像以下是代码:

import UIKit

class TripsTableViewCell : UITableViewCell
{

    @IBOutlet weak var tripName: UILabel!
    @IBOutlet weak var tripImage: UIImageView!
}
    //let urlString = "http://127.0.0.1:8000/v1/trips/"

class TripsTableViewController: UITableViewController {
var myIndex = 0
var trips = [Trip]()
var tripNameArray = [String]()
var tripImageArray = [String]()



override func viewDidLoad() {
    super.viewDidLoad()
    self.newGetFunction()

    /*let trip0:Trip = Trip(id: 1, name: "Dead Sea", location: "Amman", hostedBy: "Tarek", time: "8:00", spokenLanguage: "English", maxPeople: 25, meetingPoint: "7th Circle", image: "https://ichef-1.bbci.co.uk/news/976/cpsprodpb/75E7/production/_89938103_deadseadrone.jpg")
    let trip1:Trip = Trip(id: 1, name: "Aaqaba", location: "Amman", hostedBy: "Tarek", time: "8:00", spokenLanguage: "English", maxPeople: 25, meetingPoint: "7th Circle", image: "https://ichef-1.bbci.co.uk/news/976/cpsprodpb/75E7/production/_89938103_deadseadrone.jpg")


    let trip2:Trip = Trip(id: 1, name: "Maeen", location: "Amman", hostedBy: "Tarek", time: "8:00", spokenLanguage: "English", maxPeople: 25, meetingPoint: "7th Circle", image: "https://ichef-1.bbci.co.uk/news/976/cpsprodpb/75E7/production/_89938103_deadseadrone.jpg")


    let trip3:Trip = Trip(id: 1, name: "Petra", location: "Amman", hostedBy: "Tarek", time: "8:00", spokenLanguage: "English", maxPeople: 25, meetingPoint: "7th Circle", image: "https://ichef-1.bbci.co.uk/news/976/cpsprodpb/75E7/production/_89938103_deadseadrone.jpg")


    let trip4:Trip = Trip(id: 1, name: "Jerash", location: "Amman", hostedBy: "Tarek", time: "8:00", spokenLanguage: "English", maxPeople: 25, meetingPoint: "7th Circle", image: "https://ichef-1.bbci.co.uk/news/976/cpsprodpb/75E7/production/_89938103_deadseadrone.jpg")

    let trip5:Trip = Trip(id: 1, name: "Um Qais", location: "Amman", hostedBy: "Tarek", time: "8:00", spokenLanguage: "English", maxPeople: 25, meetingPoint: "7th Circle", image: "https://ichef-1.bbci.co.uk/news/976/cpsprodpb/75E7/production/_89938103_deadseadrone.jpg")


    let trip6:Trip = Trip(id: 1, name: "Amman", location: "Amman", hostedBy: "Tarek", time: "8:00", spokenLanguage: "English", maxPeople: 25, meetingPoint: "7th Circle", image: "https://ichef-1.bbci.co.uk/news/976/cpsprodpb/75E7/production/_89938103_deadseadrone.jpg")

    trips.append(trip0)
    trips.append(trip1)
    trips.append(trip2)
    trips.append(trip3)
    trips.append(trip4)
    trips.append(trip5)
    trips.append(trip6)

    print("HERE")
    print(trips)
    */
    tableView.delegate = self;
    tableView.dataSource = self;
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source


override func numberOfSections(in tableView: UITableView)-> Int{
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return tripNameArray.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TripsTableViewCell
    cell.tripName.text = tripNameArray[indexPath.row]
    //cell.tripImage.downloadImage(from: self.tripImageArray[indexPath.row])
   let imgURL = NSURL(string: tripImageArray[indexPath.row])
    if imgURL != nil{
        let data = NSData(contentsOf: (imgURL as URL?)!)
        cell.tripImage.image = UIImage(data: data! as Data)
    }

    //Nart's Work

    /*cell.tripImage.image = UIImage(named: trips[indexPath.row].Image)
    let t = trips[indexPath.row]
    cell.tripName.text = t.Name
    print(indexPath.row)
    print(trips[indexPath.row].Name)*/

    return cell
}


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
    NSLog("%d",myIndex)
    performSegue(withIdentifier: "segue", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}

func newGetFunction()
{
    let url = URL (string: "http://127.0.0.1:8000/v1/trips/")
    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error != nil
        {
            print ("ERROR")
        }
        else
        {
            if let content = data
            {
                do
                {
                    //Array
                    let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    print(myJson)
                    var myJsonArray = (myJson as! NSArray) as Array
                    for trip in myJsonArray
                    {
                        if let tripDict = trip as? NSDictionary{
                            if let name = tripDict.value(forKey: "name"){
                                self.tripNameArray.append(name as! String)
                            }
                            if let name = tripDict.value(forKey: "image"){
                                if let imgName = (name as? String){
                                self.tripImageArray.append((imgName as? String)!)
                            }
                            }
                            OperationQueue.main.addOperation ({
                                self.tableView.reloadData()
                            })
                        }
                    }
                }
                catch
                {
                }
            }
        }
    }
    task.resume()
}
}

我在这行上遇到一个错误:-

   let imgURL = NSURL(string: tripImageArray[indexPath.row])
错误是:-

1) PAC提取失败,错误为[NSURErrorDomain:-1003]

2) []nw_代理_解析器_创建_解析_数组PAC求值错误:NSURErrorDomain:-1003


3) 致命错误:索引超出范围

您试图将
tripImageArray
中的每个项目显示到表视图中,但对于
numberOfRowsInSection
函数,您返回的是
tripNameArray
的计数。显然,您的
tripNameArray
数组的元素数比
tripImageArray
多,因此出现了此错误


您需要从它们中选择一个作为表视图的计数。

您试图将
tripImageArray
中的每个项目显示到表视图中,但对于
numberOfRowsInSection
函数,您返回的是
tripNameArray
的计数。显然,您的
tripNameArray
数组的元素数比
tripImageArray
多,因此出现了此错误


您需要从它们中选择一个作为表视图的计数。

tripImageArray和tripNameArray的大小不同,请将newGetFunction的部分更改为:

        for trip in myJsonArray
                            {
                                if let tripDict = trip as? NSDictionary{
                                    if let name = tripDict.value(forKey: "name"){
                                        self.tripNameArray.append(name as! String)
                                        if let name = tripDict.value(forKey: "image"){
                                           if let imgName = (name as? String){
                                              self.tripImageArray.append((imgName as? String)!)
                                           }
                                           else{
                                             // append default image 
                                         self.tripImageArray.append("default-img.jpg")
                                            }
                                     }
                                     else{
                                           // append default image 
                                           self.tripImageArray.append("default-img.jpg")
                                    }
                                    OperationQueue.main.addOperation ({
                                        self.tableView.reloadData()
                                    })
                                }
                            }

tripImageArray和tripNameArray的大小不同,请将newGetFunction的部分更改为:

        for trip in myJsonArray
                            {
                                if let tripDict = trip as? NSDictionary{
                                    if let name = tripDict.value(forKey: "name"){
                                        self.tripNameArray.append(name as! String)
                                        if let name = tripDict.value(forKey: "image"){
                                           if let imgName = (name as? String){
                                              self.tripImageArray.append((imgName as? String)!)
                                           }
                                           else{
                                             // append default image 
                                         self.tripImageArray.append("default-img.jpg")
                                            }
                                     }
                                     else{
                                           // append default image 
                                           self.tripImageArray.append("default-img.jpg")
                                    }
                                    OperationQueue.main.addOperation ({
                                        self.tableView.reloadData()
                                    })
                                }
                            }
self.tripImageArray.append((名称为?字符串)!)错误:无法将类型为“NSNull”(0x10e274918)的值强制转换为“NSString”(0x10d471c60)。self.tripImageArray.append((名称为?字符串)!)错误:无法将类型为“NSNull”(0x10e274918)的值强制转换为“NSString”(0x10d471c60)。谢谢!(Y) 我所做的是删除数据库中的所有内容,然后再次添加它们,以确保数据库中的所有列都包含数据!非常感谢。(Y) 我所做的是删除数据库中的所有内容,然后再次添加它们,以确保数据库中的所有列都包含数据!