POST запрос Связь с сервером

swift
xcode
ios

#1

import UIKit

class ViewController: UIViewController, UITableViewDelegate {

//URL to our web service

@IBOutlet weak var textFieldName: UITextField!

@IBOutlet weak var textFieldMember: UITextField!

@IBAction func buttonSave(_ sender: UIButton) {
    
    guard let url = URL(string: "http://172.30.31.250:3306/iosmaster/createteam.php") else {return}
    
    let teamName=textFieldName.text
    let memberCount = textFieldMember.text
    let postParameters = "name="+teamName!+"&member="+memberCount!;
    var request = URLRequest(url:url)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    guard let httpBody = try? JSONSerialization.data(withJSONObject: postParameters, options: [] ) else {return}
    request.httpBody = httpBody
    let session = URLSession.shared
    session.dataTask(with: request) { (data,response,error) in
        if let response = response {
            print(response)
        }
        guard let data = data else {return}
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            print(json)
        } catch {
            print(error)
        }
    }.resume()
}

}

ошибка появляется вот такая2018-09-17 11:56:20.399287+0300 SwiftPHPMySQL[47152:2925457] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write’
*** First throw call stack:
(
0 CoreFoundation 0x0000000110e6a1e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x000000010d25d031 objc_exception_throw + 48
2 CoreFoundation 0x0000000110edf975 +[NSException raise:format:] + 197
3 Foundation 0x000000010cc670a5 +[NSJSONSerialization dataWithJSONObject:options:error:] + 253
4 SwiftPHPMySQL 0x000000010c9463b4 _T013SwiftPHPMySQL14ViewControllerC10buttonSaveySo8UIButtonCF + 2484
5 SwiftPHPMySQL 0x000000010c9472fc _T013SwiftPHPMySQL14ViewControllerC10buttonSaveySo8UIButtonCFTo + 60
6 UIKit 0x000000010dafa3e8 -[UIApplication sendAction:to:from:forEvent:] + 83
7 UIKit 0x000000010dc757a4 -[UIControl sendAction:to:forEvent:] + 67
8 UIKit 0x000000010dc75ac1 -[UIControl _sendActionsForEvents:withEvent:] + 450
9 UIKit 0x000000010dc74a09 -[UIControl touchesEnded:withEvent:] + 580
10 UIKit 0x000000010db6f0bf -[UIWindow _sendTouchesForEvent:] + 2729
11 UIKit 0x000000010db707c1 -[UIWindow sendEvent:] + 4086
12 UIKit 0x000000010db14310 -[UIApplication sendEvent:] + 352
13 UIKit 0x000000010e4556af __dispatchPreprocessedEventFromEventQueue + 2796
14 UIKit 0x000000010e4582c4 __handleEventQueueInternal + 5949
15 CoreFoundation 0x0000000110e0cbb1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
16 CoreFoundation 0x0000000110df14af __CFRunLoopDoSources0 + 271
17 CoreFoundation 0x0000000110df0a6f __CFRunLoopRun + 1263
18 CoreFoundation 0x0000000110df030b CFRunLoopRunSpecific + 635
19 GraphicsServices 0x0000000113801a73 GSEventRunModal + 62
20 UIKit 0x000000010daf9057 UIApplicationMain + 159
21 SwiftPHPMySQL 0x000000010c949407 main + 55
22 libdyld.dylib 0x00000001120f1955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException


#2

Переделайте свой postParameters в Dictionary


#3

‘[Any]’ is not convertible to ‘JSONSerialization.WritingOptions’
вот такую ошибку выдает после options:
guard let httpBody = try? JSONSerialization.data(withJSONObject: Dictionary, options: [] ) else {return}


#4

А вы с юмором ))
Переделайте тип postParameters в Dictionary, а не поменяйте название переменной.