gpt4 book ai didi

ios - 如何正确解开 UILabels 中使用的变量?

转载 作者:行者123 更新时间:2023-11-30 13:59:52 25 4
gpt4 key购买 nike

编辑1:在帖子底部添加了我的修订版 cellForRowAtIndexPath 代码

编辑2:添加了我的新EditorialElement代码

我很难正确展开 UILabel 文本输入,因此我的所有文本都显示“可选(作者姓名)”(例如,这是报纸的应用程序)。我尝试以不同的方式强制解开我的变量,但无法使其工作。

我的 UILabels 的文本输入是通过以下方式创建的。对应的类是“EditorialElement”,它有以下属性定义:

class EditorialElement: NSObject {

var title: String! // title
var nodeID: Int? // nid
var timeStamp: Int // revision_timestamp
var imageURL: String? // image_url
var author: String? // author

var issueNumber: String! // issue_int
var volumeNumber: String! // volume_int

var articleContent: String! // html_content

/* To get an NSDate objec from Unix timestamp
var date = NSDate(timeIntervalSince1970: timeStamp) */

init(title: String, nodeID: Int, timeStamp: Int, imageURL: String, author: String, issueNumber: String, volumeNumber: String, articleContent: String) {
self.title = title
self.nodeID = nodeID
self.timeStamp = timeStamp
self.imageURL = imageURL
self.author = author
self.issueNumber = issueNumber
self.volumeNumber = volumeNumber
self.articleContent = articleContent
}

override func isEqual(object: AnyObject!) -> Bool {
return (object as! EditorialElement).nodeID == self.nodeID
}

override var hash: Int {
return (self as EditorialElement).nodeID!
}

}

然后,我使用此类从 JSON 文件中检索数据并将其解析为“editorialObjects”数组(对所有注释和错误的间距感到抱歉):

func populateEditorials() {
if populatingEditorials {
return
}
populatingEditorials = true

Alamofire.request(GWNetworking.Router.Editorials(self.currentPage)).responseJSON() { response in
if let JSON = response.result.value {
/*
if response.result.error == nil {
*/

/* Creating objects for every single editorial is long running work, so we put that work on a background queue, to keep the app very responsive. */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {


/* Making an array of all the node IDs from the JSON file */
var nodeIDArray : [Int]


if (JSON .isKindOfClass(NSDictionary)) {

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

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

self.nodeIDArray.addObject(nodeIDValue)

if let editorialElement : EditorialElement = EditorialElement(title: "init", nodeID: 0, timeStamp: 0, imageURL: "init", author: "init", issueNumber: "init", volumeNumber: "init", articleContent: "init") {

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

let timeStampString = node.1["revision_timestamp"] as! String
editorialElement.timeStamp = Int(timeStampString)!

editorialElement.imageURL = String(node.1["image_url"])
editorialElement.author = String(node.1["author"])
editorialElement.issueNumber = String(node.1["issue_int"])
editorialElement.volumeNumber = String(node.1["volume_int"])
editorialElement.articleContent = String(node.1["html_content"])

lastItem = self.editorialObjects.count

print (editorialElement.nodeID)


self.editorialObjects.addObject(editorialElement)


/* Sorting the elements in order of newest to oldest (as the array index increases] */
let timestampSortDescriptor = NSSortDescriptor(key: "timeStamp", ascending: false)
self.editorialObjects.sortUsingDescriptors([timestampSortDescriptor])

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


/*

nodeIDArray[nodeCounter] = jsonValue{nodeCounter}.string
let editorialInfos : EditorialElement = ((jsonValue as! NSDictionary].1["\(nodeIDArray[nodeCounter]]"] as! [NSDictionary]].map { EditorialElement(title: $0["title"] as! String, nodeID: $0["nid"] as! Int, timeStamp: $0["revision_timestamp"] as! Int, imageURL: $0["image_url"] as! String, author: $0["author"], issueNumber: $0["issue_int"] as! Int, volumeNumber: $0["volume_int"] as! Int, articleContent: $0["html_content"] as! String] // I am going to try to break this line down to simplify it and fix the build errors
*/


}

print(self.editorialObjects.count)

}
}

dispatch_async(dispatch_get_main_queue()) {
self.editorialsTableView.reloadData()
}

self.currentPage++
}
}

self.populatingEditorials = false
}
}

然后我只在 cellForRowAtIndexPath 方法中使用这些对象作为标签:

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

