swift3


migration encoding:.Custom(marshal)


Because I need to make a json string from a type Array<Array<Double>> instead of Dictionary<String, Any> I need custom encoding and can't use the default json encoding from alamofire 4.
In alamofire 3 I did this:
let marshal: (URLRequestConvertible, [String: AnyObject]?) -> (NSMutableURLRequest, NSError?) = {
(urlRequest, parameters) in
var mutableURLRequest = urlRequest as! NSMutableURLRequest
mutableURLRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
mutableURLRequest.httpBody = self.buffer.json()
return (mutableURLRequest, nil)
}
But I don't know enough swift 3 to be able to understand how I should implement a encoding protocol that I can use in alamofire 4
https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol
Can you provide me with a example please that does implement a alamofire 4 encoding protocol.
Based on https://github.com/Alamofire/Alamofire#custom-encoding I tried:
struct JSONStringArrayEncoding: ParameterEncoding {
private let buffer: Array<Array<Double>>
init(_ buffer: Array<Array<Double>>) {
self.buffer = buffer
}
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var urlRequest = try urlRequest.asURLRequest()
urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = self.buffer.json()
return urlRequest
}
}
encoding:JSONStringArrayEncoding(self.buffer) compiles but doesn't seem to work.
EDIT: Nevermind it works :) did something else stupid not related to this, I will just leave it here to help other people. Feel free to answer if you have a better solution then the above.
suppose your json string is:- {"action":"userLogin","info":{"email":"chandragirish86#gmail.com","password":"qwert123","login":"login"}}
then you should write the dictionary below :-
let jsonObject: [String: Any] = ["action":Urls.loginUrl,
"info":["email":"chandragirish86#gmail.com","password":"qwert123","login":"login"]]
Then you just call the Alamofire post function with json encoding:-
Alamofire.request (url, method: .post, parameters: jsonObject,encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success(_):
if let data = response.data{
let json:JSON = JSON(data: data)
completionHandler(json,nil)
}
break
case .failure(_):
print("URL> \(url)...\(response.result.error as Any)")
completionHandler(nil,response.result.error as NSError?)
break
}
}

Related Links

How to pass text value to MFMailcomposeView Controller
login/register action using Segmented control and switch statement
Disable multiple buttons
How can I associate a particular userID with a particular table view cell?
UITextView text color when Editable attribute is disabled
Returning a substring after a specified character
Infinite UICollection View in View Controller with Swift 3
Swift 3 Add custom annotation pin to MKMapSnapShotter snapshot
Working with UITextFieldDelegate and a custom input container
Update record without using subclass of dataModel
UICollection is very slow in showing Image with Swift 3
Avoiding downcasts in a Swift 3 completion handler with Google Drive REST API
Button visibly clicking but event not registering
Alamofire.request does not map with custom object
Speech Recoginition giving error swift error domain=nsosstatuserrordomain code=2003334207 “(null)”
Twitter Friendship/create api asking for authentication in swift

Categories

HOME
gcc
url
optimization
redux
skypedeveloper
conceptual
azure-database-mysql
cryengine
esxi
aggregate-functions
comments
kohana
meson-build
open-search-server
virtualhost
annyang
cracking
olap
derby
webseal
google-closure-compiler
background-image
ibatis
openoffice-basic
descriptor
formulas
coccinelle
fibonacci
handle
aws-certificate-manager
formtastic
angular2-changedetection
spock
ipython-parallel
spring-data-gemfire
r-factor
postgresql-9.6
incapsula
asp.net-webhooks
respect-validation
lfe
glide-image-library
web-technologies
angular-fullstack
anonymous-function
confluent
history
wicketstuff
msiexec
sqlproj
nsoperationqueue
razorengine
sqlxml
markerspiderfier
google-app-engine-php
github-for-mac
magnet-uri
jcomponent
clearquest
alpha-beta-pruning
launcher
mind-manager
api-eveonline
ios-enterprise
ant-contrib
sigma-grid-control
class-library
starling-framework
thucydides
mobile-robots
ia-32
mosix
wordbreaker
mmo
linear-interpolation
hypertable
django-admin-tools
nserror
mvcmailer
automapper-2
log-shipping
opendir
n2
rollover
database-agnostic
large-data-volumes
mysql-pconnect
dojox.gfx
file-storage
atmega16
moores-law

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App