Swift初学者です。
チュートリアルを参考にSNSアプリ制作中です。
シミュレーターを起動し、SettingViewControllerからEditProfileViewControllerへ遷移しようとするとシミュレーターが落ち、以下のメッセージが表示されます。
5 -> 0x7fff5dca633a <+10>: jae 0x7fff5dca6344 ; <+20> Thread 1: "must pass a class of kind UITableViewCell"
正直自力では対処できず、こちらに投稿させていただきました。助言いただければ幸いです。
以下はコンソールに表示されたメッセージです。
2020-10-09 21:01:28.976434+0900 gyosufan[1250:35937] [Firebase/Crashlytics] Version 4.3.1
2020-10-09 21:01:29.174470+0900 gyosufan[1250:37408] 6.28.0 - [Firebase/Analytics][I-ACS031025] Analytics screen reporting is enabled. Call +[FIRAnalytics setScreenName:setScreenClass:] to set the screen name or override the default screen class name. To disable screen reporting, set the flag FirebaseScreenReportingEnabled to NO (boolean) in the Info.plist
2020-10-09 21:01:29.270831+0900 gyosufan[1250:37408] 6.28.0 - [Firebase/Analytics][I-ACS023007] Analytics v.60602000 started
2020-10-09 21:01:29.321321+0900 gyosufan[1250:37408] 6.28.0 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see http://goo.gl/RfcP7r)
2020-10-09 21:01:29.365774+0900 gyosufan[1250:37408] 6.28.0 - [Firebase/Analytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-10-09 21:01:29.616851+0900 gyosufan[1250:37403] 6.28.0 - [Firebase/Analytics][I-ACS800023] No pending snapshot to activate. SDK name: app_measurement
2020-10-09 21:01:30.616189+0900 gyosufan[1250:37403] 6.28.0 - [Firebase/Analytics][I-ACS023012] Analytics collection enabled
2020-10-09 21:01:33.545505+0900 gyosufan[1250:37407] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed
2020-10-09 21:01:34.164445+0900 gyosufan[1250:37429] 6.28.0 - [Firebase/Crashlytics][I-CLS000000] Failed to download settings Error Domain=FIRCLSNetworkError Code=-5 "(null)" UserInfo={status_code=404, type=2, request_id=, content_type=text/html; charset=utf-8}
2020-10-09 21:01:41.382143+0900 gyosufan[1250:35937] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'must pass a class of kind UITableViewCell'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff2043a126 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff20177f78 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2043a004 -[NSException initWithCoder:] + 0
3 UIKitCore 0x00007fff2488193e -[UITableView registerClass:forCellReuseIdentifier:] + 316
4 gyosufan 0x000000010749c7fa $s8gyosufan25EditProfileViewControllerC05tableD033_2A81A928A072FB25008A8B68CBA5016BLLSo07UITableD0CvpfiAGyXEfU_ + 138
5 gyosufan 0x000000010749feda $s8gyosufan25EditProfileViewControllerC7nibName6bundleACSSSg_So8NSBundleCSgtcfc + 186
6 gyosufan 0x00000001074a013c $s8gyosufan25EditProfileViewControllerC7nibName6bundleACSSSg_So8NSBundleCSgtcfcTo + 172
7 gyosufan 0x00000001074a1633 $sSo16UIViewControllerCABycfcTO + 19
8 gyosufan 0x00000001074a160f $sSo16UIViewControllerCABycfC + 31
9 gyosufan 0x00000001074bb26c $s8gyosufan21SettingViewControllerC17didTapEditProfile33_2BE0CAC7A25DB4FEB40CD0146FEA96B1LLyyF + 76
10 gyosufan 0x00000001074bb206 $s8gyosufan21SettingViewControllerC15configureModels33_2BE0CAC7A25DB4FEB40CD0146FEA96B1LLyyFyycfU_ + 198
11 gyosufan 0x00000001074bdae5 $s8gyosufan21SettingViewControllerC05tableC0_14didSelectRowAtySo07UITableC0C_10Foundation9IndexPathVtF + 725
12 gyosufan 0x00000001074bdbb7 $s8gyosufan21SettingViewControllerC05tableC0_14didSelectRowAtySo07UITableC0C_10Foundation9IndexPathVtFTo + 167
13 UIKitCore 0x00007fff248787f6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:isCellMultiSelect:] + 1081
14 UIKitCore 0x00007fff248783a6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 97
15 UIKitCore 0x00007fff24878cf5 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 334
16 UIKitCore 0x00007fff24b5697a -[_UIAfterCACommitBlock run] + 54
17 UIKitCore 0x00007fff2466f85d _runAfterCACommitDeferredBlocks + 333
18 UIKitCore 0x00007fff2465f790 _cleanUpAfterCAFlushAndRunDeferredBlocks + 221
19 UIKitCore 0x00007fff24691154 _afterCACommitHandler + 85
20 CoreFoundation 0x00007fff203a76b3 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
21 CoreFoundation 0x00007fff203a1f3f __CFRunLoopDoObservers + 547
22 CoreFoundation 0x00007fff203a24e2 __CFRunLoopRun + 1113
23 CoreFoundation 0x00007fff203a1b9e CFRunLoopRunSpecific + 567
24 GraphicsServices 0x00007fff2b773db3 GSEventRunModal + 139
25 UIKitCore 0x00007fff24660af3 -[UIApplication _run] + 912
26 UIKitCore 0x00007fff24665a04 UIApplicationMain + 101
27 libswiftUIKit.dylib 0x00007fff539f37b2 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
28 gyosufan 0x00000001074ab76a $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122
29 gyosufan 0x00000001074ab6de $s8gyosufan11AppDelegateC5$mainyyFZ + 46
30 gyosufan 0x00000001074ab7b9 main + 41
31 libdyld.dylib 0x00007fff20257415 start + 1
32 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'must pass a class of kind UITableViewCell'
terminating with uncaught exception of type NSException
CoreSimulator 732.17 - Device: iPhone SE (2nd generation) (52F44A5A-DA3B-4922-8049-5BE63803AA59) - Runtime: iOS 14.0 (18A372) - DeviceType: iPhone SE (2nd generation)
(lldb)
解決に至らなかったためコメントいただいた通り、追加させていただきます。
import SafariServices
import UIKit
struct SettingCellModel {
let title: String
let handler: (() -> Void)
}
//ユーザー設定
final class SettingViewController: UIViewController {
private let tableView: UITableView = {
let tableView = UITableView(frame: .zero,
style: .grouped)
tableView.register(UITableViewCell.self,
forCellReuseIdentifier: "cell")
return tableView
}()
private var data = [[SettingCellModel]]()
override func viewDidLoad() {
super.viewDidLoad()
configureModels()
view.backgroundColor = .systemBackground
view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
private func configureModels() {
data.append([
SettingCellModel(title: "プロフィールを編集") { [weak self] in
self?.didTapEditProfile()
},
SettingCellModel(title: "友達を招待") { [weak self] in
self?.didTapInviteFriends()
},
SettingCellModel(title: "投稿を保存") { [weak self] in
self?.didTapSaveOriginalPosts()
}
])
data.append([
SettingCellModel(title: "利用規約") { [weak self] in
self?.openURL(type: .terms)
},
SettingCellModel(title: "プライバシーポリシー") { [weak self] in
self?.openURL(type: .privacy)
},
SettingCellModel(title: "ヘルプ") { [weak self] in
self?.openURL(type: .help)
}
])
data.append([
SettingCellModel(title: "ログアウト") { [weak self] in
self?.didTapLogOut()
}
])
}
enum SettingsURLType {
case terms, privacy, help
}
private func openURL(type: SettingsURLType) {
let urlString: String
switch type {
case .terms: urlString = "https://help.instagram.com/581066165581870"
case .privacy: urlString = "https://help.instagram.com/519522125107875"
case .help: urlString = "https://help.instagram.com/"
}
guard let url = URL(string: urlString) else {
return
}
let vc = SFSafariViewController(url: url)
present(vc, animated: true)
}
private func didTapSaveOriginalPosts() {
}
private func didTapInviteFriends() {
//友達を招待する
}
private func didTapEditProfile() {
let vc = EditProfileViewController()
vc.title = "プロフィールを編集"
let navVC = UINavigationController(rootViewController: vc)
present(navVC, animated: true)
}
private func didTapLogOut() {
let actionSheet = UIAlertController(title: "ログアウト",
message: "ログアウトしますか?",
preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "キャンセル",
style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "ログアウト",
style: .destructive, handler: { _ in
AuthManager.shared.logOut(completion: { success in
DispatchQueue.main.async {
if success {
//ログイン中
let loginVC = LoginViewController()
loginVC.modalPresentationStyle = .fullScreen
self.present(loginVC, animated: true) {
self.navigationController?.popToRootViewController(animated: false)
self.tabBarController?.selectedIndex = 0
}
}
else {
//エラー発生
fatalError("ログアウト失敗")
}
}
})
}))
actionSheet.popoverPresentationController?.sourceView = tableView
actionSheet.popoverPresentationController?.sourceRect = tableView.bounds
present(actionSheet, animated: true)
}
}
extension SettingViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = data[indexPath.section][indexPath.row].title
cell.accessoryType = .disclosureIndicator
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
data[indexPath.section][indexPath.row].handler()
}
}
import UIKit
struct EditProfileForModel {
let label: String
let placeholder: String
var value: String?
}
final class EditProfileViewController: UIViewController, UITableViewDataSource {
private let tableView: UITableView = {
let tableView = UITableView()
tableView.register(UITableView.self,
forCellReuseIdentifier: "cell")
return tableView
}()
private var models = [[EditProfileForModel]]()
override func viewDidLoad() {
super.viewDidLoad()
configureModels()
tableView.tableHeaderView = createTableHeaderView()
tableView.dataSource = self
view.addSubview(tableView)
view.backgroundColor = .systemBackground
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "保存",
style: .done,
target: self,
action: #selector(didTapSave)
)
navigationItem.leftBarButtonItem = UIBarButtonItem(
title: "キャンセル",
style: .plain,
target: self,
action: #selector(didTapCancel)
)
}
private func configureModels() {
//名前、ユーザー名、サイト、経歴
let section1Labels = ["名前","ユーザー名","経歴"]
var section1 = [EditProfileForModel]()
for label in section1Labels {
let model = EditProfileForModel(label: label,
placeholder: "決定 \(label)",
value: nil)
section1.append(model)
}
models.append(section1)
//アドレス、電話番号、性別
let section2Labels = ["メールアドレス","電話番号","性別"]
var section2 = [EditProfileForModel]()
for label in section2Labels {
let model = EditProfileForModel(label: label,
placeholder: "決定 \(label)",
value: nil)
section2.append(model)
}
models.append(section2)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.frame = view.bounds
}
//mark tableview
private func createTableHeaderView() -> UIView {
let header = UIView(frame: CGRect(x: 0,
y: 0,
width: view.width,
height: view.height/4).integral)
let size = header.height/1.5
let profilePhotoButton = UIButton(frame: CGRect(x: (view.width-size)/2,
y: (header.height-size)/2,
width: size,
height: size))
header.addSubview(profilePhotoButton)
profilePhotoButton.layer.masksToBounds = true
profilePhotoButton.layer.cornerRadius = size/2.0
profilePhotoButton.tintColor = .label
profilePhotoButton.addTarget(self,
action: #selector(didTapProfilePhotoButton),
for: .touchUpInside)
profilePhotoButton.setBackgroundImage(UIImage(systemName: "person.circle"),
for: .normal)
profilePhotoButton.layer.borderWidth = 1
profilePhotoButton.layer.borderColor = UIColor.secondarySystemBackground.cgColor
return header
}
@objc private func didTapProfilePhotoButton() {
}
func numberOfSections(in tableView: UITableView) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let model = models[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = model.label
return cell
}
//mark action
@objc private func didTapSave() {
//データベースに情報を保存
}
@objc private func didTapCancel() {
dismiss(animated: true, completion: nil)
}
@objc private func didTapChangeProfilePicture() {
let actionSheet = UIAlertController(
title: "プロフィール写真",
message: "プロフィール写真を変更",
preferredStyle: .actionSheet
)
actionSheet.addAction(UIAlertAction(
title: "カメラで撮影",
style: .default,
handler: { _ in})
)
actionSheet.addAction(UIAlertAction(
title: "ライブラリーから選択",
style: .default,
handler: { _ in})
)
actionSheet.addAction(UIAlertAction(
title: "キャンセル",
style: .cancel,
handler: nil))
actionSheet.popoverPresentationController?.sourceView = view
actionSheet.popoverPresentationController?.sourceRect = view.bounds
present(actionSheet, animated: true)
}
}
[UITableView registerClass:forCellReuseIdentifier:]
とあり、理由がmust pass a class of kind UITableViewCell
とあるのですから、register(_:forCellReuseIdentifier:)
で変なクラスを渡しているんだろうとしか言えません。それでご自身で解決できない場合には、どの行で例外が発生しているのかを突き止めて、その行を含むメソッドと関連する部分全てのソースコードを本文中に追記してください。EditProfileViewController
の中で、private let tableView
の中身で、tableView.register(UITableView.self, ...)
とあるのは、tableView.register(UITableViewCell.self, ...)
の間違いではありませんか?