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 set session & cookie and store it and use it with Alamofire , Swift 3
How to construct url in swift 3 using URL(string: , relativeTo:)
Swift 3: NSNotification vs Notification
How do I find where this video is recorded to?
Trying to get swift package example to work
swift 3 - HTTPCookieStorage cookies for url cannot call
Is it possible to create a custom Calendar.Identifier
Animated iMessage App Graphics
How to reference multiple rows with Swift 3 UIPickerView?
Extra argument 'method' in call - XCode 8 / Swift 3.0 / iOS 10.0
How can I incorporate both iOS 9.3 & iOS 10 codes into my App?
respondToSelector / performSelector with parameter from a string in Swift 3
fatal error: Index out of range (Swift 3)
some error occur : The operation couldn’t be completed. ObjectMapper failed to serialize response
when i run my application i will give this error [duplicate]
Alamofire Swift 3 - Extra Argument in Call Error

Categories

HOME
gcc
kendo-ui-angular2
swift3
teradata
typo3
abc
popover
opencv4android
conceptual
itext7
pandoc
singleton
autocad
comments
rethinkdb
vsixmanifest
theory
activejdbc
jetbrains
xmlhttprequest
mbed
wallet
google-calendar-api3
caldav
computation-theory
polybase
screen-scraping
windowserror
poisson
libxml2
angular-schema-form
spring-data-gemfire
jboss-arquillian
mplab
keil
contextmenustrip
eclipse-jdt
favorites
aweber
abide
realloc
motion
directadmin
html-entities
infovis
geminabox
jcs
audio-fingerprinting
fckeditor
liferay-ide
cc
glade
eclipse-mars
dbi
deck.js
vga
magick.net
hfp
php-java-bridge
mrunit
window.location
mod
treelist
javapackager
consul-template
vspackage
touch-typing
static-files
nsq
launcher
kss
mind-manager
xmlserializer
system.net
worklight-appcenter
parallel-port
new-operator
code-complete
friendly-id
exi
zepto
nsregularexpression
webpage-screenshot
hypertable
vectorwise
peoplepicker
jexcelapi
subdirectories
newspeak
uccapi
cstring
httpbrowsercapabilities
cross-cutting-concerns

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