let row = indexPath.row

let cell = tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as! EditorialsTableViewCell

let title = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).title

let timeStamp = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).timeStamp
let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(Int((editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).timeStamp)))
timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)

let imageURL = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).imageURL

let author : String! = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).author!

let issueNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).issueNumber
let volumeNumber = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).volumeNumber

let articleContent = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).articleContent

let nodeID = (editorialObjects.objectAtIndex(indexPath.row) as! EditorialElement).nodeID

/* Unlike the Pictures Collection View, there is no need to create another Alamofire request here, since we already have all the content we want from the JSON we downloaded. There is no URL that we wish to place a request to to get extra content. */

cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.editorialHeadlineLabel.text = title

cell.editorialAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialAuthorLabel.text = author

cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialPublishDateLabel.text = timeStampDateString

return cell
}

我应该在哪里强制解包我的变量?

编辑 1:修改后的代码

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

let row = indexPath.row

guard let cell = tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as? EditorialsTableViewCell else {
print ("error: editorialsTableView cell is not of class EditorialsTableViewCell, we will use RandomTableViewCell instead")
return tableView.dequeueReusableCellWithIdentifier(EditorialTableCellIdentifier, forIndexPath: indexPath) as! RandomTableViewCell
}

if let editorialObject = editorialObjects.objectAtIndex(indexPath.row) as? EditorialElement {
// we just unwrapped editorialObject

let title = editorialObject.title ?? "" // if editorialObject.title == nil, then we return an empty string.

let timeStampDateObject = NSDate(timeIntervalSince1970: NSTimeInterval(editorialObject.timeStamp))
let timeStampDateString = dateFormatter.stringFromDate(timeStampDateObject)

let author = editorialObject.author ?? ""

let issueNumber = editorialObject.issueNumber ?? ""
let volumeNumber = editorialObject.volumeNumber ?? ""

let articleContent = editorialObject.articleContent ?? ""

let nodeID = editorialObject.nodeID ?? 0


cell.editorialHeadlineLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.editorialHeadlineLabel.text = title

cell.editorialAuthorLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialAuthorLabel.text = String(author)

cell.editorialPublishDateLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleSubheadline)
cell.editorialPublishDateLabel.text = timeStampDateString

} else {

}

return cell
}

编辑2:新的EditorialElement代码

class EditorialElement: NSObject {

var title: String // title
var nodeID: Int // nid
var timeStamp: Int // revision_timestamp
var imageURL: String // image_url
var author: String // author

var issueNumber: String // issue_int
var volumeNumber: String // volume_int

var articleContent: String // html_content

/* To get an NSDate objec from Unix timestamp
var date = NSDate(timeIntervalSince1970: timeStamp) */

init(title: String, nodeID: Int, timeStamp: Int, imageURL: String, author: String, issueNumber: String, volumeNumber: String, articleContent: String) {
self.title = title
self.nodeID = nodeID
self.timeStamp = timeStamp
self.imageURL = imageURL
self.author = author
self.issueNumber = issueNumber
self.volumeNumber = volumeNumber
self.articleContent = articleContent
}

override func isEqual(object: AnyObject!) -> Bool {
return (object as! EditorialElement).nodeID == self.nodeID
}

override var hash: Int {
return (self as EditorialElement).nodeID
}

}

最佳答案

有几件事。 1. 只有当您知道那里会有东西时,才应该强行拆开它们。但如果你对此非常有信心(或者你不希望/不希望应用程序在没有它们的情况下工作),那么你应该从一开始就强制将它们打开。仅在代码中的任何地方写入 imageURL! 来执行 var imageURL: String? 是没有意义的。可选值的是让您能够优雅地处理对象可能为零的情况。

无论如何,假设这对您来说是正确的结构,我要做的第一件事是 if let 返回索引处的对象。所以写:

如果让 element = editorialObjects.objectAtIndex(indexPath.row) 为?编辑元素

现在,您可以在整个行单元格中使用 element 作为 EditorialElement。从那里您可以决定如何/何时打开其他所有内容。

所以让作者:字符串! = (editorialObjects.objectAtIndex(indexPath.row) as!EditorialElement).author! 将变为

letauthor = element.author! 或者您可以if let 避免崩溃并处理为零的情况。 if letauthor = element.author {//做某事 }

关于ios - 如何正确解开 UILabels 中使用的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33129237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com