gpt4 book ai didi

ios - 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'

转载 作者:行者123 更新时间:2023-11-30 12:18:46 25 4
gpt4 key购买 nike

我正在通过 API 获取标签、文本和图像。当API有数据时,显示无误。但是当 API 为 nil 时,它会抛出错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty 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可能有数据,有时没有数据。这是 swift 中 Optionals 的行为。因此,您需要使用可选变量来存储数据。

NSDictionaryNSArray 变量更改为 可选 可能会有所帮助。尝试下面的代码:

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
}
}

关于ios - 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因 : '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45071271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com