https://jumpshare.com/v/Otai3BBXYwfvyz8jb53k
(明智地看这些看项目的结构)
问题:
好吧,所以我跟随tutorial创建了一个带标题的UITableView,然后是单元格内容.
代码运行良好,运行良好,现在我想扩展到该教程之外,并使用alamofire和SwiftyJSON动态加载该内容.
在本教程中,使用的代码如下:
- func getSectionsFromData() -> [Sections] {
- var sectionsArray = [Sections]()
- let animals = Sections(title: "Animals",objects: ["Cats","Dogs","Birds","Lions"])
- sectionsArray.append(animals)
- return sectionsArray
- }
我试图做的是:
- Alamofire.request(.GET,url).validate().responseJSON { response in
- switch response.result {
- case .Success:
- if let value = response.result.value {
- let json = JSON(value)
- for (_,subJson) in json {
- for (year,content) in subJson {
- let title = year
- let objects = content
- sectionsArray.append(Sections(title: title,objects: objects))
- }
- }
- }
- case .Failure(let error):
- print(error)
- }
- }
如果我打印出他们在控制台中显示的结果 – 所以我知道JSON的获取和循环工作.然后我加入了
- let title = year
- let objects = content
- sectionsArray.append(Sections(title: title,objects: objects))
但在这一行:
sectionsArray.append(Sections(title: title,objects: objects))
我收到此错误:
cannot convert value of type ‘JSON’ to expected argument type ‘[String]’
这是我正在使用的JSON:
- {"posts": [
- {
- "Category1": [
- "Post1cat1"
- ],"Category2": [
- "Post1cat2","Post2cat2"
- ]
- }
- ]}
有人能帮我吗?我可能会走错方向在这里我想循环JSON并将类别显示为标题和表格单元格中的帖子.
编辑:2016年1月29日
所以,我把循环改为:
- for (_,subJson) in json {
- for (index,data) in subJson {
- for (title,objects) in data {
- sectionsArray.append(Sections(title: title,objects: objects.self.arrayValue.map { $0.string!}))
- }
- }
- }
仍然没有运气.当我添加一些打印件(在:sectionsArray.append下)来测试是否有数据:
- print("--")
- print(title)
- print(objects.self.arrayValue.map { $0.string!})
- print(Sections(title: title,objects: objects.self.arrayValue.map { $0.string!}))
我在控制台中得到了这个结果:
—
Category1
[“Post1cat1”]
Sections(headings: “Category1”,items: [“Post1cat1”])
—
Category2
[“Post1cat2”,“Post2cat2”]
Sections(headings: “Category2”,items: [“Post1cat2”,“Post2cat2”])
这表明信息存在,但是当我运行应用程序时,仍然没有结果形式他JSON只是最初定义的部分和上面的单元格.
解决方法
- for (title,data) in subJson {
- var elements: [String] = []
- for (_,object) in data {
- if let stringELement = object.rawString() {
- elements.append(stringELement)
- }
- }
- sectionsArray.append(Sections(title: title,objects: elements))
- }
或者如果您愿意,可以使用来自JSON对象的铸造原始数组,如下例所示:
- for (_,subJson) in json["posts"] {
- for (title,data) in subJson {
- let optionalCastedObjects = data.arrayObject as? [String]
- let unwrappedObjects = optionalCastedObjects ?? []
- let section = Sections(title: title,objects: unwrappedObjects)
- sectionsArray.append(section)
- }
- }
这应该修复提到的编译问题.
但最后请记住,您在同步getSectionsFromData方法中使用异步回调(在您的GET请求中).并且你总是会在回调(clojure)的值附加新数据之前返回数组.这将导致您永远不会显示您以这种方式获取的数据.
UPDATE
要做到这一点,你应该重构你的getSectionsFromData方法,如下所示.
- func getSectionsFromData(completion: ([Sections]) -> ()) {
- var sectionsArray = [Sections]()
- Alamofire.request(.GET,subJson) in json["posts"] {
- for (title,data) in subJson {
- let optionalCastedObjects = data.arrayObject as? [String]
- let unwrappedObjects = optionalCastedObjects ?? []
- let section = Sections(title: title,objects: unwrappedObjects)
- sectionsArray.append(section)
- }
- }
- completion(sectionsArray)
- }
- case .Failure(let error):
- print(error)
- }
- }
- }
和UITableViewController类中的相关部分.
- var sections: [Sections] = []
- override func viewDidLoad() {
- super.viewDidLoad()
- SectionsData().getSectionsFromData { [weak self](sections: [Sections]) -> () in
- self?.sections = sections
- self?.tableView.reloadData()
- }
- }