質問編集履歴
1
情報追加と修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
JSONSerialization
|
1
|
+
JSONSerialization.jsonObject(with: data!, ←「with: data!」とは何でしょうか。
|
test
CHANGED
@@ -1,8 +1,24 @@
|
|
1
|
+
前提
|
2
|
+
|
3
|
+
swiftとphpでログイン機能を作っています。
|
4
|
+
|
5
|
+
MAMPのmysqlを使用しています。
|
6
|
+
|
7
|
+
ビルドは成功するのですが、ユーザー登録ができません。
|
8
|
+
|
9
|
+
JSONSerializationの使い方がわかりませんので、教えてください。
|
10
|
+
|
11
|
+
|
12
|
+
|
1
13
|
問題点
|
2
14
|
|
3
|
-
下記コード
|
15
|
+
下記コードにて、ステップインすると、catchへいってしまいます。
|
4
16
|
|
5
|
-
with: data!は
|
17
|
+
デバッグで確認したところ、with: data!はnilになっていました。
|
18
|
+
|
19
|
+
nilが原因なのかなと思うのですが。。。
|
20
|
+
|
21
|
+
|
6
22
|
|
7
23
|
|
8
24
|
|
@@ -24,4 +40,112 @@
|
|
24
40
|
|
25
41
|
|
26
42
|
|
27
|
-
|
43
|
+
こちらが全文です。
|
44
|
+
|
45
|
+
```swift
|
46
|
+
|
47
|
+
// Send HTTP POST
|
48
|
+
|
49
|
+
let myUrl = NSURL(string: "http://localhost/registerUser.php");
|
50
|
+
|
51
|
+
let request = NSMutableURLRequest(url:myUrl! as URL);
|
52
|
+
|
53
|
+
request.httpMethod = "POST";
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
let postString = "userEmail=\(userEmail)&userPassword=\(userPassword)&userFirstName=\(userFirstName)&userLastName=\(userLastName)";
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
request.httpBody = postString.data(using: String.Encoding.utf8);
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
URLSession.shared.dataTask(with: request as URLRequest, completionHandler:{(data:Data?, response:URLResponse?, error:Error?) -> Void in
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
DispatchQueue.main.async {
|
70
|
+
|
71
|
+
if error != nil{
|
72
|
+
|
73
|
+
self.displayAlertMessage(userMessage: (error?.localizedDescription)!)
|
74
|
+
|
75
|
+
return
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
do{
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
let json = try JSONSerialization.jsonObject(with: data!,options: .mutableContainers) as? NSDictionary
|
86
|
+
|
87
|
+
if let parseJSON = json {
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
let userId = parseJSON["userId"] as? String
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
if( userId != nil){
|
96
|
+
|
97
|
+
let myAlert = UIAlertController(title: "Alert",
|
98
|
+
|
99
|
+
message: "Registration successful", preferredStyle: UIAlertControllerStyle.alert);
|
100
|
+
|
101
|
+
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){
|
102
|
+
|
103
|
+
(action) in self.dismiss(animated: true, completion:nil)
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
myAlert.addAction(okAction);
|
108
|
+
|
109
|
+
self.present(myAlert, animated: true, completion: nil)
|
110
|
+
|
111
|
+
}else {
|
112
|
+
|
113
|
+
let errorMessage = parseJSON["message"] as? String
|
114
|
+
|
115
|
+
if(errorMessage != nil){
|
116
|
+
|
117
|
+
self.displayAlertMessage(userMessage: errorMessage!)
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
}catch{
|
128
|
+
|
129
|
+
print(error.localizedDescription)
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
}//dispatch.main.async
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
}).resume()
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
```
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
よろしくお願いします。
|
150
|
+
|
151
|
+
情報不足でしたら、教えていただけると幸いです。
|