Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用可编码协议,按照从服务器接收的相同顺序解码json_Ios_Swift4_Codable_Decodable - Fatal编程技术网

Ios 使用可编码协议,按照从服务器接收的相同顺序解码json

Ios 使用可编码协议,按照从服务器接收的相同顺序解码json,ios,swift4,codable,decodable,Ios,Swift4,Codable,Decodable,因此,我使用可编码协议将JSON解析为一个对象。 我现在面临的是,接收json的顺序与在对象中解码json后的顺序不同。ie密钥排序不符合json 我知道这不是一个快速的限制。Swift和JSON字典都是无序的。JSON格式不保证密钥顺序,因此不需要解析器来保持顺序 但是有什么方法可以维护您代码中的顺序吗???在代码中添加MutableOrderedDictionary类: class MutableOrderedDictionary: NSDictionary { let _valu

因此,我使用可编码协议将JSON解析为一个对象。 我现在面临的是,接收json的顺序与在对象中解码json后的顺序不同。ie密钥排序不符合json

我知道这不是一个快速的限制。Swift和JSON字典都是无序的。JSON格式不保证密钥顺序,因此不需要解析器来保持顺序


但是有什么方法可以维护您代码中的顺序吗???

在代码中添加
MutableOrderedDictionary
类:

class MutableOrderedDictionary: NSDictionary {

    let _values: NSMutableArray = []
    let _keys: NSMutableOrderedSet = []

    override var count: Int {
        return _keys.count
    }
    override func keyEnumerator() -> NSEnumerator {
        return _keys.objectEnumerator()
    }
    override func object(forKey aKey: Any) -> Any? {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            return _values[index]
        }
        return nil
    }
    func setObject(_ anObject: Any, forKey aKey: String) {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            _values[index] = anObject
        } else {
            _keys.add(aKey)
            _values.add(anObject)
        }
    }
}
//MARK: - Sort Dictionary Key by the Ascending order
    static func sortDictionaryKey(dictData : [String : Any]) -> MutableOrderedDictionary {

        // initializing empty ordered dictionary
        let orderedDic = MutableOrderedDictionary()
        // copying normalDic in orderedDic after a sort
        dictData.sorted { $0.0.compare($1.0) == .orderedAscending }
            .forEach { orderedDic.setObject($0.value, forKey: $0.key) }
        // from now, looping on orderedDic will be done in the alphabetical order of the keys
        orderedDic.forEach { print($0) }

        return orderedDic
    }
在代码中添加此函数:

class MutableOrderedDictionary: NSDictionary {

    let _values: NSMutableArray = []
    let _keys: NSMutableOrderedSet = []

    override var count: Int {
        return _keys.count
    }
    override func keyEnumerator() -> NSEnumerator {
        return _keys.objectEnumerator()
    }
    override func object(forKey aKey: Any) -> Any? {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            return _values[index]
        }
        return nil
    }
    func setObject(_ anObject: Any, forKey aKey: String) {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            _values[index] = anObject
        } else {
            _keys.add(aKey)
            _values.add(anObject)
        }
    }
}
//MARK: - Sort Dictionary Key by the Ascending order
    static func sortDictionaryKey(dictData : [String : Any]) -> MutableOrderedDictionary {

        // initializing empty ordered dictionary
        let orderedDic = MutableOrderedDictionary()
        // copying normalDic in orderedDic after a sort
        dictData.sorted { $0.0.compare($1.0) == .orderedAscending }
            .forEach { orderedDic.setObject($0.value, forKey: $0.key) }
        // from now, looping on orderedDic will be done in the alphabetical order of the keys
        orderedDic.forEach { print($0) }

        return orderedDic
    }
您需要将字典传递给
sortDictionaryKey
函数:

let sortedDict = self.sortDictionaryKey(dictData: YourDictionary)
此代码将按字母顺序对词典进行排序


希望这对您有用。

在代码中添加
MutableOrderedDictionary
类:

class MutableOrderedDictionary: NSDictionary {

    let _values: NSMutableArray = []
    let _keys: NSMutableOrderedSet = []

    override var count: Int {
        return _keys.count
    }
    override func keyEnumerator() -> NSEnumerator {
        return _keys.objectEnumerator()
    }
    override func object(forKey aKey: Any) -> Any? {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            return _values[index]
        }
        return nil
    }
    func setObject(_ anObject: Any, forKey aKey: String) {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            _values[index] = anObject
        } else {
            _keys.add(aKey)
            _values.add(anObject)
        }
    }
}
//MARK: - Sort Dictionary Key by the Ascending order
    static func sortDictionaryKey(dictData : [String : Any]) -> MutableOrderedDictionary {

        // initializing empty ordered dictionary
        let orderedDic = MutableOrderedDictionary()
        // copying normalDic in orderedDic after a sort
        dictData.sorted { $0.0.compare($1.0) == .orderedAscending }
            .forEach { orderedDic.setObject($0.value, forKey: $0.key) }
        // from now, looping on orderedDic will be done in the alphabetical order of the keys
        orderedDic.forEach { print($0) }

        return orderedDic
    }
在代码中添加此函数:

class MutableOrderedDictionary: NSDictionary {

    let _values: NSMutableArray = []
    let _keys: NSMutableOrderedSet = []

    override var count: Int {
        return _keys.count
    }
    override func keyEnumerator() -> NSEnumerator {
        return _keys.objectEnumerator()
    }
    override func object(forKey aKey: Any) -> Any? {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            return _values[index]
        }
        return nil
    }
    func setObject(_ anObject: Any, forKey aKey: String) {
        let index = _keys.index(of: aKey)
        if index != NSNotFound {
            _values[index] = anObject
        } else {
            _keys.add(aKey)
            _values.add(anObject)
        }
    }
}
//MARK: - Sort Dictionary Key by the Ascending order
    static func sortDictionaryKey(dictData : [String : Any]) -> MutableOrderedDictionary {

        // initializing empty ordered dictionary
        let orderedDic = MutableOrderedDictionary()
        // copying normalDic in orderedDic after a sort
        dictData.sorted { $0.0.compare($1.0) == .orderedAscending }
            .forEach { orderedDic.setObject($0.value, forKey: $0.key) }
        // from now, looping on orderedDic will be done in the alphabetical order of the keys
        orderedDic.forEach { print($0) }

        return orderedDic
    }
您需要将字典传递给
sortDictionaryKey
函数:

let sortedDict = self.sortDictionaryKey(dictData: YourDictionary)
此代码将按字母顺序对词典进行排序


希望这对你有用。

字典是swift中的无序集合。如果您需要与json中相同的顺序,那么应该像解析
String
一样解析它,然后使用标准
String
函数以正确的顺序获取数据,并将其添加到数组中。但是没有
Codable
:(

Dictionary
是swift中的无序集合。如果您需要与json中相同的顺序,您应该像
String
一样解析它,然后使用标准
String
函数以正确的顺序获取数据并将其添加到数组中。但是没有
Codable
:(

如果顺序很重要,它应该是一个数组。否则,在大多数语言中,这样的JSON会有问题。可能会有帮助,或者如果顺序很重要,它应该是一个数组。否则,在大多数语言中,这样的JSON会有问题。可能会有帮助,或者按字母顺序排序字典不是最好的方法与保持原始顺序相同。按字母顺序对词典排序与保持原始顺序不同。