我收到“强制施放违反:应避免强制施放警告”
在我的代码上:

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

屏幕截图如下:

ios - 应避免强行施放-LMLPHP
 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?]()

最佳答案

您的问题与“可选”和“unwrapper ”的概念有关。这里简要介绍了两者以及它们之间的区别:How to understand ! and ? in swift?

? (可选)表示您的变量可能包含nil值,而! (解包)表示在运行时使用变量(尝试从中获取值)时,变量必须具有内存(或值)。

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

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

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

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

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

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

共享您的完整源代码,以获取更好的查询解决方案,因为您尚未共享问题中的数组声明。因为我对可选数组[Any?]元素感到困惑。 Swift不允许将数组元素作为可选元素。

更新:通过将所有数组的元素视为'Dictionary ',此处删除了从数组分配中强制展开的内容。
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

09-05 20:22