gpt4 book ai didi

swift 1.2 NSDate?!不可转换为 NSDate

转载 作者:行者123 更新时间:2023-11-28 10:00:04 26 4
gpt4 key购买 nike

升级到1.2后出现编译错误:

NSDate?! is not convertible to NSDate

代码:

let dateCreated = photoCommentObjects[indexPath.row].createdAt as NSDate

其他尝试:

我也试过:

let dateCreated = photoCommentObjects[indexPath.row].createdAt as? NSDate

我得到错误:

Downcast from NSDate?! to 'NSDate' only unwraps optionals

最佳答案

这是一个类型问题。您有一个 AnyObject 类型的数组,您无法读取 AnyObject 的属性。

//sample data
class PhotoComment {
let createdAt = NSDate()
}

let photoCommentObjects: [AnyObject] = [PhotoComment()]


//let's get the indexed object first and let's cast it from AnyObject
let photoComment = photoCommentObjects[indexPath.row] as! PhotoComment
//now trivially
let dateCreated = photoComment.createdAt

//let's cast the whole array first
let photoComments = photoCommentObjects as! [PhotoComment]
let dateCreated = photoComments[indexPath.row].createdAt

关于 swift 1.2 NSDate?!不可转换为 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30138832/

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