Terminating app due to uncaught exception 'NSInternalInconsistencyException' problem - ios

I am trying to implement a table view that displays books information in every cell. I get this information from the API. Here is my code:
class ViewController: UIViewController {
#IBOutlet weak var allBooksTable: UITableView!
var bookList:myList? = nil {
didSet {
allBooksTable.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.allBooksTable.dataSource = self
self.allBooksTable.delegate = self
let url = URL(string: "https://ipvefa0rg0.execute-api.us-east-1.amazonaws.com/dev/books?lang=fr&term=self")!
var request = URLRequest(url: url)
request.setValue(
"jFXzWHx7SkK6",
forHTTPHeaderField: "api-key"
)
request.httpMethod = "GET"
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, response, error) in
if let error = error {
print(error.localizedDescription)
} else if let data = data {
do {
let decodedData = try JSONDecoder().decode(myList.self,
from: data)
self.bookList = decodedData
print("user: ", decodedData.list[0].imageLinks.thumbnail)
print("===================================")
} catch let DecodingError.dataCorrupted(context) {
print(context)
} catch let DecodingError.keyNotFound(key, context) {
print("Key '\(key)' not found:", context.debugDescription)
//print("codingPath:", context.codingPath)
} catch let DecodingError.valueNotFound(value, context) {
print("Value '\(value)' not found:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch let DecodingError.typeMismatch(type, context) {
print("Type '\(type)' mismatch:", context.debugDescription)
print("codingPath:", context.codingPath)
} catch {
print("error: ", error)
}
} else {
// Handle unexpected error
}
}
task.resume()
}
}
extension ViewController: UITableViewDataSource , UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! bookTableViewCell
cell.bookName.text = bookList?.list[indexPath.row].title
if bookList?.list[indexPath.row].imageLinks.smallThumbnail != nil{
//cell.img.load(url: (bookList?.list[indexPath.row].imageLinks.smallThumbnail)!)
print((bookList?.list[indexPath.row].imageLinks.smallThumbnail)!)
cell.img.downloaded(from: (bookList?.list[indexPath.row].imageLinks.smallThumbnail)!)
}
return cell
}
}
extension UIImageView {
func downloaded(from url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { [weak self] in
self?.image = image
}
}.resume()
}
func downloaded(from link: String, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloaded(from: url, contentMode: mode)
}
}
When I run the app, it only shows my the first book and then crashes with the following error.
2020-10-15 23:12:00.953424+1100 MSA[50129:2517792] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff2043a126 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff20177f78 objc_exception_throw + 48
2 CoreAutoLayout 0x00007fff58010d41 -[NSISEngine tryToOptimizeReturningMutuallyExclusiveConstraints] + 0
3 CoreAutoLayout 0x00007fff58010fcd -[NSISEngine withBehaviors:performModifications:] + 25
4 UIKitCore 0x00007fff24ac64ad -[UIView(AdditionalLayoutSupport) _recursiveUpdateConstraintsIfNeededCollectingViews:forSecondPass:] + 112
5 UIKitCore 0x00007fff24ac6136 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 827
6 UIKitCore 0x00007fff24ac6a08 __100-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke + 85
7 UIKitCore 0x00007fff24ac6594 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 154
8 UIKitCore 0x00007fff24ac7482 -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:] + 393
9 UIKitCore 0x00007fff24ba9ad6 -[UIView _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 275
10 UIKitCore 0x00007fff24bbda37 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2979
11 QuartzCore 0x00007fff27a3dd87 -[CALayer layoutSublayers] + 258
12 QuartzCore 0x00007fff27a44239 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 575
13 UIKitCore 0x00007fff24ba8fe9 -[UIView(Hierarchy) layoutBelowIfNeeded] + 573
14 UIKitCore 0x00007fff24bb0479 +[UIView(Animation) performWithoutAnimation:] + 84
15 UIKitCore 0x00007fff248954d0 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 1300
16 UIKitCore 0x00007fff2485e8bb -[UITableView _updateVisibleCellsNow:] + 2942
17 UIKitCore 0x00007fff2487e6e6 -[UITableView layoutSubviews] + 237
18 UIKitCore 0x00007fff24bbd9ce -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2874
19 QuartzCore 0x00007fff27a3dd87 -[CALayer layoutSublayers] + 258
20 QuartzCore 0x00007fff27a44239 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 575
21 QuartzCore 0x00007fff27a4ff91 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 65
22 QuartzCore 0x00007fff27990078 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 496
23 QuartzCore 0x00007fff279c6e13 _ZN2CA11Transaction6commitEv + 783
24 QuartzCore 0x00007fff279c7616 _ZN2CA11Transaction14release_threadEPv + 210
25 libsystem_pthread.dylib 0x00007fff5dcda054 _pthread_tsd_cleanup + 551
26 libsystem_pthread.dylib 0x00007fff5dcdc512 _pthread_exit + 70
27 libsystem_pthread.dylib 0x00007fff5dcd9ddd _pthread_wqthread_exit + 77
28 libsystem_pthread.dylib 0x00007fff5dcd8afc _pthread_wqthread + 481
29 libsystem_pthread.dylib 0x00007fff5dcd7b77 start_wqthread + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Could you please help me to understand what's going on here? I have looked over for similar threads, but most of the solutions are specific to the question.

if a UIElement getting update in background thread thats why applications getting crashed so whenever you want to update UI you need to update that UI Element in main thread, and your session data task is running in background thread and there you set the value bookList but when bookList is set there is an property observer which is reload the tableview in background thread so there is two solutions.
inside your api calling functions you need to set the property in main thread like this:-
DispatchQueue.main.async {
self.bookList = decodedData
}
inside your property observer you need to do like that
var bookList:myList? = nil {
didSet {
DispatchQueue.main.async {
allBooksTable.reloadData()
}
}
}
Enjoy:-

The issue is that you set bookList from a background thread (since URLSession.dataTask calls its completion on a background thread) and in the didSet of bookList, you update the UI. You should dispatch the UI update to the main thread.
var bookList:myList? = nil {
didSet {
DispatchQueue.main.async {
allBooksTable.reloadData()
}
}
}

You can set the bookList on main thread like below...
DispatchQueue.main.async {
self.bookList = decodedData
}

Related

AudioKit crashes

I'm trying to create an app that:
Records my microphone and saves the output to a file
Plays the last recorded file
The playback should be manipulated with effects such as pitch-shift
So far I've got 1+2 down, but when I try to assign the AudioKit.output to my timePitch (or PitchShifter for that matter), I get an exception (see below). Can anyone help me out? Seems like if I set output to anything else than player, it crashes..
Disclaimer: I'm new to Swift, so please go easy on me and forgive my bad code
2017-11-08 16:39:58.637075+0100 mysoundplayer[41113:759865] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'player started when in a disconnected state'
*** First throw call stack:
(
0 CoreFoundation 0x0000000108a3c1ab __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001047ddf41 objc_exception_throw + 48
2 CoreFoundation 0x0000000108a41372 +[NSException raise:format:arguments:] + 98
3 AVFAudio 0x000000010b3bb00e _Z19AVAE_RaiseExceptionP8NSStringz + 158
4 AVFAudio 0x000000010b4131ce _ZN21AVAudioPlayerNodeImpl9StartImplEP11AVAudioTime + 204
5 AVFAudio 0x000000010b412482 -[AVAudioPlayerNode playAtTime:] + 82
6 AudioKit 0x0000000103a2270d _T08AudioKit13AKAudioPlayerC4playySo11AVAudioTimeCSg2at_tFTf4gn_n + 1933
7 AudioKit 0x0000000103a1c78d _T08AudioKit13AKAudioPlayerC5startyyF + 45
8 mysoundplayer 0x00000001035dd3b8 _T010mysoundplayer14ViewControllerC14playLoadedFileyyF + 1832
9 mysoundplayer 0x00000001035dca1e _T010mysoundplayer14ViewControllerC4playySo8UIButtonCF + 46
10 mysoundplayer 0x00000001035dca6c _T010mysoundplayer14ViewControllerC4playySo8UIButtonCFTo + 60
11 UIKit 0x000000010507c275 -[UIApplication sendAction:to:from:forEvent:] + 83
12 UIKit 0x00000001051f94a2 -[UIControl sendAction:to:forEvent:] + 67
13 UIKit 0x00000001051f97bf -[UIControl _sendActionsForEvents:withEvent:] + 450
14 UIKit 0x00000001051f81e7 -[UIControl touchesBegan:withEvent:] + 282
15 UIKit 0x00000001050f1916 -[UIWindow _sendTouchesForEvent:] + 2130
16 UIKit 0x00000001050f32de -[UIWindow sendEvent:] + 4124
17 UIKit 0x0000000105096e36 -[UIApplication sendEvent:] + 352
18 UIKit 0x00000001059d9434 __dispatchPreprocessedEventFromEventQueue + 2809
19 UIKit 0x00000001059dc089 __handleEventQueueInternal + 5957
20 CoreFoundation 0x00000001089df231 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
21 CoreFoundation 0x0000000108a7ee41 __CFRunLoopDoSource0 + 81
22 CoreFoundation 0x00000001089c3b49 __CFRunLoopDoSources0 + 185
23 CoreFoundation 0x00000001089c312f __CFRunLoopRun + 1279
24 CoreFoundation 0x00000001089c29b9 CFRunLoopRunSpecific + 409
25 GraphicsServices 0x000000010cc289c6 GSEventRunModal + 62
26 UIKit 0x000000010507a5e8 UIApplicationMain + 159
27 mysoundplayer 0x00000001035e12a7 main + 55
28 libdyld.dylib 0x000000010aa49d81 start + 1
29 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Source code:
import AudioKit
import AudioKitUI
import UIKit
class ViewController: UIViewController {
var micMixer: AKMixer!
var recorder: AKNodeRecorder!
var player: AKAudioPlayer!
var tape: AKAudioFile!
var timePitch: AKTimePitch!
var pitchShifter: AKPitchShifter!
var mainMixer : AKMixer!
var loadedFile: AKAudioFile!
let mic = AKMicrophone()
var state = State.readyToRecord
enum State {
case readyToRecord
case recording
case readyToPlay
case playing
}
#IBAction func toggleRecord(_ sender: UIButton) {
switch state {
case .recording:
sender.setTitle("record", for: .normal)
state = .readyToRecord
do {
try player.reloadFile()
} catch {
print("Error reloading!")
}
let recordedDuration = player != nil ? player.audioFile.duration : 0
if recordedDuration > 0.0 {
recorder.stop()
let randomfilename:String = NSUUID().uuidString + ".m4a"
print("Filename: \(randomfilename)")
player.audioFile.exportAsynchronously(name: randomfilename, baseDir: .documents, exportFormat: .m4a, callback: {file, exportError in
if let error = exportError {
print("Export failed \(error)")
} else {
print("Export succeeded")
self.loadedFile = file
}
})
}
case .readyToRecord:
do {
try recorder.record()
sender.setTitle("stop", for: .normal)
state = .recording
} catch { print("Error recording!") }
default:
print("no")
}
}
#IBAction func play(_ sender: UIButton) {
playLoadedFile()
}
#IBAction func valueChanged(_ sender: UISlider) {
timePitch.pitch = Double(sender.value)
}
func playLoadedFile() {
do {
try player.replace(file: loadedFile)
player.start()
} catch { print("Error playing!") }
}
func exportedAudioFile(filename: String) {
print("yay")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
AKAudioFile.cleanTempDirectory()
AKSettings.bufferLength = .medium
AKSettings.defaultToSpeaker = true
//inputPlot.node = mic
micMixer = AKMixer(mic)
mainMixer = AKMixer(player,timePitch)
pitchShifter = AKPitchShifter(player)
timePitch = AKTimePitch(player)
recorder = try? AKNodeRecorder(node: micMixer)
if let file = recorder.audioFile {
player = try? AKAudioPlayer(file: file)
}
AudioKit.output = timePitch // works with player
AudioKit.start()
print("mainMixer status: \(mainMixer.isStarted)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
It looks like mainMixer isn't connected to anything. Try changing AudioKit.output = timePitch to AudioKit.output = mainMixer.
As for best practices, get rid of any try?s. Use a do try catch and at least print the error.
if let file = recorder.audioFile {
do{
player = try AKAudioPlayer(file: file)
} catch {
print(error)
}
}
You’re attaching the player to the mixer before you create it. At the time where you give player to AKMixer(), it is nil. Move the mixer creation after the player assignment.

Trying to send web request in swift (IOS)

I looked up how to send a request, and found the answer on here, but while it seems to work for everyone else, it won't for me. Can some please please help me get the code to actually send the POST request, and print the response. The URL works, because I can manually enter in the information via my borwser, but it is a LAN website, but this app is just for learning SWIFT.
import UIKit
class PostController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sendPost(url:String,attrs:String) {
print("YO BOY")
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
let postString = attrs
request.httpBody = postString.data(using: .utf8)
print("NABBBADABBA")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
print("CHIKKA CHIKKA")
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
print("FLEKKA FLEEKA WAHAPPA")
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}
#IBOutlet var text: UITextView!
#IBAction func post(_ sender: Any) {
print("SENDING POST")
sendPost(url: "localhost/testingtest.php",attrs: "post=\(text.text)")
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
`
my problem is happening somewhere here
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
with the terminal saying:
SENDING POST
YO BOY
NABBBADABBA
2017-07-29 20:53:08.644 SocialMe[71829:3715670] -[SocialMe.PostController takePhoto:]: unrecognized selector sent to instance 0x7f850bf092e0
2017-07-29 20:53:08.647 SocialMe[71829:3715670] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SocialMe.PostController takePhoto:]: unrecognized selector sent to instance 0x7f850bf092e0'
*** First throw call stack:
(
0 CoreFoundation 0x00000001040c7b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010136d141 objc_exception_throw + 48
2 CoreFoundation 0x0000000104137134 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010404e840 ___forwarding___ + 1024
4 CoreFoundation 0x000000010404e3b8 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000101840d82 -[UIApplication sendAction:to:from:forEvent:] + 83
6 UIKit 0x00000001019c55ac -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x00000001019c58c7 -[UIControl _sendActionsForEvents:withEvent:] + 450
8 UIKit 0x00000001019c4802 -[UIControl touchesEnded:withEvent:] + 618
9 UIKit 0x00000001018ae7ea -[UIWindow _sendTouchesForEvent:] + 2707
10 UIKit 0x00000001018aff00 -[UIWindow sendEvent:] + 4114
11 UIKit 0x000000010185ca84 -[UIApplication sendEvent:] + 352
12 UIKit 0x00000001020405d4 __dispatchPreprocessedEventFromEventQueue + 2926
13 UIKit 0x0000000102038532 __handleEventQueue + 1122
14 CoreFoundation 0x000000010406dc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x00000001040530cf __CFRunLoopDoSources0 + 527
16 CoreFoundation 0x00000001040525ff __CFRunLoopRun + 911
17 CoreFoundation 0x0000000104052016 CFRunLoopRunSpecific + 406
18 GraphicsServices 0x0000000105fd2a24 GSEventRunModal + 62
19 UIKit 0x000000010183f134 UIApplicationMain + 159
20 SocialMe 0x0000000100d8f957 main + 55
21 libdyld.dylib 0x000000010506765d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

Crash on decode of object after renaming and rewriting to Swift

Since we have renamed (Bestemming -> Place) the class and rewrote it from Objective-c to Swift some users experience crashes with it. We are trying to load an object from the NSUserDefaults with the NSCoding principle.
The crash:
Thread : Crashed: com.apple.main-thread
0 Flitsmeister 0x10018b720 specialized Place.init(coder : NSCoder) -> Place? (Place.swift)
1 Flitsmeister 0x10018a6f4 #objc Place.init(coder : NSCoder) -> Place? (Place.swift)
2 Foundation 0x1839ab92c _decodeObjectBinary + 2276
3 Foundation 0x1839aaf90 _decodeObject + 304
4 Foundation 0x1839aa124 +[NSKeyedUnarchiver unarchiveObjectWithData:] + 92
5 Flitsmeister 0x100103fa0 +[SharedUserDefaultsManager WorkPlace] (SharedUserDefaultsManager.m:72)
6 Flitsmeister 0x100090830 -[InvoerBestemmingTableViewController viewWillAppear:] (InvoerBestemmingTableViewController.m:106)
7 UIKit 0x187d8074c -[UIViewController _setViewAppearState:isAnimating:] + 628
8 UIKit 0x187d804c0 -[UIViewController __viewWillAppear:] + 156
9 UIKit 0x187e27130 -[UINavigationController _startTransition:fromViewController:toViewController:] + 760
10 UIKit 0x187e26a6c -[UINavigationController _startDeferredTransitionIfNeeded:] + 868
11 UIKit 0x187e26694 -[UINavigationController __viewWillLayoutSubviews] + 60
12 UIKit 0x187e265fc -[UILayoutContainerView layoutSubviews] + 208
13 UIKit 0x187d63778 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 656
14 QuartzCore 0x185772b2c -[CALayer layoutSublayers] + 148
15 QuartzCore 0x18576d738 CA::Layer::layout_if_needed(CA::Transaction*) + 292
16 QuartzCore 0x18576d5f8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
17 QuartzCore 0x18576cc94 CA::Context::commit_transaction(CA::Transaction*) + 252
18 QuartzCore 0x18576c9dc CA::Transaction::commit() + 512
19 UIKit 0x187d59c78 _afterCACommitHandler + 180
20 CoreFoundation 0x18302c588 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
21 CoreFoundation 0x18302a32c __CFRunLoopDoObservers + 372
The class:
#objc(Place)
class Place : NSObject, NSCoding, CustomDebugStringConvertible
{
let name: String
let location: CLLocation
var lastUsed: NSDate?
var type: PlaceType
var address: String?
//MARK: - NSCoding protocol
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
aCoder.encodeObject(address, forKey: "address")
aCoder.encodeInt(type.rawValue, forKey: "type")
aCoder.encodeObject(location, forKey: "location")
aCoder.encodeObject(lastUsed, forKey: "lastUsed")
}
required init?(coder aDecoder: NSCoder) {
if let locatieNaam : String = aDecoder.decodeObjectForKey("locatieNaam") as? String {
//This is the OLD object
name = locatieNaam
let nullableLocation : CLLocation? = aDecoder.decodeObjectForKey("locatie") as? CLLocation
if let notnulllablelocation : CLLocation = nullableLocation {
location = notnulllablelocation
} else {
location = CLLocation.init(latitude: 0, longitude: 0) //Not possible
}
lastUsed = aDecoder.decodeObjectForKey("lastUsed") as? NSDate
if aDecoder.decodeBoolForKey("isThuis") {
type = .Home
} else if aDecoder.decodeBoolForKey("isWerk") {
type = .Work
} else if aDecoder.decodeBoolForKey("isFavoriet") {
type = .Favoriet
} else {
type = .Other
}
address = nil
}
else {
name = aDecoder.decodeObjectForKey("name") as! String
let nullableLocation : CLLocation? = aDecoder.decodeObjectForKey("location") as? CLLocation
if let notnullableLocation : CLLocation = nullableLocation {
location = notnullableLocation
} else {
location = CLLocation.init(latitude: 0, longitude: 0) //Not possible
}
lastUsed = aDecoder.decodeObjectForKey("lastUsed") as? NSDate
type = PlaceType.init(rawValue: aDecoder.decodeInt32ForKey("type"))!
address = aDecoder.decodeObjectForKey("address") as? String
}
}
}
Reading from NSUserDefaults:
+ (Place*)WorkPlace;
{
#try {
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:kSharedUserDefaults];
NSData *result = [mySharedDefaults objectForKey:kWerkBestemming];
if(result == NULL)
return nil;
[NSKeyedUnarchiver setClass:[Place class] forClassName:#"Bestemming"];
[NSKeyedUnarchiver setClass:[Place class] forClassName:#"BestemmingBase"];
Place *place = [NSKeyedUnarchiver unarchiveObjectWithData:result];
if(place != nil) {
place.type = PlaceTypeWork; //Needed because the old Bestemming class didnt saved the boolean isWerk
}
return place;
}
#catch (NSException *exception) {
return nil;
}
}
The crash log says it crashes on line 0, which is comment so I think it crashes in the init method and I think it has something to do with an object which is null but could not be null.
What i've tried:
Try catch in SharedUserDefaultsManager
Extra checks on non-nullables
For those users where the app crashes I can live with removing the object from NSUserDefaults. Only if I can know when it happens.
Think this is a much better approach to deal with the NSCoding init method and return nil if variables are not what you are expecting:
required convenience init?(coder decoder: NSCoder) {
guard let title = decoder.decodeObjectForKey("title") as? String,
let author = decoder.decodeObjectForKey("author") as? String,
let categories = decoder.decodeObjectForKey("categories") as? [String]
else { return nil }
self.init(
title: title,
author: author,
pageCount: decoder.decodeIntegerForKey("pageCount"),
categories: categories,
available: decoder.decodeBoolForKey("available")
)
}
From NSHipster: http://nshipster.com/nscoding/
Now let's see how the new version is working out.
Edit: it worked!

NSUnknownKeyException on app startup [duplicate]

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 7 years ago.
Program starts up and then an error is thrown and it brings me to the appDelegate.swift file --class AppDelegate: UIResponder, UIApplicationDelegate { -- thread 1:signal sigabort
Here is the code:
import UIKit
class ViewController: UIViewController {
let filemgr = NSFileManager.defaultManager()
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
#IBOutlet weak var address: UITextField!
#IBOutlet weak var street: UITextField!
#IBOutlet weak var city: UITextField!
#IBOutlet weak var state: UITextField!
#IBOutlet weak var zip: UITextField!
#IBOutlet weak var status: UILabel!
var database = NSString() // <-- CRASHING HERE
override func viewDidLoad() {
super.viewDidLoad()
let docsDir = dirPaths[0]
let databasePath = docsDir.stringByAppendingString("/contacts.db") // This seems to work
if !filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB == nil {
print("Error DB")
}
if contactDB.open() {
let sql = "CREATE TABLE IF NOT EXISTS Houses (ID INTEGER PRIMARY KEY AUTOINCREMENT, Address TEXT, Street TEXT, City TEXT, State TEXT, Zip TEXT)"
if !contactDB.executeStatements(sql) {
print("Error DB")
}
else {
print("Error DB")
}
}
}
}
#IBAction func saveData(sender: AnyObject) {
let docsDir = dirPaths[0]
let databasePath = docsDir.stringByAppendingString("/contacts.db") // This seems to work
if !filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB.open() {
let sql = "insert into Houses (Address, State, City, State, Zip) values ('\(address),\(street),\(city),\(state),\(zip))"
let result = contactDB.executeUpdate(sql, withArgumentsInArray: nil)
if !result {
status.text = "Failed to add house"
print("DB Error")
}
else {
status.text = "Added House"
address.text = ""
street.text = ""
city.text = ""
state.text = ""
zip.text = ""
}
}
else {
print("DB Error")
}
}
}
#IBAction func findContact(sender: AnyObject) {
let docsDir = dirPaths[0]
let databasePath = docsDir.stringByAppendingString("/contacts.db")
if !filemgr.fileExistsAtPath(databasePath as String) {
let contactDB = FMDatabase(path: databasePath as String)
if contactDB.open() {
let sql = "SELECT address, street, state, state, zip WHERE ID = 1" // Change this later
let results:FMResultSet? = contactDB.executeQuery(sql,withArgumentsInArray: nil)
if results?.next() == true {
address.text = results?.stringForColumn("Address")
street.text = results?.stringForColumn("Street")
city.text = results?.stringForColumn("City")
state.text = results?.stringForColumn("State")
zip.text = results?.stringForColumn("Zip")
}
else {
status.text = "Record not found"
address.text = ""
street.text = ""
city.text = ""
state.text = ""
zip.text = ""
}
contactDB.close()
}
else {
print("DBError")
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here are the errors:
2015-11-10 00:34:33.385 SQLiteDatabaseExample[3656:218507] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000106aa8f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106522deb objc_exception_throw + 48
2 CoreFoundation 0x0000000106aa8b89 -[NSException raise] + 9
3 Foundation 0x000000010488fa6b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
4 UIKit 0x0000000104e6e04c -[UIViewController setValue:forKey:] + 88
5 UIKit 0x000000010509ba71 -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x00000001069e9a80 -[NSArray makeObjectsPerformSelector:] + 224
7 UIKit 0x000000010509a454 -[UINib instantiateWithOwner:options:] + 1864
8 UIKit 0x0000000104e74c16 -[UIViewController _loadViewFromNibNamed:bundle:] + 381
9 UIKit 0x0000000104e75542 -[UIViewController loadView] + 178
10 UIKit 0x0000000104e758a0 -[UIViewController loadViewIfRequired] + 138
11 UIKit 0x0000000104e76013 -[UIViewController view] + 27
12 UIKit 0x0000000104d4f51c -[UIWindow addRootViewControllerViewIfPossible] + 61
13 UIKit 0x0000000104d4fc05 -[UIWindow _setHidden:forced:] + 282
14 UIKit 0x0000000104d614a5 -[UIWindow makeKeyAndVisible] + 42
15 UIKit 0x0000000104cdb396 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
16 UIKit 0x0000000104ce19c3 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1750
17 UIKit 0x0000000104cdeba3 -[UIApplication workspaceDidEndTransaction:] + 188
18 FrontBoardServices 0x0000000108776784 -[FBSSerialQueue _performNext] + 192
19 FrontBoardServices 0x0000000108776af2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
20 CoreFoundation 0x00000001069d5011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
21 CoreFoundation 0x00000001069caf3c __CFRunLoopDoSources0 + 556
22 CoreFoundation 0x00000001069ca3f3 __CFRunLoopRun + 867
23 CoreFoundation 0x00000001069c9e08 CFRunLoopRunSpecific + 488
24 UIKit 0x0000000104cde4f5 -[UIApplication _run] + 402
25 UIKit 0x0000000104ce330d UIApplicationMain + 171
26 SQLiteDatabaseExample 0x00000001047a05ad main + 109
27 libdyld.dylib 0x000000010824f92d start + 1
28 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Check your ViewController's IBOutlets connections and remove any unwanted connection.It can happen when you delete the outlet code in your class.

Deleting a row in UITableView causes NSInternalInconsistencyException with computed property

I have a computed property as an instance variable:
var favorites: [PFObject]
get { return Array(ObjectManager.favorites) }
}
where ObjectManager.favorites is a Set static var on the ObjectManager class.
Below are my table view methods:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favorites.count
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
//delete it from the original set
ObjectManager.removeFavoriteObject(favorites[indexPath.row])
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
println(favorites.count)
// Delete the row from the data source
}
//If there are no longer any fav objects, then there is no need to display the Edit button.
if favorites.isEmpty {
self.navigationItem.rightBarButtonItem = nil
}
}
This results in the following exception being trigger when I call deleteRowsAtIndexPaths.
2015-06-08 21:00:35.320 MedConf[56258:707923] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:1623
2015-06-08 21:00:35.396 MedConf[56258:707923] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(
0 CoreFoundation 0x00000001114aec65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000110defbb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001114aeaca +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000110a0498f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x0000000111b30c53 -[UITableView _endCellAnimationsWithContext:] + 12678
5 UIKit 0x0000000121f59f1b -[UITableViewAccessibility deleteRowsAtIndexPaths:withRowAnimation:] + 48
6 MedConf 0x000000010f1cebb3 _TFC7MedConf28FavoritesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 195
7 MedConf 0x000000010f1ced87 _TToFC7MedConf28FavoritesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 87
8 UIKit 0x0000000111b56226 -[UITableView animateDeletionOfRowWithCell:] + 132
9 UIKit 0x0000000111b353fd __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 72
10 UIKit 0x0000000111a54da2 -[UIApplication sendAction:to:from:forEvent:] + 75
11 UIKit 0x0000000111b6654a -[UIControl _sendActionsForEvents:withEvent:] + 467
12 UIKit 0x0000000111b65919 -[UIControl touchesEnded:withEvent:] + 522
13 UIKit 0x0000000111dffa10 _UIGestureRecognizerUpdate + 9487
14 UIKit 0x0000000111aa1686 -[UIWindow _sendGesturesForEvent:] + 1041
15 UIKit 0x0000000111aa22b2 -[UIWindow sendEvent:] + 666
16 UIKit 0x0000000111a68581 -[UIApplication sendEvent:] + 246
17 UIKit 0x0000000111a75d1c _UIApplicationHandleEventFromQueueEvent + 18265
18 UIKit 0x0000000111a505dc _UIApplicationHandleEventQueue + 2066
19 CoreFoundation 0x00000001113e2431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
20 CoreFoundation 0x00000001113d82fd __CFRunLoopDoSources0 + 269
21 CoreFoundation 0x00000001113d7934 __CFRunLoopRun + 868
22 CoreFoundation 0x00000001113d7366 CFRunLoopRunSpecific + 470
23 GraphicsServices 0x0000000113603a3e GSEventRunModal + 161
24 UIKit 0x0000000111a53900 UIApplicationMain + 1282
25 MedConf 0x000000010f1debf7 main + 135
26 libdyld.dylib 0x0000000113bff145 start + 1
27 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I'm not sure what the cause of the exception is, as it gets thrown even if I comment out line where I remove the object from the data source: ObjectManager.removeFavoriteObject(favorites[indexPath.row]).
Below is the removeFavoriteObject method.
static func removeFavoriteObject( objectToRemove: PFObject ) {
if contains(favorites, objectToRemove) {
if let favObject = objectToRemove as? Attendee {
favObject.isFavorite = false
} else if let favObject = objectToRemove as? Event {
favObject.isFavorite = false
} else if let favObject = objectToRemove as? Session {
favObject.isFavorite = false
}
}
favorites.remove(objectToRemove)
}
Try this
static func removeFavoriteObject( objectToRemove: PFObject ) {
if contains(favorites, objectToRemove) {
favorites.remove(objectToRemove)
if let favObject = objectToRemove as? Attendee {
favObject.isFavorite = false
} else if let favObject = objectToRemove as? Event {
favObject.isFavorite = false
} else if let favObject = objectToRemove as? Session {
favObject.isFavorite = false
}
}
}
try passing index of object to that removeFavoriteObject static method:
ObjectManager.removeFavoriteObject(indexPath.row)
and as you are passing element index from favourites array, it must be there. So no need to check using 'if'.
static func removeFavoriteObject( index: Int ) {
var objectToRemove = favorites[index]
if let favObject = objectToRemove as? Attendee {
favObject.isFavorite = false
}
else if let favObject = objectToRemove as? Event {
favObject.isFavorite = false
}
else if let favObject = objectToRemove as? Session {
favObject.isFavorite = false
}
favorites.removeAtIndex(index)
}
tableview.beginUpdates()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableview.endUpdates()
Hope this helps

Resources