iOS - Swift 模拟网络请求从本地取得假数据 同时创立模型类

  • 时间:2019-06-11 02:24 作者:GA_ 来源:GA_ 阅读:86
  • 扫一扫,手机访问
摘要:show doc文档数据格式在后端没时间造假数据的情况,可尝试使用如下方法。1、模拟网络请求2、上拉加载更多数据3、快速创立模型文件参数fileName: 文件名macName: 电脑客户名,创立好的文件存在桌面的一个文件里localFileName: 桌面生成一个文件目录,存放模型文件classN
show doc文档数据格式

在后端没时间造假数据的情况,可尝试使用如下方法。
1、模拟网络请求
2、上拉加载更多数据
3、快速创立模型文件

参数

fileName: 文件名macName: 电脑客户名,创立好的文件存在桌面的一个文件里localFileName: 桌面生成一个文件目录,存放模型文件className: 类的名字preString: 文件和类的前缀isOptional: 创立的属性能否可选inheritClass: 继承的类名returnCode: 模拟网络请求返回状态isError: 模拟网络请求失败errorMessage: 请求失败返回错误信息deadline: 模拟网络请求推迟时间resultHandler: 成功回调errorHandler: 失败回调

json格式模拟数据

{    "returnCode": 200,    "message": "查询成功",    "dic": {        "a": "",        "b": ""    },    "result": {        "curPage": "1",        "pageSize": "10",        "total": "30",        "isNextPage": "false",        "data": [{                "id": 1,                "dic": {                    "a": "",                    "b": ""                },                "code_string": "活动编码",                "name": "活动名称",                "dic": {                    "a": "",                    "b": ""                }            },            {                "id": 2,                "code": "活动编码",                "name": "活动名称",                "dic": {                    "a": "",                    "b": ""                }            }        ]    }}

将模拟数据贴进文件内,如下图


json文件

工具类文件

