应避免强制施放

时间:2017-06-28 12:17:16

标签: ios swift casting optional

我得到"强制施放违规:强制施法应该避免警告" 在我的代码上:

daysCombinedFinal = daysCombined[0] as! [Any?]

屏幕截图如下:

The warning shown is in the image

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if  let cell = tableView.dequeueReusableCell(withIdentifier: "location", for: indexPath) as? TravelShopCustomCell {
        if !isFirstTime && self.nameArray.count != 0 {
            var daysCombined = [Any]()
            var daysCombinedFinal = [Any?]()
            daysCombined = [self.combinedArray[0]]
            daysCombinedFinal = daysCombined[0] as? [Any?] ?? []
            let str = daysCombinedFinal.flatMap { $0 as? String }.joined(separator:" ")
            var startAMCombined = [Any]()
            var startAMCombinedFinal = [Any?]()
            startAMCombined = [self.combinedStartAMArray[0]]
            startAMCombinedFinal = startAMCombined[0] as? [Any?] ?? []
            var endPMCombined = [Any]()
            var endPMCombinedFinal = [Any?]()
            endPMCombined = [self.combinedEndPMArray[0]]
            endPMCombinedFinal = endPMCombined[0] as? [Any?] ?? []
            cell.operatingDaysLabel.text = str
            let isAMEqual = checkArrayStatus(testArray: [startAMCombinedFinal as Any])
            let isPMEqual = checkArrayStatus(testArray: [endPMCombinedFinal as Any])
            if isAMEqual && isPMEqual {
                self.mergedArray = arrayMerger(array1: startAMCombinedFinal, array2: endPMCombinedFinal)
            }
            let startTimeString = self.mergedArray[0] as? String
            let endTimeString = self.mergedArray[1] as? String
            cell.operatingTimeLabel.text = startTimeString! + " - " + endTimeString!
            return cell
        } else if isFirstTime && self.nameArray.count != 0 {
            var daysCombined = [Any]()
            var daysCombinedFinal = [Any?]()
            daysCombined = [self.combinedArray[indexPath.row]]
            daysCombinedFinal = daysCombined[0] as! [Any?]
            let str = daysCombinedFinal.flatMap { $0 as? String }.joined(separator:" ")
            var startAMCombined = [Any]()
            var startAMCombinedFinal = [Any?]()
            startAMCombined = [self.combinedStartAMArray[indexPath.row]]
            startAMCombinedFinal = startAMCombined[0] as! [Any?]
            var endPMCombined = [Any]()
            var endPMCombinedFinal = [Any?]()
            endPMCombined = [self.combinedEndPMArray[indexPath.row]]
            endPMCombinedFinal = endPMCombined[0] as! [Any?]
            cell.operatingDaysLabel.text = str
            let isAMEqual = checkArrayStatus(testArray: [startAMCombinedFinal as Any])
            let isPMEqual = checkArrayStatus(testArray: [endPMCombinedFinal as Any])
            if isAMEqual && isPMEqual {
                self.mergedArray = arrayMerger(array1: startAMCombinedFinal, array2: endPMCombinedFinal)
            }
            let startTimeString = self.mergedArray[0] as? String
            let endTimeString = self.mergedArray[1] as? String
            cell.operatingTimeLabel.text = startTimeString! + " - " + endTimeString!
            return cell
        }
        return cell
    } else {
        fatalError("Dequeueing SomeCell failed")
    }
}

数组声明是:

var dateArray = [Any]()
var endAmTimeArray = [Any]()
var endPmTimeArray = [Any]()
var startAmTimeArray = [Any]()
var startPmTimeArray = [Any]()
var combinedArray = [Any]()
var combinedStartAMArray = [Any]()
var combinedEndPMArray = [Any]()
var mergedArray = [Any?]()

1 个答案:

答案 0 :(得分:2)

您的问题与' 可选'的概念有关。 &安培; '解包&#39 ;.以下是关于它们的简要介绍:How to understand ! and ? in swift?

  

<强>? (可选)表示您的变量在时可能包含零值! (unwrapper)表示你的变量在运行时使用(尝试从中获取值)时必须有一个内存(或值)。

在您的情况下,您尝试使用索引号从数组中获取值。现在,您的数组元素的类型是任何&#39;

因此,数组元素可能包含任何类型的值/信息。如果您尝试强制解包元素的值,当它不会返回一个您使用unwrapper进行转换的类型的值或值时,会导致应用程序崩溃。

Here is basic tutorial in detail, by Apple Developer Committee.

此警告表示您的应用可能因强行展开可选值而崩溃。

作为解决方案,您应该使用 if-let 阻止(可选),以避免强制解包和安全执行代码,例如:

if let daysC = daysCombined[0] as? [Any] {
    daysCombinedFinal = daysC
}

分享您的完整源代码,以便更好地解决您的查询,因为您没有在您的问题中共享您的数组声明。因为我对可选数组[Any?]元素感到困惑。 Swift不允许将数组元素作为可选项。

更新:将所有数组的元素视为&#39; Dictionary&lt;字符串:任何&gt;&#39;,从这里删除数组赋值的强制解包。

var daysCombined = [Any]()
var daysCombinedFinal = [Any?]()
daysCombined = [self.combinedArray[indexPath.row]]

// Update 1
// if combinedArray is an array of array
if let arrayElement = daysCombined[0] as? [Any] {
    daysCombinedFinal = arrayElement
}
let str = daysCombinedFinal.flatMap { $0 as? String }.joined(separator:" ")
var startAMCombined = [Any]()
var startAMCombinedFinal = [Any?]()
startAMCombined = [self.combinedStartAMArray[indexPath.row]]

// Update 2
if let arrayElement = startAMCombined[0] as? [Any] {
    startAMCombinedFinal = arrayElement
}
var endPMCombined = [Any]()
var endPMCombinedFinal = [Any?]()
endPMCombined = [self.combinedEndPMArray[indexPath.row]]

// Update 3
if let arrayElement = endPMCombined[0] as? [Any] {
    endPMCombinedFinal = arrayElement
}

cell.operatingDaysLabel.text = str