Use Jsonmodel instance code in Swift _ios

Source: Internet
Author: User

Objective

First of all model or the use of OC to write-see this sentence is not to close the page--#, in Swift directly to write the error so will be written by OC, here is mainly to share the experience with Alamofire use.

Body

This does not discuss Jsonmodel and alamofire these two items, directly on the code,BaseModel.h

#import "JSONModel.h"

@interface basemodel:jsonmodel

-(Instancetype) Initwithdictionary: (nsdictionary*) Dict;

@end

basemodel.m

#import "BaseModel.h"

@implementation basemodel

//make all model properties Optional (avoid if possible)
+ (BOOL) propertyisoptional: (nsstring*) PropertyName
{return
  YES;
}

-(Instancetype) Initwithdictionary: (nsdictionary*) dict {return
  (self = [[super init] Initwithdictionary:dict Error:nil]);
}

@end

All model must inherit Basemodel, the other writing is the same

Baseapi.swift

Internal Func requestmodel<t:basemodel> (Method:method, _ urlstring:urlstringconvertible, parameters: [string:a Nyobject]?  = Nil, Success: (T)-> Void, failure: (nserror?)-> Void) {Mhttpmanager.request (method, urlstring, Parameters: Parameters, Encoding:ParameterEncoding.JSON). Responsejson {(Request, response, data, error) in if error = = Nil {if let dict = data as?
              Nsdictionary {if Let model = T (dictionary:dict as [Nsobject:anyobject]) {success (model) Return}} failure (Error)}} internal Func requestarray< T:basemodel> (Method:method, _ urlstring:urlstringconvertible, Parameters: [string:anyobject]? = Nil, success: (ARRA y<t>)-> void, failure: (nserror?)-> void) {Mhttpmanager.request (method, URLString, Parameters:paramet ERS, Encoding:ParameterEncoding.JSON). Responsejson {(Request, response, data, error)In if error = = Nil {if let array = data as? Nsarray {if let result = t.arrayofmodelsfromdictionaries (array as [Anyobject]). Copy () as? array<t>{success (Result) return}} failure (err OR)}}

Code description

1, Mhttpmanager This is Alamofire Manager object

2, pay attention to the return of the service end of the data format, here support Model and array<model>

3, notice in Swift inside Nsdictionary turn model, with T (dictionary:dict as [Nsobject:anyobject]), this t is the specific generic type

4. Note Nsarray the model array in Swift with T.arrayofmodelsfromdictionaries (array as [Anyobject]). Copy () as? Array<t>, be careful not to use Basemodel. Arrayofmodelsfromdictionaries (compilation does not complain but the type does not turn out)

5. Specific usage:

 Public func Casts (success: (array<custommodel>)-> void, failure: (nserror?)-> void) {
        Requestarray ( Method.get, Url_casts, Parameters:nil, success:success, failure:failure)
      } public
      
      func like (id:string, succes S: (Custommodel)-> void, failure: (nserror?)-> void) {
        Requestmodel (Method.patch, String (format:url_casts_ Like, id), Parameters:nil, success:success, failure:failure)
      }

The above is the use of Jsonmodel instance code in swift, with the need for friends to refer to.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.