Swift可选语法
我写了一些代码,但使用了一些太多的强制展开。所以我把它修好了,但还有更好的方法Swift可选语法,swift,optional,Swift,Optional,我写了一些代码,但使用了一些太多的强制展开。所以我把它修好了,但还有更好的方法 var currentTask: Tasks? var photoArray: [Photo]? @IBOutlet var photoButton: [TaskImageButton]! photoArray = Array(currentTask?.photos) as? [Photo] if photoArray!.count > 0 { for i in (photoArray?.indice
var currentTask: Tasks?
var photoArray: [Photo]?
@IBOutlet var photoButton: [TaskImageButton]!
photoArray = Array(currentTask?.photos) as? [Photo]
if photoArray!.count > 0 {
for i in (photoArray?.indices)! {
photoButton[i].setImage(UIImage(data: photoArray![i].imageData!), for: .normal)
}
}
我尝试的解决方案:
if let photoArraySize = photoArray?.count {
if photoArraySize > 0 {
for i in (photoArray?.indices)! {
if let photoData = photoArray?[i].imageData {
photoButton[i].setImage(UIImage(data: photoData), for: .normal)
}
}
}
}
展开
photoArray
一次,剩下的代码就简单多了:
if let photoArray = photoArray {
for i in photoArray.indices {
photoButton[i].setImage(UIImage(data: photoArray[i].imageData), for: .normal)
}
}
如果imageData
是可选的,则需要:
if let photoArray = photoArray {
for i in photoArray.indices {
if let imageData = photoArray[i].imageData {
photoButton[i].setImage(UIImage(data: imageData), for: .normal)
}
}
}
这句话:
photoArray = Array(currentTask?.photos) as? [Photo]
可以写成:
photoArray = currentTask?.photos
但是如果没有更多关于所涉及类型的详细信息,就很难确定。一次展开
photoArray
,剩下的代码就简单多了:
if let photoArray = photoArray {
for i in photoArray.indices {
photoButton[i].setImage(UIImage(data: photoArray[i].imageData), for: .normal)
}
}
如果imageData
是可选的,则需要:
if let photoArray = photoArray {
for i in photoArray.indices {
if let imageData = photoArray[i].imageData {
photoButton[i].setImage(UIImage(data: imageData), for: .normal)
}
}
}
这句话:
photoArray = Array(currentTask?.photos) as? [Photo]
可以写成:
photoArray = currentTask?.photos
但是如果没有更多关于所涉及类型的详细信息,就很难确定。如果您先从可选阵列中取出阵列,这可能会更干净 另外,您不必检查count是否大于0,因为如果数组为空(count=0),则for循环甚至不会运行
if let array = photoArray {
for i in array.indices {
photoButton[i].setImage(UIImage(data: array[i].imageData), for: normal)
}
}
我不知道Photo类,但如果imageData也是可选的,那么:
if let array = photoArray {
for i in array.indices {
if let photoData = array[i].imageData {
photoButton[i].setImage(UIImage(data: photoData), for: .normal)
}
}
}
如果您先从可选阵列中取出阵列,这可能会更干净 另外,您不必检查count是否大于0,因为如果数组为空(count=0),则for循环甚至不会运行
if let array = photoArray {
for i in array.indices {
photoButton[i].setImage(UIImage(data: array[i].imageData), for: normal)
}
}
我不知道Photo类,但如果imageData也是可选的,那么:
if let array = photoArray {
for i in array.indices {
if let photoData = array[i].imageData {
photoButton[i].setImage(UIImage(data: photoData), for: .normal)
}
}
}
更好的方法是将照片数组声明为非可选
var photoArray = [Photo]()
...
photoArray = (Array(currentTask?.photos) as? [Photo]) ?? []
...
for (index, photoData) in photoArray.enumerated() where photoData.imageData != nil {
photoButton[index].setImage(UIImage(data: photoData.imageData!), for: .normal)
}
更好的方法是将照片数组声明为非可选
var photoArray = [Photo]()
...
photoArray = (Array(currentTask?.photos) as? [Photo]) ?? []
...
for (index, photoData) in photoArray.enumerated() where photoData.imageData != nil {
photoButton[index].setImage(UIImage(data: photoData.imageData!), for: .normal)
}
当前任务属性
照片
返回的类型是什么?照片
是实体任务
和照片
之间的一对多关系。(一个任务可以有许多照片,不管我的实体名称如何。)当前任务属性photos
返回的类型是什么?photo
是实体Tasks
和photo
之间的一对多关系。(一个任务可以有许多照片,不管我的实体名称如何。)