import UIKit
let cellid = "cell"
class Post {
var videoName: String?
var videoDescription: String?
}
class VideoFeedController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var posts = [Post]()
var json: [Any]?
var names: [String] = []
var contacts: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let CatagoryMain = Post()
CatagoryMain.videoDescription = "example text"
CatagoryMain.videoName = "wewewewew"
posts.append(CatagoryMain)
collectionView?.backgroundColor = UIColor.white
navigationItem.title = "Main Video Feed"
collectionView?.alwaysBounceVertical = true
collectionView?.register(VideoFeedCell.self, forCellWithReuseIdentifier: cellid)
let urlString = "example url"
let url = URL(string: urlString)
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print("failed")
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
let currentVideo = parsedData["video"] as! [String:Any]
if let currentVideoTitle = currentVideo["title"] as? String {
print(currentVideoTitle)
// have text display instead of wewewew and example text
}
} catch let error as NSError {
print(error)
}
}
}.resume()
}
所以现在它说视频描述的示例文本和我们wewewewew视频名称和那些显示罚款。我试图从json获取信息,虽然所以而不是示例文字那就说明和代替wewewewew那就我认为这会是这样的在Swift中不显示JSON文本
if let currentVideoTitle = currentVideo["title"] as? String {
print(currentVideoTitle)
CatagoryMain.videoDescription = "\(currentVideoTitle)"
CatagoryMain.videoName = "\(currentVideoTitle)"
posts.append(CatagoryMain)
}
然而这是错误的,视频名称,如果我这样做,没有任何显示在屏幕上,除了导航栏和标题为导航栏。如果您可以提供帮助,请提前致谢!顺便说一下,实际的标题确实显示在控制台中。
所以有行'print(currentVideoTitle)'的任何输出?还可以发布'currentVideo' json吗?那太好了:) –