Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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
SwiftyJSON将字符串数组转换为数组不工作_Json_Swift_Swifty Json - Fatal编程技术网

SwiftyJSON将字符串数组转换为数组不工作

SwiftyJSON将字符串数组转换为数组不工作,json,swift,swifty-json,Json,Swift,Swifty Json,我的数据如下: data = [ "\u65b0\u5317\u5e02\u4e09\u91cd\u5340","\u65b0\u5317\u5e02\u6c38\u548c\u5340", "\u53f0\u5317\u5e02\u4e2d\u5c71\u5340","\u53f0\u5317\u5e02\u4e2d\u6b63\u5340", "\u53f0\u5317\u5e02\u4fe1\u7f

我的数据如下:

data = 
[
"\u65b0\u5317\u5e02\u4e09\u91cd\u5340","\u65b0\u5317\u5e02\u6c38\u548c\u5340",
"\u53f0\u5317\u5e02\u4e2d\u5c71\u5340","\u53f0\u5317\u5e02\u4e2d\u6b63\u5340",
"\u53f0\u5317\u5e02\u4fe1\u7fa9\u5340","\u53f0\u5317\u5e02\u5357\u6e2f\u5340",
"\u53f0\u5317\u5e02\u5927\u540c\u5340","\u53f0\u5317\u5e02\u5927\u5b89\u5340",
"\u53f0\u5317\u5e02\u6587\u5c71\u5340","\u53f0\u5317\u5e02\u677e\u5c71\u5340",
"\u53f0\u5317\u5e02\u842c\u83ef\u5340"
]
但当我想将其转换为数组时,我使用以下代码:

data.array
它总是给我零,我能做什么


我还尝试了
data.arrayValue
data.arrayValue.map{$0.stringValue}

假设您的数据结构是

[
    {
        "data": [
            "\u65b0\u5317\u5e02\u4e09\u91cd\u5340",
            "\u65b0\u5317\u5e02\u6c38\u548c\u5340",
            "\u53f0\u5317\u5e02\u4e2d\u5c71\u5340",
            "\u53f0\u5317\u5e02\u4e2d\u6b63\u5340",
            "\u53f0\u5317\u5e02\u4fe1\u7fa9\u5340",
            "\u53f0\u5317\u5e02\u5357\u6e2f\u5340",
            "\u53f0\u5317\u5e02\u5927\u540c\u5340",
            "\u53f0\u5317\u5e02\u5927\u5b89\u5340",
            "\u53f0\u5317\u5e02\u6587\u5c71\u5340",
            "\u53f0\u5317\u5e02\u677e\u5c71\u5340",
            "\u53f0\u5317\u5e02\u842c\u83ef\u5340"
        ]
    }
]
将JSON转换为实体可编码协议

typealias DistEntity = [Dist]

struct Dist: Codable {
    let data: [String]
}
实现模型层

protocol JSONFetcher: AnyObject {
    func distParser(forResource fileName: String, completionHandler handler: @escaping((Result<DistEntity, Error>) -> ()))
}

class ModelLayer: JSONFetcher {

    enum ParserError: Error {
        case PathNotFound
        case ConvertsObjectError
        case DecoderError
    }

    func distParser(forResource fileName: String, completionHandler handler: @escaping((Result<DistEntity, Error>) -> ())) {
        guard let url = Bundle.main.url(forResource: fileName, withExtension: "json") else { return handler(.failure(ParserError.PathNotFound)) }
        guard let jsonData = try? Data(contentsOf: url) else { return handler(.failure(ParserError.ConvertsObjectError)) }
        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .iso8601
        guard let distEntity: DistEntity = try? decoder.decode(DistEntity.self, from: jsonData) else { return handler(.failure(ParserError.DecoderError)) }
        handler(.success(distEntity))
    }
}
无法确保这对您的场景有用


如果没有解决方案,请您提供更多详细信息。

不确定是否与此相关,因为我不使用SwiftyJSON,但swift字符串中十六进制值的正确格式是
\u{65b0}
您的
数据类型是什么?@OlegBaidalka我的数据类型实际上只是字符串only@Berlin请附上
final class viewModel {

    private var fetcher: JSONFetcher

    init(fetcher: JSONFetcher = ModelLayer()) {
        self.fetcher = fetcher
    }

    private func distParser() {
        self.fetcher.distParser(forResource: "YourJSONFileName") { (result) in
            switch result {
            case .success(let entity):
                print("[Dist Entity]: \(entity)")
            case .failure(let error):
                print(error.localizedDescription)
            }
        }
    }
}