我有一个名为UITableViewCell
的{{1}}子类。这是它的代码:
ConferenceResultsTableViewCell
此单元格应具有一个名为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.swift:
game
我的问题是此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
在UITableViewCell子类中将以下内容用作设置方法时,我得到了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
}
:
Thread 1: EXC_BAD_ACCESS
做所有这些事情的更好的方法是什么?
答案 0 :(得分:0)
我决定只从班级中删除game
变量,然后不用它。出于某种原因,我以为我需要它,但我意识到自己不需要。