Trying to show 100 EV charger stations on map using annotations that display their name and address, but only one of the 100 annotations which should be displayed by below code is showing up on the map.
Code builds fine and does not have any error flags, but I have a feeling I am doing something wrong with the map annotation code. During runtime get a long error message in debugger which begins:
2015-11-05 01:07:47.758 JSON[65210:8214517] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
Stack:(
0 CoreFoundation 0x000000010dd67f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010fdc2deb objc_exception_throw + 48
2 CoreFoundation 0x000000010dd67e7d +[NSException raise:format:] + 205
3 Foundation 0x000000010e35c289 _AssertAutolayoutOnMainThreadOnly + 79
4 Foundation 0x000000010e1bccce -[NSISEngine withBehaviors:performModifications:] + 31
5 UIKit 0x000000010e95ed4a -[UIView(Hierarchy) _postMovedFromSuperview:] + 575
6 UIKit 0x000000010e96c7e7 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1967
7 MapKit 0x000000010e6f2c90 -[MKAnnotationContainerView addSubview:] + 128
8 MapKit 0x000000010e6f283e -[MKAnnotationContainerView addAnnotationView:allowAnimation:] + 466
9 MapKit 0x000000010e5ff976 -[MKMapView addAnnotationRepresentation:allowAnimation:] + 487
10 MapKit 0x000000010e66ed64 -[MKAnnotationManager _addRepresentationForAnnotation:] + 721
11 MapKit 0x000000010e66e3be -[MKAnnotationManager updateVisibleAnnotations] + 1551
12 MapKit 0x000000010e66f314 -[MKAnnotationManager observeValueForKeyPath:ofObject:change:context:] + 859
13 Foundation 0x000000010e17b610 NSKeyValueNotifyObserver + 347
14 Foundation
//
// ViewController.swift
// JSON
//
// Created by Matt Velker on 11/4/15.
// Copyright © 2015 slingshot. All rights reserved.
//
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController {
#IBOutlet weak var map: MKMapView!
var annotations = [MKPointAnnotation](count: 100, repeatedValue: MKPointAnnotation())
override func viewDidLoad() {
super.viewDidLoad()
var mapLat:CLLocationDegrees = CLLocationDegrees(40.596061)
var mapLong:CLLocationDegrees = CLLocationDegrees(-98.819799)
var mapLongDelta:CLLocationDegrees = CLLocationDegrees(50)
var mapLatDelta:CLLocationDegrees = CLLocationDegrees(50)
var mapCenterLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(mapLat, mapLong)
var mapSpan:MKCoordinateSpan = MKCoordinateSpanMake(mapLatDelta, mapLongDelta)
var mapRegion:MKCoordinateRegion = MKCoordinateRegionMake(mapCenterLocation, mapSpan)
map.setRegion(mapRegion, animated: true)
let url = NSURL(string: "https://developer.nrel.gov/api/alt-fuel-stations/v1.json?fuel_type=ELEC&state=CA&limit=100&api_key=vo1v1jn4eZC83ni2pII19vYmzLmk7UQzID4VZsZT&format=JSON")!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in
if let urlContent = data {
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(urlContent, options: NSJSONReadingOptions.MutableContainers)
//print(jsonResult)
var lat: [CLLocationDegrees] = []
var long: [CLLocationDegrees] = []
var location: CLLocationCoordinate2D
var locations: [CLLocationCoordinate2D] = []
if let jsonArrayOfDictionaries = jsonResult["fuel_stations"] {
for var x=0; x < jsonArrayOfDictionaries!.count; x++ {
lat.append(jsonArrayOfDictionaries![x]["latitude"] as! CLLocationDegrees)
long.append(jsonArrayOfDictionaries![x]["longitude"] as! CLLocationDegrees)
location = CLLocationCoordinate2DMake(lat[x], long[x])
locations.append(location)
self.annotations[x].coordinate = locations[x]
self.annotations[x].title = jsonArrayOfDictionaries![x]["station_name"] as? String
self.annotations[x].subtitle = jsonArrayOfDictionaries![x]["street_address"] as? String
}
}
} catch {
print("JSON serialization failed")
}
}
}
task.resume()
map.addAnnotations(annotations)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This application is modifying the autolayout engine from a background thread,...
...
self.annotations[x].coordinate = locations[x] //this line causes the error log
...
You should load the data in the background and then when you are done update the view in main thread.
Related
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
}
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.
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)
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!
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.