////  GALocalTestDataManager.swift//  YYFramework////  Created by houjianan on 2019/3/11.//  Copyright ? 2019 houjianan. All rights reserved.//  模拟网络请求、创立Model文件import Foundationopen class GASimulationDataManager {    static let share = GASimulationDataManager()        var resultData: [String : String] = [String : String]()        public func ga_getLocalData(fileName: String, macName: String = "houjianan", localFileName: String = "ll", className: String = "Test", preString: String = "GA", isOptional: Bool = true, inheritClass: String = "") -> [String : Any] {        guard let pathLocal = Bundle.main.path(forResource: fileName, ofType: "") else {            #if DEBUG            print("fileName 错误")            #endif            return ["":""]        }                let d = try! Data(contentsOf: URL(fileURLWithPath: pathLocal))        let dic = try? JSONSerialization.jsonObject(with: d, options: JSONSerialization.ReadingOptions.mutableContainers)        writeToFile(macName: macName, localFileName: localFileName, dic: dic as! [String : Any], className: className, preString: preString, isOptional: isOptional, inheritClass: inheritClass)        return dic as! [String : Any]    }        public func ga_simulationRequest(fileName: String, macName: String, localFileName: String = "ll", className: String = "Test", preString: String = "GA", isOptional: Bool = true, inheritClass: String, returnCode: Int = 200, isError: Bool = false, errorMessage: String = "模拟错误请求-完成", deadline: Double = 2.0, resultHandler: @escaping (_ data: [String : Any]) -> (), errorHandler: @escaping (_ error: String) -> ()) {        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + deadline) {            if isError {                DispatchQueue.main.async {                    errorHandler(errorMessage)                }            } else {                DispatchQueue.main.async {                    var dic = self.ga_getLocalData(fileName: fileName, macName: macName, localFileName: localFileName, className: className, preString: preString, isOptional: isOptional, inheritClass: inheritClass)                    if var result = dic["result"] as? [String : Any] {                        if let _ = result["isNextPage"], let data = result["data"] as? [Any] {                            result["isNextPage"] = Date().minute / 2 == 0 ? true : false                            let count = data.count                            var newData = [Any]()                            let randomCount = Int(arc4random()) % (count - 1)                            while newData.count < 10 && count != 0 {                                newData.append(data[randomCount])                            }                            result["data"] = newData                            dic["result"] = result                        }                    }                                        if returnCode != 200 {                        dic["returnCode"] = returnCode                    }                                        if isError {                        errorHandler(errorMessage)                        return                    }                                        #if DEBUG                    print(dic)                    #endif                    resultHandler(dic)                }            }        }    }        func _splitData(dicData: [String : Any], className: String, preString: String, key: String, isOptional: Bool = true, inheritClass: String) {        var resultString = ""                for (k, v) in dicData {            // "<#GA"+""+"#"+">"            if let dic = v as? [String : Any] {                let newKey = key + _firstWordsBig(s: k)                resultString += "    var " + k + ": " + preString + className + newKey + "Model" + "?\n"                _splitData(dicData: dic, className: className, preString: preString, key: newKey, isOptional: isOptional, inheritClass: inheritClass)            } else if let arr = v as? [Any] {                let newKey = key + _firstWordsBig(s: k)                resultString += "    var " + k + ": " + "[" + preString + className + newKey + "Model" + "]?\n"                _splitData(arrData: arr, className: className, preString: preString, key: newKey, isOptional: isOptional, inheritClass: inheritClass)            } else if let _ = v as? String {                if isOptional {                    resultString += "    var " + _newClassName(className: k) + ": " + "String?\n"                } else {                    resultString += "    var " + _newClassName(className: k) + ": " + "String = \"\"\n"                }            } else if let _ = v as? Int {                if isOptional {                    resultString += "    var " + _newClassName(className: k) + ": " + "Int?\n"                } else {                    resultString += "    var " + _newClassName(className: k) + ": " + "Int = 0\n"                }            } else {                            }        }                resultData[className + _firstWordsBig(s: key)] = resultString + (inheritClass == "HandyJSON" ? "\n    required init() {}\n": "")    }        func _splitData(arrData: [Any], className: String, preString: String, key: String, isOptional: Bool = true, inheritClass: String) {        for item in [arrData.first] {            if let d = item as? [String : Any] {                _splitData(dicData: d, className: className, preString: preString, key: key, isOptional: isOptional, inheritClass: inheritClass)            } else if let arr = item as? [Any] {                _splitData(arrData: arr, className: className, preString: preString, key: key, isOptional: isOptional, inheritClass: inheritClass)            } else {                #if DEBUG                print("_splitData error")                #endif            }        }    }        public func writeToFile(macName: String = "houjianan", localFileName: String = "ll", dic: [String : Any], className: String, preString: String = "GA", isOptional: Bool = true, inheritClass: String) {        resultData.removeAll()                _splitData(dicData: dic, className: className, preString: preString, key: "", isOptional: isOptional, inheritClass: inheritClass)                let path = "/Users/" + macName + "/Desktop/" + localFileName + "/" + className + ".swift"                if FileManager.default.createFile(atPath: path, contents: nil, attributes: nil) {            let fileHandle = FileHandle.init(forWritingAtPath: path)            let headerData = ("//\n//  \(className).swift\n//  \(Bundle.main.infoDictionary! ["CFBundleName"] as! String)\n//\n//  Created by " + macName + " on \(Date())\n//  Copyright ? 2019 " + macName + ". All rights reserved.\n\n").data(using: String.Encoding.utf8)            fileHandle?.write(headerData!)            var bodyString = (inheritClass == "HandyJSON" ? "import HandyJSON \n\n" : "")            for (k, v) in resultData {                if v.isEmpty {                    continue                }                let newK = (_firstWordsBig(s: k).contains(className) ? "" : className) + _firstWordsBig(s: k)                let classTitle = _firstWordsBig(s: newK + (_firstWordsBig(s: newK).contains("Model") ? "" : "Model")) + (inheritClass.isEmpty ? "" : ": \(inheritClass)")                bodyString += "class " + preString + classTitle + " {\n"                bodyString += v + ""                bodyString += "}\n\n"            }                        let bodyData = bodyString.data(using: String.Encoding.utf8)            fileHandle?.write(bodyData!)        }    }        private func _firstWordsBig(s: String) -> String {        return s.prefix(1).uppercased() + s.sd_sliceString(start: 1, end: s.length)    }        private func _newClassName(className: String) -> String {        let set = CharacterSet.init(charactersIn: "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:\"”“’。,、?]|\n|\r|\t")        var newClassName = className.trimmingCharacters(in: set)        if newClassName == "id" {            newClassName = newClassName.uppercased()        }        let words = newClassName.components(separatedBy: "_")        if words.count > 1 {            var item = ""            for i in 0..<words.count {                if i == 0 {                    item += words[i]                } else {                    item += words[i].prefix(1).uppercased() + words[i].sd_sliceString(start: 1, end: words[i].length)                }            }            newClassName = item        }        return newClassName    }    }extension String {        func sd_sliceString(start: Int, end: Int) -> String {        if self.isEmpty {            return self        }        return sd_sliceString((start...end))    }        func sd_subString(range: Range<String.Index>) -> String {        if self.isEmpty {            return self        }        return String(self[range.lowerBound..<range.upperBound])    }        // 切割字符串(区间范围 (区间范围 前闭后开) )    func sd_sliceString(_ range:CountableRange<Int>) -> String{        guard            let startIndex = sd_validStartIndex(original: range.lowerBound),            let endIndex = sd_validEndIndex(original: range.upperBound),            startIndex <= endIndex            else {                return ""        }                return String(self[startIndex..<endIndex])    }        // 切割字符串(区间范围 (区间范围 前闭后闭) )    func sd_sliceString(_ range:CountableClosedRange<Int>) -> String {        guard            let start_Index = sd_validStartIndex(original: range.lowerBound),            let end_Index = sd_validEndIndex(original: range.upperBound),            startIndex <= endIndex            else {                return ""        }        if(endIndex.encodedOffset <= end_Index.encodedOffset){            return String(self[start_Index..<endIndex])        }        return String(self[start_Index...end_Index])    }        // 校验字符串位置能否正当    func sd_validIndex(original: Int) -> String.Index {        switch original {        case ...startIndex.encodedOffset:            return startIndex        case endIndex.encodedOffset...:            return endIndex        default:            return index(startIndex, offsetBy: original)        }    }    // 校验能否是合法的起始位置    func sd_validStartIndex(original: Int) -> String.Index? {        guard original <= endIndex.encodedOffset else { return nil }        return sd_validIndex(original:original)    }    // 校验能否是合法的结束位置    func sd_validEndIndex(original: Int) -> String.Index? {        guard original >= startIndex.encodedOffset else { return nil }        return sd_validIndex(original:original)    }    }

