当我尝试在我的iPhone上检查互联网连接时,我收到了一堆错误.任何人都可以帮我解决这个问题吗?
代码:
import Foundation
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
var flags: SCNetworkReachabilityFlags = 0
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == 0 {
return false
}
let isReachable = (flags & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection) ? true : false
}
}
Run Code Online (Sandbox Code Playgroud)
代码中的错误:

如果不可读,错误1说:
'Int'不能转换为'SCNetworkReachabilityFlags'
错误2和3:
找不到接受提供的参数的'init'的重载
我的应用程序应该去特定的位置来下载它需要加载的网站.在2.3它的工作就像一个魅力,但因为我已经更新了xcode(我没有很多经验)它给我错误"类型'任何'没有下标成员"并突出显示"json"就在第三行之前
...Retriever = json["WEB"]...
Run Code Online (Sandbox Code Playgroud)
这是与之相关的代码.
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments)
if let Retriever = json["WEB"] as? [[String: AnyObject]] {
for website in Retriever {
if let name = website["URL"] as? String {
self.loadAddressURL(name)
Run Code Online (Sandbox Code Playgroud)
我觉得我错过了一些小事.如果有更好的方法,我会喜欢建议.URL返回此JSON
{
"WEB" : [
{
"URL" : "http://www.google.com"
}
]
}
Run Code Online (Sandbox Code Playgroud)
但如果我能简化它,我会喜欢它
{"URL":"http://www.google.com"}
Run Code Online (Sandbox Code Playgroud) 有没有人能够找到一种方法来解析Swift 3中的JSON文件?我已经能够返回数据但是在将数据分解为特定字段时我没有成功.我会发布示例代码,但我已经通过这么多不同的方法失败了,并没有保存任何.我要解析的基本格式是这样的.提前致谢.
{
"Language": {
"Field":[
{
"Number":"976",
"Name":"Test"
},
{
"Number":"977",
"Name":"Test"
}
]
}
}
Run Code Online (Sandbox Code Playgroud) 嗨,我正在使用post and get响应方法,它工作良好,但是有两个问题:1-当响应为get时,我无法使用警报; 2-在另一个变化的响应中,我无法获得api_token和Id
这是我用于发布的代码**我使用的是本地代码,但是您可以更改网址并对此进行测试**
var request = URLRequest(url: URL(string: "http://172.16.15.137:8888/TheoryTipo/public/api/register")!)
request.httpMethod = "POST"
let postString = "name=\(usernameforsignup.text!)&email=\(emailforsignup.text!)&password=\(passwordforsignup.text!)&tel=\(phonenumberforsignup.text!)"
print(postString)
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
// check for fundamental networking error
print("error=\(error)")
let alertController = UIAlertController(title: "Error", message: "can't Connect to the server", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "retry", style: UIAlertActionStyle.default)
{
(result : UIAlertAction) -> Void in
}
alertController.addAction(okAction)
self.present(alertController, …Run Code Online (Sandbox Code Playgroud) 我是解析 JSON 和使用 Restful API 的新手。Whatevs,我一直在寻找这个问题,我发现了很多。但我想不通。
我要解析的 JSON 是:http : //jsonplaceholder.typicode.com/users
guard let url = URL(string: "http://jsonplaceholder.typicode.com/users/") else {return}
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error ?? "")
}
if data != nil {
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableLeaves)
guard let idk = json as? [Dictionary<String, Any>] else {return}
print(idk)
}
catch{
print(error)
}
}
}.resume()
Run Code Online (Sandbox Code Playgroud)
我来到这里,我无法继续前进。例如,我想访问 id、name 和 username,但我不知道该怎么办了。我想知道如果我想深入研究 Adress 的字典怎么办?
我有这个JSON:
{
cover = {
id = 1;
};
description = "Test"
place = {
id = 11;
location = {
city = Wheatley;
};
name = "Wheatley Provincial Park";
};
},
{
cover = {
id = 2;
};
description = "Cool"
place = {
id = 22;
location = {
city = Wheatley;
};
name = "Wheatley Provincial Park";
};
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
if let fbData = result as? [String : Any] {
print(fbData)
for events in fbData { …Run Code Online (Sandbox Code Playgroud) 我的swift程序一直出现这个错误,我使用PHP和MySQL作为数据库.我将数据从数据库显示到tableview.它以前工作但经过几次运行到模拟器后,我一直有同样的错误
class Home: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var btnaddclass: UIButton!
@IBOutlet var tableView: UITableView!
var values: NSArray = []
func get(){
let url = NSURL(string: "http://localhost/show_db.php")
let data = NSData(contentsOf: url! as URL)
values = try! JSONSerialization.jsonObject(with: data! as Data, options:JSONSerialization.ReadingOptions.mutableContainers)as! NSArray
tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return values.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SpecialCell
let maindata …Run Code Online (Sandbox Code Playgroud) 我是JSON的新手.我做了一些阅读但我仍然感到困惑,我得到了错误.
你如何解析以下数据?
[{"symbol":"ALI","date":"1/22/2018","open":44.9000,"high":45.5000,"low":44.9000,"close":45.2000,"bid":45.1500,"ask":45.2000,"volume":6698800,"value":303245610.0000,"netForeign":-42279365.0000}]
Run Code Online (Sandbox Code Playgroud)
我想分别得到每个字符串.
例如:符号,日期等
任何帮助将不胜感激.谢谢!
作为参考,我尝试了以下解决方案但得到了一个错误: 如何在swift 3中解析Json对象
在我的Swift3代码中,我有一个数组:
var eventParams =
[ "fields" :
[ "photo_url":uploadedPhotoURL,
"video_url":uploadedVideoURL
]
]
Run Code Online (Sandbox Code Playgroud)
后来我想在这个数组中添加另一个数组,我想我可以做到:
eventParams["fields"]["user_location"] = [
"type":"Point", "coordinates":[appDelegate.longitude, appDelegate.latitude]
]
Run Code Online (Sandbox Code Playgroud)
但我在这里得到错误:
Type Any? has no subscript members
Run Code Online (Sandbox Code Playgroud)
如何将该数组添加到我之前声明的数组fields?
swift ×8
ios ×5
json ×5
swift3 ×3
xcode ×2
http-post ×1
parsing ×1
reachability ×1
swift3.0.2 ×1
xcode8 ×1