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

Operation not supportedCommand /usr/bin/ditto failed with exit code 1
Any property or method to clear a WKWebiew's cache
IOS 10 - Swift 3.0: Stop the delegated function to save the image
Notifications Not Showing on iPhone Simulator After Implementing Code Apple Provides
AVPlayerViewController does not seem to respect rotation restrictions on full screen mode (XCode 8)
every time i code at least one line of code xcode takes around 3 minutes to build or run
how to play a video from read from gallery in my app in ios
Side menu view controller ios overlap
How to launch interactive shell command from swift3 script?
table view cell issue with dealloc from memory
Trying to get `SecKeyEncrypt` to work with `.OAEP` padding
Add UINavigation Bar with Code and make constraints with Swift 3
Animating individual characters in a SKLabelNode
Reload collectionview thats embedded in dynamic table view
Unsure why we are setting currentQuestionIndex = 0 in this part of the code
Swift 3 Howto parse json from Alamofire into a model

Categories

HOME
asp.net
itunesconnect
process
android-ndk
skypedeveloper
xss
mailchimp
sdk
doctrine2
vsixmanifest
samsung-gear-s2
pyodbc
google-search-appliance
resharper
captcha
drush
hdf5
camunda
android-notifications
loss
civicrm
ampscript
monad-transformers
sasl
knapsack-problem
hierarchical-data
resx
typeorm
spring-data-gemfire
2checkout
drawrect
keil
qt-designer
ilog
dropdownbox
geocomplete
extern
scip
realloc
dotnetnuke-module
byobu
jslider
keychain
google-cloud-powershell
pyexcel
amazon-rds-aurora
amazon-fire-tv
angular-fullstack
synchronized
activeandroid
yank
runner
perldoc
jcifs
photoshop-script
richtext
opticalflow
multi-select
timex
suitesparse
sharing
stripes
foreground
bytearray
nsoperationqueue
laterjs
jde
user-profile
phpdesigner
sqlxml
std
roslyn-code-analysis
winforms-interop
autoscaling
android-filterable
waterline
autopep8
connection-timeout
crystal-reports-7
selenium-grid2
pytz
time-frequency
parallel-port
rpt
nspopupbutton
banana-pi
flask-mongoengine
carrier
complete
google-mirror-api
randomaccessfile
misfire-instruction
wpd
junction-table
gwt2
setcookie
configurationsection
diazo
datasheet
unordered-set
apache-commons-email
clgeocoder
rollover
viewdata
arraycollection
wtsapi32
file-icons
cstring

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