简体   繁体   English

我要过滤的数组元素不存在时的问题

[英]Problem when an array element i'm trying to filter for does not exist

I have some code that gets a key from firebase, and compares it with a key stored locally in a text file. 我有一些代码从firebase获取密钥,并将其与本地存储在文本文件中的密钥进行比较。 If there is a match, it checks an additional read/unread parameter, and then marks a variable as "Read" or "Unread". 如果存在匹配项,它将检查其他已读/未读参数,然后将变量标记为“已读”或“未读”。

The code works fine as long as every key I pull from Firebase exists in the text file. 只要我从Firebase提取的每个键都存在于文本文件中,该代码就可以正常工作。 However, I get an "Index out of bounds" crash if a key from Firebase does not exist in my local file. 但是,如果本地文件中不存在Firebase的密钥,则会出现“索引超出范围”崩溃。

The code in question is as per below: 有问题的代码如下:

func readFile(){

    let filename = getDocumentsDirectory().appendingPathComponent("output.txt")
    do {

        let textStringFromFile = try String(contentsOf: filename, encoding: .utf8)

        var result: [(messageID: String, readStatus: String)] = []

        let rows = textStringFromFile.components(separatedBy: "\n")
        for row in rows {
            let columns = row.components(separatedBy: ",")
            result.append((columns[0], columns[1]))
        }


        let filteredMessageID = result.filter { $0.messageID == (nominationKeyForReadStatus) }

        var messageIDElement = filteredMessageID.map { $0.messageID }
        var readStatusElement = filteredMessageID.map { $0.readStatus }

        if readStatusElement[0] == "1" && nominationKeyForReadStatus == messageIDElement[0] {

            readStatusFromFunction = "Read"
        }
        else {
            readStatusFromFunction = "Unread"
        }
    }
    catch{

        }

    }

The crash seems to happen when filteredMessageID has 0 values. 当filteredMessageID具有0值时,似乎发生了崩溃。 How do I account for this case? 如何处理这种情况? Thanks in advance. 提前致谢。

You can make a pre check before accessing the index for messageIDElement , readStatusElement and filteredMessageID that it not having count 0. 您可以在访问messageIDElementreadStatusElementfilteredMessageID的索引之前进行预先检查, readStatusElement确认其没有计数0。

if filteredMessageID.count != 0 {
   if readStatusElement.count != 0 && messageIDElement != 0 {
    // Your code
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当数组尚不存在时,如何在 Redux 中的 reducer 中向数组添加元素? - How do I add an element to array in reducer in Redux when the array does not yet exist? (VHDL)尝试从阵列输出时收到错误 - (VHDL) I'm Receiving an Error When Trying to Output from an Array 数组值不显示长度,但当我打开时元素存在 - array value not showing length, but when i open so the element is exist 我正在尝试为用户创建一个程序来输入 5 个数字并检查它们在数组中是否连续,但我遇到了问题 - I'm trying to create a program for the user to enter 5 numbers and check if they are consecutive in an array but I have a problem 在 Swiftui 中,我有一个 @Published 数组。 我在数组上使用.filter,但收到成员元素不存在的错误 - In Swiftui, I have an array that is @Published. I use .filter on the array but get error that member elements does not exist 我正在尝试通过访问嵌套属性并检查它的属性值是否包含某个字符串来过滤数组中的项目 - I'm trying to filter items from array by accessing a nested property & checking if it property value contains a certain string 我正在尝试使用 getElementsByClassName 来 select 一个 canvas 元素,但我能得到的只是一个元素数组 - I'm trying to select a canvas element using getElementsByClassName, but all I can get is an array of the elements 我无法理解 Array.prototype.filter() 中的“元素”参数 - I'm having trouble understanding the “element” parameter in Array.prototype.filter() 如果数组元素不存在则返回 null - Return null if array element does not exist 我试图在数组中得到一个随机元素,为什么我的简单代码不起作用? - I'm trying to get a random element in an array, why wont my simple code work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM