I'm getting a very weird error, while unwrapping an optional. When I print the value, I can actually see it but yet, it crashes and it says:
我解决了一个非常奇怪的错误,同时打开了一个可选项。当我打印该值时,我实际上可以看到它,但它崩溃并且它说:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
致命错误:在展开Optional值时意外发现nil
Here's the code (fetchPost()
is called in the viewDidLoad()
:
这是代码(在viewDidLoad()中调用fetchPost():
func fetchPost() {
Api.Posts.observePostIncludingImages(withId: postId) { (post, photosStringArray) in
self.isLiked(postId: post.id!, completion: { boolValue in
Api.Posts.fetchCount(postId: post.id!, completion: { totalCount in
post.isLiked = boolValue
self.photosArrayString = photosStringArray
print(photosStringArray) // prints the actual image string
self.setScrollView()
})
})
}
}
func setScrollView() {
for i in stride(from: 0, to: photosArrayString.count, by: 1) {
var frame = CGRect.zero
frame.origin.x = self.galleryScrollView.frame.size.width * CGFloat(i)
frame.origin.y = 0
frame.size = self.galleryScrollView.frame.size
galleryScrollView.isPagingEnabled = true
let newUIImageView = UIImageView()
print(photosArrayString[i]) // Prints the value again
let myImage = UIImage(named: photosArrayString[i])! // CRASHES! i = 0 (first item in the array)
avgBackgroundColorsOfImage.append(myImage.averageColor!)
newUIImageView.image = myImage
newUIImageView.frame = frame
newUIImageView.contentMode = UIViewContentMode.scaleAspectFit
newUIImageView.sd_setImage(with: URL(string: self.photosArrayString[i]), completed: nil)
galleryScrollView.backgroundColor = avgBackgroundColorsOfImage[i]
galleryScrollView.addSubview(newUIImageView)
self.galleryScrollView.contentSize = CGSize(width: self.galleryScrollView.frame.size.width * CGFloat(photosArrayString.count),
height: self.galleryScrollView.frame.size.height)
pageControl.addTarget(self, action: #selector(changePage), for: UIControlEvents.valueChanged)
}
}
1 个解决方案
#1
3
The thing you are unwrapping is the optional UIImage
you just created. Whatever string is being held in the array is a valid string. It is not, however, the name of a valid image in your asset catalog. Note the error is about "unwrapping a nil value", not "array out of bounds" - these are different things.
你要展开的东西是你刚刚创建的可选UIImage。无论数组中保存的是什么字符串都是有效的字符串。但是,它不是资产目录中有效图像的名称。注意错误是关于“展开零值”,而不是“数组越界” - 这些是不同的东西。
The docs on this initializer show how it's an optional init that returns "the image object for the specified file, or nil if the method could not find the specified image."
这个初始化程序的文档显示了它是一个可选的init,它返回“指定文件的图像对象,如果方法找不到指定的图像则为nil”。
#1
3
The thing you are unwrapping is the optional UIImage
you just created. Whatever string is being held in the array is a valid string. It is not, however, the name of a valid image in your asset catalog. Note the error is about "unwrapping a nil value", not "array out of bounds" - these are different things.
你要展开的东西是你刚刚创建的可选UIImage。无论数组中保存的是什么字符串都是有效的字符串。但是,它不是资产目录中有效图像的名称。注意错误是关于“展开零值”,而不是“数组越界” - 这些是不同的东西。
The docs on this initializer show how it's an optional init that returns "the image object for the specified file, or nil if the method could not find the specified image."
这个初始化程序的文档显示了它是一个可选的init,它返回“指定文件的图像对象,如果方法找不到指定的图像则为nil”。