Swift-从字典[String:Any]中删除键和值

问题描述 投票:0回答:2

我正在尝试从数据库中抓取的词典[String:Any]中删除阻止用户。首先,我获取当前用户已阻止的UID列表:

var updatedBlockList: Any?
func findBlockUsers() {
    // find current users list of blocked users
    guard let currentUserUid = Auth.auth().currentUser?.uid else { return }

    let blockedUsers = Database.database().reference().child("users").child(currentUserUid)

    blockedUsers.observeSingleEvent(of: .value, with: { (snapshot) in
        guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
        userIdsDictionary.forEach({ (key, value) in
            guard let userDictionary = value as? [String: Any] else { return }

            var blockedList : Any
            blockedList = userDictionary.keys

            print(blockedList)

            self.updateBlockList(blockedList: blockedList)

        })
    })
}

func updateBlockList(blockedList: Any) {
    updatedBlockList = blockedList
    print(updatedBlockList)
}

如果我打印updatedBlockList,则会得到:[“ gdqzOXPWaiaTn93YMJBEv51UUUn1”,“ RPwVj59w8pRFLf55VZ6LGX6G2ek2”,“ xmigo8CPzhNLlXN4oTHMpGo7f213”]

我现在想获取那些UID(这将是UserIdsDictionary中的键,并在拉所有用户之后将其删除:

fileprivate func fetchAllUserIds() {

    let ref = Database.database().reference().child("users")
        ref.observeSingleEvent(of: .value, with: { (snapshot) in
        guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }

        userIdsDictionary.forEach({ (key, value) in

            // attempting to remove the blocked users here without any luck
            var updatedKey = key as String?
            updatedKey?.remove(at: self.updatedBlockList as! String.Index)
            print(updatedKey!)

            guard let  userDictionary = value as? [String: Any] else { return }

            let user = User(uid: key, dictionary: userDictionary)
            self.fetchPostsWithUser(user: user)
        })

    }) { (err) in
        print("Failed to fetch following user ids:", err)
    }

}

尝试删除时收到此错误:无法将类型'Swift.Dictionary.Keys'(0x1de2f6b78)的值强制转换为'Swift.String.Index'

我确定我会采用错误的方式,但是我知道我已经接近了。最终目标是获取被阻止用户的UID并将其从词典中删除。任何帮助将不胜感激!

swift string dictionary any
2个回答
0
投票

您在userIdsDictionary上的forEach循环在这里是错误的方法,因此与其尝试修复该代码,我不使用固定方法并在updatedBlockList上循环

for item in updatedBlockList {
    if let userID = item as? String {
        userIdsDictionary.removeValue(forKey: userID)
    }
}

0
投票

对于任何想知道的人,这是为了使其工作而进行的最终更改。

var updatedBlockList = [String]()

func findBlockUsers() {
    // find current users list of blocked users
    guard let currentUserUid = Auth.auth().currentUser?.uid else { return }

    let blockedUsers = Database.database().reference().child("users").child(currentUserUid)

    blockedUsers.observeSingleEvent(of: .value, with: { (snapshot) in
        guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
        userIdsDictionary.forEach({ (key, value) in
            guard let userDictionary = value as? [String: Any] else { return }

            let blockedList = Array(userDictionary.keys)

            print(blockedList)

            self.updateBlockList(blockedList: blockedList)

        })
    })
}

func updateBlockList(blockedList: [String]) {
    updatedBlockList = blockedList
    print(updatedBlockList)
}

fileprivate func fetchAllUserIds() {

    let ref = Database.database().reference().child("users")
        ref.observeSingleEvent(of: .value, with: { [weak self] (snapshot) in
        guard var userIdsDictionary = snapshot.value as? [String: Any], let self = self else { return }

        for item in self.updatedBlockList {
            userIdsDictionary.removeValue(forKey: item)
        }

        userIdsDictionary.forEach({ (key, value) in

            guard let  userDictionary = value as? [String: Any] else { return }
            let user = User(uid: key, dictionary: userDictionary)
            self.fetchPostsWithUser(user: user)
        })
    }) { (err) in
        print("Failed to fetch following user ids:", err)
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.