使用方法如下:

GASimulationDataManager.share.ga_simulationRequest(fileName: "testJson", macName: "houjianan", inheritClass: "HandyJSON", resultHandler: { (dic) in            }) { (error) in    print(error)}
生成的模型文件
  • 全部评论(0)
最新发布的资讯信息
【系统环境|】极客时间-数据分析实战45讲【完结】(2021-09-02 16:26)
【系统环境|windows】字节跳动前台面试题解析:盛最多水的容器(2021-03-20 21:27)
【系统环境|windows】DevOps敏捷60问,肯定有你想理解的问题(2021-03-20 21:27)
【系统环境|windows】字节跳动最爱考的前台面试题:JavaScript 基础(2021-03-20 21:27)
【系统环境|windows】JavaScript 的 switch 条件语句(2021-03-20 21:27)
【系统环境|windows】解决 XML 数据应用实践(2021-03-20 21:26)
【系统环境|windows】20个编写现代CSS代码的建议(2021-03-20 21:26)
【系统环境|windows】《vue 3.0探险记》- 运行报错:Error:To install them, you can run: npm install --save core-js/modules/es.arra...(2021-03-20 21:24)
【系统环境|windows】浅谈前台可视化编辑器的实现(2021-03-20 21:24)
【系统环境|windows】产品经理入门迁移学习指南(2021-03-20 21:23)
血鸟云
手机二维码手机访问领取大礼包
返回顶部