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嗎?那太好了:) –