簡體   English   中英

由於未捕獲的異常“ NSRangeException”而終止應用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范圍”

[英]Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'

我正在通過API獲取標簽,文本和圖像。 當API包含data時,它顯示沒有錯誤。 但是,當API為nil時,它將引發錯誤,如下所示:

由於未捕獲的異常“ NSRangeException”而終止應用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范圍”。

如果API提供nil值但要防止崩潰,我需要顯示null。 任何人都可以幫助解決Swift 3中的此問題。代碼如下:

import UIKit
import Alamofire

class Instructors: UIViewController {

    @IBOutlet var fullName: UILabel!
    @IBOutlet var post: UILabel!
    @IBOutlet var descriptionName: UITextView!
    @IBOutlet var instructorImage: UIImageView!

    var dictDataContent:NSDictionary = NSDictionary()
    var dictData:NSArray = NSArray()
    var dictprofile:NSDictionary = NSDictionary()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dictData = (dictDataContent.value(forKey: "instructors") as AnyObject) as! NSArray
        self.dictprofile = (dictData.object(at: 0) as AnyObject) as! NSDictionary
        let url:URL = URL(string: (self.dictprofile.value(forKey: "profile_image")) as! String)!
        SDWebImageManager.shared().downloadImage(with: url, options: [],progress: nil, completed: {[weak self] (image, error, cached, finished, url) in
            if self != nil {
              self?.instructorImage.image = image
            }
        })

        self.fullName.text = dictprofile.value(forKey: "full_name") as! String?
        self.post.text = dictprofile.value(forKey: "designation") as! String?
        self.descriptionName.text = dictprofile.value(forKey: "description") as! String?
    }

}

因此,我從您的問題中了解到您的API可能有數據,有時甚至沒有數據 這是Optionals的行為。 因此,您需要具有用於存儲數據的optional變量。

將您的NSDictionaryNSArray變量更改為optional可能會有所幫助。 請嘗試以下代碼:

class Instructors: UIViewController {
    @IBOutlet var fullName: UILabel!
    @IBOutlet var post: UILabel!
    @IBOutlet var descriptionName: UITextView!
    @IBOutlet var instructorImage: UIImageView!

    var dictDataContent:NSDictionary?
    var dictData:NSArray?
    var dictprofile:NSDictionary?
    override func viewDidLoad() {
        super.viewDidLoad()
        dictData = dictDataContent?.value(forKey: "instructors") as? NSArray
        if let count = dictData?.count, count > 0 {
            dictprofile = dictData?.object(at: 0) as? NSDictionary
        }
        if let urlString = dictprofile?.value(forKey: "profile_image") as? String {
            if let url = URL(string: urlString) {
                SDWebImageManager.shared().downloadImage(with: url, options: [],progress: nil, completed: {[weak self] (image, error, cached, finished, url) in
                    if self != nil {
                        self?.instructorImage.image = image
                    }
                })
            }
        }
        fullName.text = dictprofile?.value(forKey: "full_name") as? String
        post.text = dictprofile?.value(forKey: "designation") as? String
        descriptionName.text = dictprofile?.value(forKey: "description") as? String
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 ***由於未捕獲的異常'NSRangeException'而終止應用程序,原因:'***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范圍 ***由於未捕獲的異常“ NSRangeException”而終止應用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:索引0超出了空NSArray的范圍” 由於未捕獲的異常“ NSRangeException”而終止應用程序,原因:“ ***-[__ NSArray0 objectAtIndex:]:空NSArray的索引2超出范圍 由於未捕獲的異常'NSRangeException'而終止應用程序,原因:'***-[__ NSArray0 objectAtIndex:] 異常“ NSRangeException”,原因:“ ***-[__ NSArray0 objectAtIndex:]:空NSArray的索引0超出范圍” 由於未捕獲的異常“ NSRangeException”而終止應用程序,原因:-[__ NSArrayM objectAtIndex:]:索引0超出了空數組的范圍” 由於未捕獲的異常'NSRangeException'終止應用程序,原因: - [__ NSArrayM objectAtIndex:]:索引0超出空數組的邊界' - 2 由於未捕獲的異常'NSRangeException'而終止應用程序,原因:'***-[__ NSArrayI objectAtIndex:]:索引0超出了空數組的范圍 由於未捕獲的異常'NSRangeException'終止應用程序,原因: - [__ NSArrayM objectAtIndex:]:索引0超出空數組的邊界' 由於未捕獲的異常“ NSRangeException”而終止應用程序,原因:“-[__ NSCFArray objectAtIndex:]:超出范圍(14)的索引(14)”
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM