如何打开我用于标签的字符串?

时间:2015-10-20 21:59:28

标签: ios json swift optional

我的UI中有标签,它们一直显示为“Optional(xxxxxxx)”。我该如何解决这个问题?

这里我从JSON数据中创建本地对象:

func populateCurrentIssue() {
    if populatingCurrentIssue {
        return
    }

    populatingCurrentIssue = true

    Alamofire.request(GWNetworking.Router.Issue).responseJSON() { response in
        if let JSON = response.result.value {

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {

                var nodeIDArray : [Int]

                if (JSON .isKindOfClass(NSDictionary)) {

                    for node in JSON as! Dictionary<String, AnyObject> {

                        let nodeIDValue = node.0
                        var lastItem : Int = 0

                        if let issueElement : IssueElement = IssueElement(title: "init", nodeID: 0, timeStamp: 0, imageURL: "init", author: "init", issueNumber: "init", volumeNumber: "init", articleContent: "init", coverImageInteger: "init", coverImage: UIImage()) {

                            issueElement.title = node.1["title"] as! String
                            issueElement.nodeID = Int(nodeIDValue)!


                            issueElement.author = String(node.1["author"])

                            lastItem = self.currentIssueObjects.count


                            self.currentIssueObjects.addObject(issueElement)

                            // Sorting with decreasing timestamp from top to bottom.
                            let timestampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)

                            let indexPaths = (lastItem..<self.currentIssueObjects.count).map {
                                NSIndexPath(forItem: $0, inSection: 0) }
                            }
                        }

                    }

                dispatch_async(dispatch_get_main_queue()) {
                    self.currentIssueTableView.reloadData()

                }

            }

        }

        self.populatingCurrentIssue = false
}
}

这里我使用数据填充表格视图单元格:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {

    let row = indexPath.row

    guard let cell = tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as? CurrentIssueArticlesTableViewCell else {

        return tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell
    }

    if let currentIssueObject = currentIssueObjects.objectAtIndex(indexPath.row) as? IssueElement {

        let author = currentIssueObject.author

        let nodeID = currentIssueObject.nodeID ?? 0

        cell.currentIssueArticlesAuthorLabel!.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
        cell.currentIssueArticlesAuthorLabel!.text = author


        else {
                    }
                }
            }
        }

        else {
        }
    }
    return cell
}

d

JSON数据:

"637": {
"title": "Syd Editorial: The Truth Behind Disney Princesses",
"nid": "637",
"type": "article",
"created": "1444923059",
"revision_timestamp": "1444923059",
"disqus": {
"domain": "****domainname*****
"url": "****the API URL***",
"title": "Syd Editorial: The Truth Behind Disney Princesses",
"identifier": "node/637",
"status": false
},
"image_url": null,
"video_url": null,
"author": "Sydney Wilson",
"issue": "Issue 6",
"volume": "Volume 50",
"issue_int": "6",
"volume_int": "50",
"html_content": "<p>Earlier this week I was spending my time perusing facebook and generally wasting valuable time I could be using to better my life and came across a BuzzFeed article a ‘friend’ had shared: Belle Was Literally the Fucking Worst. After immediately unfriending that anonymous friend (sorry, Mom), because I don’t need that kind of negativity in my life, I carried on with my mindless internet clicking. But it nagged at the back of my mind - what could Belle, the princess praised for her smarts, and more importantly, beauty, have done to make her so hated? So I held my breath, bracing myself for the worst kind of bullshit and clicked the clickbait article, like a fish, I was hooked and they reeled me in. I’ve never spent so much time researching Disney Princesses since I wished I was one (who am I kidding, I still wish I was one).&nbsp;<br />\r\n\tGet this, Belle was actually a rich little overdramatic brat who doesn’t care at all who she hurts, and they got all that incredible insight just from the first song. She uses her beauty to get books and bully farm animals - who does she think she is, making fun of poor illiterate sheep. She is horrible to the furniture, who only want to make her stay pleasant, and then has the gall to feel victimized for getting in trouble for ignoring the rest of the giant castle to explore literally the only room she was asked to avoid. Belle is actually the fucking worst, or is she?<br />\r\n\tSo that got me wondering, are all the Disney Princesses actually horrible people in disguise. I can’t imagine there being anything wrong with them at all, but even still I decided to revisit my personal favourite Disney Princess and see if perhaps she wasn’t exactly the role model I thought she was.&nbsp;<br />\r\n\tAriel: &nbsp;“I’m, like, sixteen Dad, I’m an adult. I can do whatever I want.”&nbsp;<br />\r\n\tI’ve heard that once you start agreeing with the parents in Disney films your childhood is officially over. I must have reached that point, because if you really think about it, Ariel is just some whiney guppy who actually has no understanding of how life really works. She misses the party dedicated entirely to her, so she could ‘find treasure’ with her ‘friend’. Looks like someone ditched a family occasion to score some drugs. While high on the latest swamp drug ‘C Weed’ she falls in love with a man who belongs to an entirely different species and is surprised when her dad gets pissed of and destroys her paraphernalia.&nbsp;<br />\r\n\tI’m sure I could go on for pages about how disney princesses are actually shit, and mayhaps one day I will, but fortunately for you today is not that day. Just heed my warning, if you still want to be a disney princess, while I commend you, your priorites are shit and you should probably seek some kind of professional for your mental or drug related issues. &nbsp;&nbsp; &nbsp;</p>",
"article_category": {
"name": "Editorials",
"machine_name": "editorials"
},
"cover_image": "0",
"slider_item": "0",
"tags": [
"Disney",
"Role Models"
]
},

4 个答案:

答案 0 :(得分:2)

访问字典会返回Optional。

node.1["author"]位于String()的{​​{1}}就是这种情况。

解决方案:

populateCurrentIssue()

答案 1 :(得分:0)

我相信你在作者部分获得了一个可选值。如果是这样,那么这样做。

guard let author = currentIssueObject.author as? String else {
print("author not found")
return cell
}

答案 2 :(得分:0)

Optional(someText)问题可以这样解决:

guard let author = currentIssueObject.author else {
    return
}

遗憾的是,当您将包装的字符串分配给.text属性时,您不会收到警告,可能是因为您被允许为其分配nil。

我看到你的代码还有一些问题,让我提出一个替代方案:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {

    let row = indexPath.row

    guard let cell = tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as? CurrentIssueArticlesTableViewCell else {
        return tableView.dequeueReusableCellWithIdentifier(CurrentIssueArticlesTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell
    }

    guard currentIssueObjects.count - 1 > indexPath.row else {
        return cell // There is no safe unwrappy way go get an object from an array, just do a boundaries check
    }

    let currentIssueObject = currentIssueObjects.objectAtIndex(indexPath.row)

    guard let author = currentIssueObject.author,
        nodeID = currentIssueObject.nodeID else {
        return cell
    }

    // Always use ?. instead of !. whenever possible, or unwrap first
    cell.currentIssueArticlesAuthorLabel?.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
    cell.currentIssueArticlesAuthorLabel?.text = author

    return cell
}

还有很多可以改进的代码。我不认为IssueElement author属性应该是一个nillable String,因为你通过init创建它。

同样重要的是:

Alamofire.request(GWNetworking.Router.Issue).responseJSON() { [weak self] response in
     guard strongSelf = self else {
         return // self doesn't exist anymore when the thread returns
     }

答案 3 :(得分:0)

if let author = currentIssueObject.author { //author is unwrapped }

和此:

if let nodeID = currentIssueObject.nodeID { //nodeID is unwrapped }