gpt4 book ai didi

ios - 设置当前为 "get-only"的属性值的最佳方法

转载 作者:行者123 更新时间:2023-11-29 05:36:53 25 4
gpt4 key购买 nike

我有一个名为 ConferenceResultsTableViewCellUITableViewCell 子类。这是它的代码:

import Foundation
import UIKit
import os.log

class ConferenceResultsTableViewCell: UITableViewCell {

public var game: Game {

get {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
fatalError("Could not get appDelagate in ConferenceResultsTableViewCell.swift")
}
let managedContext = appDelegate.persistentContainer.viewContext

var contestantsStrArray = [String]()
guard let contestant1 = gameWinnerControl.titleForSegment(at: 0) else {
os_log("Could not unwrap contestant1 in ConferenceResultsTableViewCell.swift", log: OSLog.default, type: .debug)
return Game.newGame(context: managedContext, contestants: nil, winner: nil, confidence: nil, conferences: nil)
}
guard let contestant2 = gameWinnerControl.titleForSegment(at: 1) else {
os_log("Could not unwrap contestant2 in ConferenceResultsTableViewCell.swift", log: OSLog.default, type: .debug)
return Game.newGame(context: managedContext, contestants: nil, winner: nil, confidence: nil, conferences: nil)
}
contestantsStrArray.append(contestant1)
contestantsStrArray.append(contestant2)
let winnerStr = contestantsStrArray[gameWinnerControl.selectedSegmentIndex]
guard let confidenceStr = confidenceTextField.text else {
os_log("Could not unwrap confidenceStr in ConferenceResultsTableViewCell.swift", log: OSLog.default, type: .debug)
return Game.newGame(context: managedContext, contestants: contestantsStrArray, winner: winnerStr, confidence: nil, conferences: nil)
}
return Game.newGame(context: managedContext, contestants: contestantsStrArray, winner: winnerStr, confidence: Int(confidenceStr), conferences: nil)
}

}

@IBOutlet weak var gameWinnerControl: UISegmentedControl!
@IBOutlet weak var confidenceTextField: UITextField!
@IBOutlet weak var confidenceAverageAllUsersLabel: UILabel!

}

此单元格应该有一个名为 game 的属性,其中包含将显示的一些信息。这是 Game.swift:

import Foundation
import CoreData

class Game: NSManagedObject {

@NSManaged var contestants: [String]
@NSManaged var winner: String
@NSManaged var confidence: Int
@NSManaged var conferences: [String]

}

extension Game {

public static func newGame(context: NSManagedObjectContext, contestants: [String]?, winner: String?, confidence: Int?, conferences: [Conference]?) -> Game {

let newGame = Game(context: context)

if let contestants = contestants {
newGame.contestants = contestants
} else {
newGame.contestants = ["", ""]
}
if let winner = winner {
newGame.winner = winner
} else {
newGame.winner = ""
}
if let confidence = confidence {
newGame.confidence = confidence
} else {
newGame.confidence = 0
}
if let conferences = conferences {
newGame.conferences.removeAll()
for conference in conferences {
newGame.conferences.append(Conference.getStringValue(conference: conference))
}
// newGame.conferences = conferences.map({ Conference.getStringValue(conference: $0) })
} else {
newGame.conferences.removeAll()
newGame.conferences.append("CAA")
}

return newGame

}

}

我的问题是这个 game 变量当前是“仅获取”的,因此我无法从 ConferenceResultsTableViewController.tableView(cellForRowAt:) 设置游戏的值。以下是该方法的代码,该方法尝试为 game 变量设置单元格的值:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier: CONFERENCE_RESULTS_CELL_IDENTIFIER, for: indexPath) as? ConferenceResultsTableViewCell else {
fatalError("The dequeued cell is not an instance of ConferenceResultsTableViewCell.")
}
guard let game = self.gamesToBeShown[safe: indexPath.row] else {
os_log("Could not unwrap game for indexPath in ConferenceResultsTableViewController.swift", log: OSLog.default, type: .default)
self.tableView.reloadData()
return ConferencesTableViewCell()
}

cell.game = game
cell.gameWinnerControl.insertSegment(withTitle: game.contestants[0], at: 0, animated: true)
cell.gameWinnerControl.insertSegment(withTitle: game.contestants[1], at: 1, animated: true)
cell.confidenceTextField.text = String(game.confidence)
cell.confidenceAverageAllUsersLabel.text = "0"

return cell

}

在 UITableViewCell 子类中使用以下内容作为 setter 方法时,我收到Thread 1: EXC_BAD_ACCESS:

set {
self.game = newValue
}

什么是完成这一切的更好方法?

最佳答案

我决定简单地从我的类中删除 game 变量,并且不使用它。出于某种原因,我认为我需要它,但我意识到我不需要。

关于ios - 设置当前为 "get-only"的属性值的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56957837/

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