Userdefault.standardを用いた際のエラー
swiftに関する質問です.アーティスト名とそのアーティストの曲とそのキーを入力し,Userdefault.standardに保存することでリストを作成するアプリを作成しています.しかし,ビルドは成功するもののデータを入力する際に,決定ボタンを押すと Thread 1: signal SIGABRT と表示されアプリが止まってしまいます.エラーコードから推測するとUserdefault.standardに空のartistクラスを保存しようとしていることがいけないのかも知れません. また,入力するデータを["","",""] にすると決定ボタンを押しても止まることはありませんでした.なのでif分以下の,データを格納する部分の分岐でエラーが発生していると考えています. 解決方法がわからないためみなさんに質問させていただきたいです.以下にコードとエラーを載せます.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let defaults = UserDefaults.standard
//クラスの中にアーティスト情報を格納する
// for i in 0..<sectionTitle.count {
// let band = artist(artist: sectionTitle[i], song: "", key: "")
// band.songList = (tableData[i])
// allData.append(band)
// }
// defaults.set(allData, forKey: "allData")
}
override func viewWillAppear(_ animated: Bool) {
let defaults = UserDefaults.standard
var names:[String] = []
var songs:[String] = []
var artistIndex = 0
var songIndex = 0
for i in 0..<allData.count {
names.append(allData[i].artistName)
}
if names.firstIndex(of: newSong[0]) != nil{
artistIndex = names.firstIndex(of: newSong[0])!
for i in 0..<allData[artistIndex].songList.count {
songs.append(allData[artistIndex].songList[i][0])
}
if songs.firstIndex(of: newSong[1]) != nil {
songIndex = songs.firstIndex(of: newSong[1])!
}
}
if newSong == ["","",""] {
//何もしない
} else if !names.contains(newSong[0]) {
//アーティストのクラスを作成し追加
var tmpData = defaults.array(forKey: "allData") as! [artist]
let band = artist(artist: newSong[0], song: newSong[1], key: newSong[2])
tmpData.append(band)
defaults.set(tmpData, forKey: "allData")
} else if !songs.contains(newSong[1]) {
//既存のアーティストに曲を追加
let index = names.firstIndex(of: newSong[0])!
var tmpData = defaults.array(forKey: "allData") as! [artist]
tmpData[index].songList.append(Array(newSong[1...2]))
defaults.set(tmpData, forKey: "allData")
} else {
//既存のアーティストのきょくのキーを変更
var tmpData = defaults.array(forKey: "allData") as! [artist]
tmpData[artistIndex].songList[songIndex][1] = newSong[2]
defaults.set(tmpData, forKey: "allData")
}
// sectionTitle = defaults.stringArray(forKey: "sectionTitle")!
// tableData = defaults.array(forKey: "tableData") as! [[[String]]]
//並べ替え
allData.sort(by: {String($0.artistName) < String($1.artistName)})
for i in 0..<allData.count {
allData[i].songList.sort(by: {String($0[0]) < String($1[0])})
print(allData[i].artistName, allData[i].songList)
}
let MyTableView: UITableView!
MyTableView = UITableView(frame: view.frame, style: .grouped)
MyTableView.delegate = self
MyTableView.dataSource = self
view.addSubview(MyTableView)
}
}
以下がエラー文です.
2019-04-06 16:31:35.482362+0900 Karaoke[31929:2461031] libMobileGestalt MobileGestalt.c:890: MGIsDeviceOneOfType is not supported on this platform.
2019-04-06 16:31:38.481424+0900 Karaoke[31929:2461031] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/matatabinoneko/Library/Developer/CoreSimulator/Devices/BF931511-B48E-4CF8-A87C-6CC0D2BEA563/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2019-04-06 16:31:38.482555+0900 Karaoke[31929:2461031] [MC] Reading from private effective user settings.
2019-04-06 16:31:43.654878+0900 Karaoke[31929:2461031] [User Defaults] Attempt to set a non-property-list object (
"Karaoke.artist"
) as an NSUserDefaults/CFPreferences value for key allData
2019-04-06 16:31:43.665879+0900 Karaoke[31929:2461031] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
"Karaoke.artist"
) for key allData'
*** First throw call stack:
(
0 CoreFoundation 0x000000010eaea6fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x000000010e08eac5 objc_exception_throw + 48
2 CoreFoundation 0x000000010eaea555 +[NSException raise:format:] + 197
3 CoreFoundation 0x000000010ea0582b _CFPrefsValidateValueForKey + 283
4 CoreFoundation 0x000000010ea05c95 -[CFPrefsSource setValues:forKeys:count:copyValues:removeValuesForKeys:count:from:] + 373
5 CoreFoundation 0x000000010ea05fbc -[CFPrefsSource setValues:forKeys:count:copyValues:from:] + 28
6 CoreFoundation 0x000000010ea06013 -[CFPrefsSource setValue:forKey:from:] + 67
7 CoreFoundation 0x000000010eae159e __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke + 318
8 CoreFoundation 0x000000010eae0e0a normalizeQuintuplet + 314
9 CoreFoundation 0x000000010eae1454 -[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:] + 100
10 CoreFoundation 0x000000010eabe3fb -[_CFXPreferences setValue:forKey:appIdentifier:container:configurationURL:] + 91
11 CoreFoundation 0x000000010eac2065 _CFPreferencesSetAppValueWithContainer + 117
12 Foundation 0x000000010db78563 -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 55
13 Karaoke 0x000000010d796de2 $s7Karaoke14ViewControllerC14viewWillAppearyySbF + 6098
14 Karaoke 0x000000010d7992a3 $s7Karaoke14ViewControllerC14viewWillAppearyySbFTo + 51
15 UIKitCore 0x0000000111d6e437 -[UIViewController _setViewAppearState:isAnimating:] + 687
16 UIKitCore 0x0000000111d6eba2 -[UIViewController __viewWillAppear:] + 131
17 UIKitCore 0x0000000111cb7491 -[UINavigationController _startCustomTransition:] + 1122
18 UIKitCore 0x0000000111ccd31a -[UINavigationController _startDeferredTransitionIfNeeded:] + 741
19 UIKitCore 0x0000000111cce6a7 -[UINavigationController __viewWillLayoutSubviews] + 150
20 UIKitCore 0x0000000111caf38d -[UILayoutContainerView layoutSubviews] + 217
21 UIKitCore 0x00000001128389c1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1417
22 QuartzCore 0x0000000113da9eae -[CALayer layoutSublayers] + 173
23 QuartzCore 0x0000000113daeb88 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 396
24 QuartzCore 0x0000000113dbaee4 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 72
25 QuartzCore 0x0000000113d2a3aa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 328
26 QuartzCore 0x0000000113d61584 _ZN2CA11Transaction6commitEv + 608
27 QuartzCore 0x0000000113d61ede _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
28 CoreFoundation 0x000000010ea510f7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
29 CoreFoundation 0x000000010ea4b5be __CFRunLoopDoObservers + 430
30 CoreFoundation 0x000000010ea4bc31 __CFRunLoopRun + 1505
31 CoreFoundation 0x000000010ea4b302 CFRunLoopRunSpecific + 626
32 GraphicsServices 0x0000000117aec2fe GSEventRunModal + 65
33 UIKitCore 0x000000011236aba2 UIApplicationMain + 140
34 Karaoke 0x000000010d79e28b main + 75
35 libdyld.dylib 0x0000000110edb541 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException