swift nil判断 字典转模型 afn封装

  1nil,空字符串
解决办法一:OC

#define UENSStringIsEmpty(str) ((str==nil)||[str isEqualToString:@"(null)"]||([str length]==0)||[str isEqualToString:@""])

解决办法二:swift

//value 是AnyObject类型是因为有可能所传的值不是String类型,有可能是其他任意的类型。
func UEStringIsEmpty(value: AnyObject?) -> Bool {
    //首先判断是否为nil
    if (nil == value) {
        //对象是nil,直接认为是空串
        return true
    }else{
        //然后是否可以转化为String
        if let myValue  = value as? String{
            //然后对String做判断
            return myValue == "" || myValue == "(null)" || 0 == myValue.count
        }else{
            //字符串都不是,直接认为是空串
            return true
        }
    }
}

  2字典转模型

/// 字典 -> 模型
///
/// - Parameters:
///   - type: 类型
///   - data: 字典数据
/// - Returns: 模型结果
/// - Throws: 错误处理
func JSONModel(_ type: T.Type, withKeyValues data:[String:Any]) throws -> T where T: Decodable {
    let jsonData = try JSONSerialization.data(withJSONObject: data, options: [])
    let model = try JSONDecoder().decode(type, from: jsonData)
    return model
}

/// 字典数组 -> 模型数组
///
/// - Parameters:
///   - type: 类型
///   - datas: 字典组数
/// - Returns: 模型数组结果
/// - Throws: 错误处理
func JSONModels(_ type: T.Type, withKeyValuesArray datas: [[String:Any]]) throws -> [T]  where T: Decodable {
    var temp: [T] = []
    for data in datas {
        let model = try JSONModel(type, withKeyValues: data)
        temp.append(model)
    }
    return temp
}

  3afn封装

static let shareInstance : UENetworkManager = {
        
        let baseUrl = NSURL(string: "xxx")!
        let manager = UENetworkManager.init(baseURL: baseUrl as URL, sessionConfiguration: URLSessionConfiguration.default)
        manager.requestSerializer.setValue("application/json", forHTTPHeaderField: "Accept")
        manager.responseSerializer.acceptableContentTypes = NSSet(objects: "text/plain", "text/json", "application/json","text/javascript","text/html", "text/xml") as? Set
        return manager
    }()
/**
     get请求
     
     - parameter urlString:  请求的url
     - parameter parameters: 请求的参数
     - parameter success:    请求成功回调
     - parameter failure:    请求失败回调
     */
    class func get(urlString:String,parameters:AnyObject?,success:((_ responseObject:AnyObject?) -> Void)?,failure:((_ error:NSError) -> Void)?) -> Void {
        
        UENetworkManager.shareInstance.get(urlString, parameters: parameters, progress: { (progress) in }, success: { (task, responseObject) in
            
            //如果responseObject不为空时
            if responseObject != nil {
                success!(responseObject as AnyObject?)
            }
            
        }, failure: { (task, error) in
            
            failure!(error as NSError)
            
        })
    }
/**
     post请求
     
     - parameter urlString:  请求的url
     - parameter parameters: 请求的参数
     - parameter success:    请求成功回调
     - parameter failure:    请求失败回调
     */
    class func post(urlString:String,parameters:AnyObject?,success:((_ responseObject:AnyObject?) -> Void)?,failure:((_ error:NSError) -> Void)?) -> Void {
        UENetworkManager.shareInstance.post(urlString, parameters: param, progress: { (progress) in
            
        }, success: { (task, responseObject) in
            
            //如果responseObject不为空时
            if responseObject != nil {
                success!(responseObject as AnyObject?)
            }
            
        }) { (task, error) in
            
            failure!(error as NSError)
        }
        
        
    }

你可能感兴趣的:(swift nil判断 字典转模型 afn封装)