My application crash when i use below code.
var char : Character = "1"
var char2 : Character = "1"
func changeChar( char1: inout Character,char2: inout Character) {
char = "b"
char2 = "b"
}
override func viewDidAppear(_ animated: Bool) {
print(char,char2)
changeChar(char1: &char, char2: &char2)
print(char,char2)
}
Error:
Simultaneous accesses to 0x100e0ec50, but modification requires exclusive access.
Previous access (a modification) started at PPlayerNew`ViewController.viewDidAppear(_:) + 340 (0x1000eeda8).
Current access (a modification) started at:
0 libswiftCore.dylib 0x00000001004b9a38 swift_beginAccess + 468
1 PPlayerNew 0x00000001000ee960 ViewController.char.setter + 92
2 PPlayerNew 0x00000001000eebbc ViewController.changeChar(char1:char2:) + 96
3 PPlayerNew 0x00000001000eec54 ViewController.viewDidAppear(_:) + 436
4 PPlayerNew 0x00000001000eef98 #objc ViewController.viewDidAppear(_:) + 64
5 UIKit 0x000000018b3cc5c0 <redacted> + 856
6 UIKit 0x000000018b439630 <redacted> + 44
7 UIKit 0x000000018b43959c <redacted> + 92
8 UIKit 0x000000018b641470 <redacted> + 556
9 UIKit 0x000000018b633420 <redacted> + 528
10 UIKit 0x000000018b64c7b4 <redacted> + 152
11 CoreFoundation 0x00000001852312f8 <redacted> + 20
12 CoreFoundation 0x0000000185230a08 <redacted> + 288
13 CoreFoundation 0x000000018522e6c0 <redacted> + 728
14 CoreFoundation 0x000000018515ebfc CFRunLoopRunSpecific + 424
15 GraphicsServices 0x0000000186bc9010 GSEventRunModal + 100
16 UIKit 0x000000018b419bcc UIApplicationMain + 208
17 PPlayerNew 0x00000001000f0570 main + 76
18 libdyld.dylib 0x000000018416d598 <redacted> + 4
But when i use single param function, like below. code is working
func changeChar( char1: inout Character) {
char = "b"
}
So my main goal is to use inout function with multiple parameters.How can i do this or what's wrong with my code?
Thank You.
You’re changing the property char in your func instead of the parameter char1.
Related
I'm a bit stumped on this one. I have two core data entities: Person and Memory. Memory has a people field that is a "to many" relationship to Person, and Person has a memories field that is a "to many" relationship to Memory.
I created a PeopleForm view to manage the people associated with a memory. The memory object gets passed in as an #ObservedObject to the view. I want to list the people, but people is an NSSet, so I created a computed property called peopleArray that converts the NSSet to [Person] (and sorts it for good measure).
I use the ForEach view with peopleArray to create a list of people. I use the onDelete view modifier to let the user remove a person from the list of people for that memory. The onDelete calls the removePerson function, which looks up the person object in peopleArray by the index, and then passes it to the memory object's removeFromPeople() method.
The removeFromPeople() method appears to be what triggers the runtime error. I've ready about exclusivity enforcement, but I can't figure out where I've gone wrong here.
The following is a pared down version of my code:
struct PeopleForm: View {
#ObservedObject var memory: Memory
#Environment(\.managedObjectContext) var viewContext
#Environment(\.dismiss) var dismiss
private var peopleArray: [Person] {
get {
Array(memory.people as! Set<Person>).sorted {
$0.displayName < $1.displayName
}
}
}
var body: some View {
NavigationView {
List {
ForEach(peopleArray) { person in
Text(person.displayName)
}
.onDelete(perform: removePerson)
}
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
dismiss()
}
}
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
}
}
}
func removePerson(at offsets: IndexSet) {
offsets.forEach { index in
let person = peopleArray[index]
viewContext.perform {
memory.removeFromPeople(person)
}
}
}
}
I also want to note that the stack trace points at the #main decorator on my App object declaration, which I understand is a symptom of a crash occurring within objc code. The error does not occur when stepping through the code line by line in the debugger, but I also can't step into the removeFromPeople method that is generated from my data model. If I step through to the removeFromPeople line and resume, the error occurs, so it seems to be something internal to CoreData that is going on here. Sure enough, the CoreData generated removeFromPeople method is objective c code for which I don't know how to see the implementation.
Simultaneous accesses to 0x117406ef0, but modification requires exclusive access.
Previous access (a modification) started at SwiftUI`<redacted> + 156 (0x198684c4c).
Current access (a read) started at:
0 libswiftCore.dylib 0x00000001957a59c8 <redacted> + 432
1 libswiftCore.dylib 0x00000001957a5bf0 swift_beginAccess + 84
2 SwiftUI 0x0000000198599250 <redacted> + 28
3 SwiftUI 0x000000019926b5e0 <redacted> + 44
4 SwiftUI 0x00000001985a5658 <redacted> + 124
5 SwiftUI 0x000000019937a078 <redacted> + 104
6 SwiftUI 0x000000019937a0fc <redacted> + 236
7 UIKitCore 0x0000000193e74fbc <redacted> + 124
8 UIKitCore 0x000000019329a428 <redacted> + 212
9 UIKitCore 0x00000001932e2fb4 <redacted> + 1100
10 UIKitCore 0x000000019310d2b0 <redacted> + 136
11 UIKitCore 0x0000000192f41030 <redacted> + 532
12 UIKitCore 0x0000000192f7015c <redacted> + 96
13 UIKitCore 0x000000019329aa58 <redacted> + 72
14 UIKitCore 0x0000000192fb6340 <redacted> + 436
15 SwiftUI 0x0000000198699f5c <redacted> + 64
16 SwiftUI 0x000000019865abf0 <redacted> + 16
17 libswiftCore.dylib 0x00000001957a83ec <redacted> + 56
18 SwiftUI 0x0000000198699f5c <redacted> + 64
19 SwiftUI 0x000000019865abf0 <redacted> + 16
20 libswiftCore.dylib 0x00000001957a83ec <redacted> + 56
21 SwiftUI 0x0000000198699f5c <redacted> + 64
22 SwiftUI 0x000000019865abf0 <redacted> + 16
23 libswiftCore.dylib 0x00000001957a83ec <redacted> + 56
24 libswiftCore.dylib 0x000000019579b060 swift_arrayDestroy + 124
25 SwiftUI 0x0000000198fe3c84 <redacted> + 44
26 SwiftUI 0x0000000198fe39ac <redacted> + 116
27 SwiftUI 0x00000001987883dc <redacted> + 40
28 libswiftCore.dylib 0x00000001957a83ec <redacted> + 56
29 AttributeGraph 0x00000001c2e76a70 <redacted> + 152
30 AttributeGraph 0x00000001c2e76178 <redacted> + 1260
31 AttributeGraph 0x00000001c2e75cc8 <redacted> + 264
32 SwiftUI 0x0000000198684bb0 <redacted> + 188
33 SwiftUI 0x00000001985cd1a0 <redacted> + 84
34 SwiftUI 0x00000001985c7758 _UIHostingView.__deallocating_deinit + 228
35 SwiftUI 0x00000001985d25ac <redacted> + 28
36 SwiftUI 0x0000000198718618 <redacted> + 36
37 libobjc.A.dylib 0x00000001a964027c <redacted> + 116
38 libobjc.A.dylib 0x00000001a963d0f4 objc_destructInstance + 80
39 libobjc.A.dylib 0x00000001a9646964 _objc_rootDealloc + 80
40 UIKitCore 0x00000001930be6c8 <redacted> + 156
41 UIKitCore 0x0000000192f89278 <redacted> + 1220
42 CoreFoundation 0x000000019095ebd4 <redacted> + 116
43 CoreFoundation 0x0000000190972848 <redacted> + 276
44 UIKitCore 0x0000000192f89278 <redacted> + 1184
45 UIKitCore 0x00000001931569bc <redacted> + 448
46 libobjc.A.dylib 0x00000001a963f9b0 <redacted> + 196
47 libobjc.A.dylib 0x00000001a963bd20 objc_autoreleasePoolPop + 212
48 UIKitCore 0x00000001930e8cf0 <redacted> + 92
49 UIKitCore 0x0000000192ffc008 <redacted> + 192
50 UIKitCore 0x0000000192f25664 <redacted> + 644
51 UIKitCore 0x0000000192f25f88 <redacted> + 132
52 UIKitCore 0x00000001932fe990 <redacted> + 84
53 UIKitCore 0x0000000193599030 <redacted> + 84
54 UIKitCore 0x0000000193c1ec20 <redacted> + 144
55 UIKitCore 0x0000000193c1e41c <redacted> + 92
56 CoreFoundation 0x0000000190a0bee8 <redacted> + 28
57 CoreFoundation 0x0000000190a1cbc0 <redacted> + 208
58 CoreFoundation 0x0000000190956078 <redacted> + 268
59 CoreFoundation 0x000000019095b810 <redacted> + 828
60 CoreFoundation 0x000000019096f460 CFRunLoopRunSpecific + 600
61 GraphicsServices 0x00000001aca092d0 GSEventRunModal + 164
62 UIKitCore 0x00000001932d4a3c <redacted> + 1100
63 UIKitCore 0x0000000193056480 UIApplicationMain + 364
64 SwiftUI 0x00000001987a659c <redacted> + 164
65 SwiftUI 0x00000001986d4428 <redacted> + 252
66 SwiftUI 0x00000001986b5790 static App.main() + 128
67 Matter 0x0000000104b657d8 static MatterApp.$main() + 40
68 Matter 0x0000000104b65938 main + 12
The design is not quite right. PeopleForm needs a #FetchRequest for People with an NSPredicate(format: "memory == %#", memory), e.g.
struct PeopleForm: View {
private var fetchRequest: FetchRequest<People>
private var people: FetchedResults< People > {
fetchRequest.wrappedValue
}
init(memory: Memory) {
let sortDescriptors = [SortDescriptor(\People.timestamp, order: sortAscending ? .forward : .reverse)]
fetchRequest = FetchRequest(sortDescriptors: sortDescriptors, predicate: NSPredicate(format: "memory = %#", memory), animation: .default)
}
I have an object that stores latitude and longitude coordinates. At one point, they were stored as an Optional Double?. I have since changed so it is a non optional Double. Now I see random crash logs from what I assume is users updating their app.
I tried checking to make sure that there is a value there, but it is still causing the crash.
let lat: Double
...
required init (coder aDecoder: NSCoder) {
...
if aDecoder.containsValue(forKey: "lat") {
lat = aDecoder.decodeDouble(forKey: "lat") //here is the crash
}else{
lat = 0
}
...
}
I previously came across this issue and used the following code to get around it, but Xcode says I cannot decode Doubles from objects any more
if let v = aDecoder.decodeObject(forKey: "lat") as? Double{//*** -[NSKeyedUnarchiver decodeObjectForKey:] : value for key ( lat ) is not an object. This will become an error in the future.
lat = v
}else{
if aDecoder.containsValue(forKey: "lat") {
lat = aDecoder.decodeDouble(forKey: "lat")
}else{
lat = 0
}
}
Here is a crash log:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Last Exception Backtrace:
0 CoreFoundation 0x18120004c __exceptionPreprocess + 220 (NSException.m:200)
1 libobjc.A.dylib 0x199874f54 objc_exception_throw + 60 (objc-exception.mm:565)
2 Foundation 0x182aa8e68 -[NSCoder __failWithException:] + 180 (NSCoder.m:0)
3 Foundation 0x182aa8fd4 -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 260 (NSCoder.m:1191)
4 Foundation 0x182a4689c _decodeDouble + 660 (NSKeyedArchiver.m:0)
5 Foundation 0x1829bd3cc -[NSKeyedUnarchiver decodeDoubleForKey:] + 172 (NSKeyedArchiver.m:3918)
6 *Project* 0x102eefb98 specialized Location.init(coder:) + 1936 (Location.swift:145)
7 *Project* 0x102eed854 init + 4 (<compiler-generated>:0)
8 *Project* 0x102eed854 #objc Location.init(coder:) + 32
9 Foundation 0x1829c0268 _decodeObjectBinary + 2560 (NSKeyedArchiver.m:3058)
10 Foundation 0x1829a4a78 _decodeObject + 180 (NSKeyedArchiver.m:3324)
11 Foundation 0x1829c4304 -[NSKeyedUnarchiver decodeObjectForKey:] + 176 (NSKeyedArchiver.m:3344)
12 Foundation 0x182a272b8 +[NSKeyedUnarchiver unarchiveObjectWithData:] + 84 (NSKeyedArchiver.m:2390)
13 *Project* 0x102ee729c APS.loadOnDeckFromiCloud() + 240 (APS.swift:138)
14 *Project* 0x102ee7058 APS.().init() + 860 (APS.swift:105)
15 *Project* 0x102ee6ce8 one-time initialization function for SI + 40 (APS.swift:0)
16 libdispatch.dylib 0x180e72660 _dispatch_client_callout + 20 (object.m:560)
17 libdispatch.dylib 0x180e73f08 _dispatch_once_callout + 32 (once.c:52)
18 *Project* 0x102f3417c specialized DivertToOnboardingIfNeeded_VC.viewWillAppear(_:) + 1536 (APS.swift:97)
19 *Project* 0x102f334a8 viewWillAppear + 4 (<compiler-generated>:0)
20 *Project* 0x102f334a8 #objc DivertToOnboardingIfNeeded_VC.viewWillAppear(_:) + 28
21 UIKitCore 0x1837ba3e0 -[UIViewController _setViewAppearState:isAnimating:] + 664 (UIViewController.m:5522)
22 UIKitCore 0x1838e9004 -[UIViewController __viewWillAppear:] + 120 (UIViewController.m:5654)
23 UIKitCore 0x1838ba5b0 -[UINavigationController _startTransition:fromViewController:toViewController:] + 720 (UINavigationController.m:7329)
24 UIKitCore 0x183ae657c -[UINavigationController _startDeferredTransitionIfNeeded:] + 876 (UINavigationController.m:7494)
25 UIKitCore 0x1839bb6ac -[UINavigationController __viewWillLayoutSubviews] + 168 (UINavigationController.m:7806)
26 UIKitCore 0x1838e20f4 -[UILayoutContainerView layoutSubviews] + 228 (UILayoutContainerView.m:88)
27 UIKitCore 0x18379aed8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2620 (UIView.m:18347)
28 QuartzCore 0x184ef6e24 CA::Layer::layout_if_needed(CA::Transaction*) + 536 (CALayer.mm:10038)
29 QuartzCore 0x184ee9644 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 144 (CALayer.mm:2480)
30 QuartzCore 0x184efdc6c CA::Context::commit_transaction(CA::Transaction*, double, double*) + 524 (CAContextInternal.mm:2586)
31 QuartzCore 0x184f06560 CA::Transaction::commit() + 680 (CATransactionInternal.mm:449)
32 UIKitCore 0x183963f9c __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 44 (UIApplication.m:11470)
33 CoreFoundation 0x1811d9924 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 28 (CFRunLoop.c:1820)
34 CoreFoundation 0x1811da820 __CFRunLoopDoBlocks + 412 (CFRunLoop.c:1862)
35 CoreFoundation 0x181172808 __CFRunLoopRun + 840 (CFRunLoop.c:2953)
36 CoreFoundation 0x1811863b8 CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
37 GraphicsServices 0x19cb1638c GSEventRunModal + 164 (GSEvent.c:2200)
38 UIKitCore 0x183b266a8 -[UIApplication _run] + 1100 (UIApplication.m:3493)
39 UIKitCore 0x1838a57f4 UIApplicationMain + 2092 (UIApplication.m:5046)
40 *Project* 0x102e93388 main + 68 (MapBox_VC.swift:15)
41 dyld 0x103169a24 start + 520 (dyldMain.cpp:876)
How do I check the value stored in lat to make sure it is a Double ? (Non optional)
I'm trying to generate QR code with following code :
DispatchQueue.main.async {
let image = generateQRCode(from: qrCodeString)
}
func generateQRCode(from string: String?) -> UIImage? {
if let data = string?.data(using: .utf8, allowLossyConversion: false) {
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 5, y: 5)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output)
}
}
}
return nil
}
This code works well for most devices/iOS version.
But firebase brings me a lot of device crashes: iPhone 11, XR. iOS 13.4.0
Crashed: com.apple.main-thread
0 CocoaDebug 0x1023a0adc perform_rebinding_with_section + 332
1 CocoaDebug 0x1023a087c rebind_symbols_for_image + 416
2 libdyld.dylib 0x1999c3f4c invocation function for block in dyld3::AllImages::runImageCallbacks(dyld3::Array<dyld3::LoadedImage> const&) + 244
3 libdyld.dylib 0x1999c3768 dyld3::AllImages::runImageCallbacks(dyld3::Array<dyld3::LoadedImage> const&) + 160
4 libdyld.dylib 0x1999c8dd8 dyld3::AllImages::loadImage(Diagnostics&, unsigned int, dyld3::closure::DlopenClosure const*, bool, bool, bool, bool) + 580
5 libdyld.dylib 0x1999c89ec dyld3::AllImages::dlopen(Diagnostics&, char const*, bool, bool, bool, bool, bool, void const*) + 868
6 libdyld.dylib 0x1999ca434 dyld3::dlopen_internal(char const*, int, void*) + 364
7 libdyld.dylib 0x1999bd6c0 dlopen + 116
8 CoreFoundation 0x199bcf5c8 _CFBundleDlfcnLoadBundle + 156
9 CoreFoundation 0x199af1eac _CFBundleLoadExecutableAndReturnError + 372
10 Foundation 0x199ec27a8 -[NSBundle loadAndReturnError:] + 316
11 CoreImage 0x19b479bcc invocation function for block in register_more_builtins(void (NSString*) block_pointer) + 804
12 libdispatch.dylib 0x19986833c _dispatch_client_callout + 20
13 libdispatch.dylib 0x199869a68 _dispatch_once_callout + 32
14 CoreImage 0x19b476318 register_more_builtins(void (NSString*) block_pointer) + 304
15 CoreImage 0x19b476b40 classNameIsSystemFilter(NSString*) + 112
16 CoreImage 0x19b476dbc +[CIFilter(CIFilterRegistryPrivate) filterWithName:setDefaults:] + 396
17 MYApp 0x100656598 QRCodeViewController.generateQRCode(from:) + 4370490776 (<compiler-generated>:4370490776)
18 MYApp 0x1006563a4 QRCodeViewController.viewDidLoad() + 28 (QRCodeViewController.swift:28)
19 MYApp 0x10065642c #objc QRCodeViewController.viewDidLoad() + 4370490412 (<compiler-generated>:4370490412)
20 UIKitCore 0x19d6a236c -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 104
21 UIKitCore 0x19d6a6f20 -[UIViewController loadViewIfRequired] + 952
22 UIKitCore 0x19d6a730c -[UIViewController view] + 32
23 UIKitCore 0x19d602fa4 -[UINavigationController _startCustomTransition:] + 1148
24 UIKitCore 0x19d617478 -[UINavigationController _startDeferredTransitionIfNeeded:] + 692
25 UIKitCore 0x19d618818 -[UINavigationController __viewWillLayoutSubviews] + 176
26 UIKitCore 0x19d5fb4fc -[UILayoutContainerView layoutSubviews] + 228
27 UIKitCore 0x19e1de6a0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2168
28 QuartzCore 0x1a08314a0 -[CALayer layoutSublayers] + 292
29 QuartzCore 0x1a08318e0 CA::Layer::layout_if_needed(CA::Transaction*) + 472
30 QuartzCore 0x1a0843dc4 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 144
31 QuartzCore 0x1a0788884 CA::Context::commit_transaction(CA::Transaction*, double) + 304
32 QuartzCore 0x1a07b33d0 CA::Transaction::commit() + 656
33 QuartzCore 0x1a07b3fc8 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 96
34 CoreFoundation 0x199b42c54 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
35 CoreFoundation 0x199b3d8e4 __CFRunLoopDoObservers + 420
36 CoreFoundation 0x199b3dd84 __CFRunLoopRun + 1020
37 CoreFoundation 0x199b3d660 CFRunLoopRunSpecific + 480
38 GraphicsServices 0x1a3f4e604 GSEventRunModal + 164
39 UIKitCore 0x19dd1215c UIApplicationMain + 1944
40 MYApp 0x100518e54 main + 4369190484 (<compiler-generated>:4369190484)
41 libdyld.dylib 0x1999b91ec start + 4
I tried several different encoding, I tried to do it on the main thread or in backgroud, but always without result.
it looks like the whole class CIFilter causes a crash !!
I figured out how to fix it. The problem was the third-party CocoaDebug ! I remove the pod and I have no more crashes! Thanks!
let task = URLSession.shared.dataTask(with: postRequest) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "NoData")
return
}
let jsonString = String(data: data, encoding: String.Encoding.utf8)!
self.submitView.text = jsonString
}
task.resume()
I'm trying to access the return value of this post request. I set a breakpoint and saw that jsonString does get the correct value. However, which I try to set self.submitView.text to anything, I get this error:
2017-06-21 14:20:16.062815-0400 Button[958:197756] This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
Stack:(
0 CoreFoundation 0x0000000191c82ff8 <redacted> + 148
1 libobjc.A.dylib 0x00000001906e4538 objc_exception_throw + 56
2 CoreFoundation 0x0000000191c82f28 <redacted> + 0
3 Foundation 0x0000000192874338 <redacted> + 128
4 Foundation 0x00000001926bc9f8 <redacted> + 36
5 UIKit 0x0000000197daf76c <redacted> + 816
6 UIKit 0x0000000197dbb668 <redacted> + 1740
7 UIKit 0x0000000197dbaf84 <redacted> + 828
8 UIKit 0x0000000197df90f4 <redacted> + 256
9 UIKit 0x0000000197df1818 <redacted> + 116
10 UIKit 0x00000001987ec308 <redacted> + 48
11 UIKit 0x0000000197e5d880 <redacted> + 280
12 UIFoundation 0x0000000197cdb238 <redacted> + 7012
13 UIFoundation 0x0000000197cdc0c8 <redacted> + 52
14 UIFoundation 0x0000000197d0eb70 <redacted> + 48
15 UIKit 0x0000000197e5d85c <redacted> + 244
16 UIFoundation 0x0000000197ce0214 <redacted> + 2172
17 UIFoundation 0x0000000197d0b058 <redacted> + 240
18 UIFoundation 0x0000000197d2f474 <redacted> + 160
19 UIFoundation 0x0000000197d2ebb4 <redacted> + 92
20 UIKit 0x0000000197e60500 <redacted> + 252
21 UIKit 0x0000000197e603e4 <redacted> + 196
22 Button 0x0000000100061e24 _TFFC6Button14ViewController12submitTappedFT_T_U_FTGSqV10Foundation4Data_GSqCSo11URLResponse_GSqPs5Error___T_ + 784
23 Button 0x0000000100062468 _TTRXFo_oGSqV10Foundation4Data_oGSqCSo11URLResponse_oGSqPs5Error____XFdCb_dGSqCSo6NSData_dGSqS1__dGSqCSo7NSError___ + 224
24 CFNetwork 0x000000019228c34c <redacted> + 32
25 CFNetwork 0x00000001922a4048 <redacted> + 148
26 Foundation 0x0000000192751814 <redacted> + 16
27 Foundation 0x0000000192696770 <redacted> + 96
28 Foundation 0x0000000192686b28 <redacted> + 612
29 Foundation 0x0000000192753bb0 <redacted> + 228
30 libdispatch.dylib 0x0000000100a6da10 _dispatch_client_callout + 16
31 libdispatch.dylib 0x0000000100a7b2e8 _dispatch_queue_serial_drain + 1140
32 libdispatch.dylib 0x0000000100a71634 _dispatch_queue_invoke + 852
33 libdispatch.dylib 0x0000000100a7d630 _dispatch_root_queue_drain + 552
34 libdispatch.dylib 0x0000000100a7d39c _dispatch_worker_thread3 + 140
35 libsystem_pthread.dylib 0x0000000190d43100 _pthread_wqthread + 1096
36 libsystem_pthread.dylib 0x0000000190d42cac start_wqthread + 4
)
2017-06-21 14:20:16.085280-0400 Button[958:197756] This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
Stack:(
0 CoreFoundation 0x0000000191c82ff8 <redacted> + 148
1 libobjc.A.dylib 0x00000001906e4538 objc_exception_throw + 56
2 CoreFoundation 0x0000000191c82f28 <redacted> + 0
3 Foundation 0x0000000192874338 <redacted> + 128
4 Foundation 0x00000001926bc9f8 <redacted> + 36
5 UIKit 0x0000000197eb18e0 <redacted> + 564
6 UIKit 0x0000000197db0138 <redacted> + 224
7 UIKit 0x0000000197daf8d0 <redacted> + 120
8 Foundation 0x00000001926bca7c <redacted> + 168
9 UIKit 0x0000000197daf76c <redacted> + 816
10 UIKit 0x0000000197dbb668 <redacted> + 1740
11 UIKit 0x0000000197dbaf84 <redacted> + 828
12 UIKit 0x0000000197df90f4 <redacted> + 256
13 UIKit 0x0000000197df1818 <redacted> + 116
14 UIKit 0x00000001987ec308 <redacted> + 48
15 UIKit 0x0000000197e5d880 <redacted> + 280
16 UIFoundation 0x0000000197cdb238 <redacted> + 7012
17 UIFoundation 0x0000000197cdc0c8 <redacted> + 52
18 UIFoundation 0x0000000197d0eb70 <redacted> + 48
19 UIKit 0x0000000197e5d85c <redacted> + 244
20 UIFoundation 0x0000000197ce0214 <redacted> + 2172
21 UIFoundation 0x0000000197d0b058 <redacted> + 240
22 UIFoundation 0x0000000197d2f474 <redacted> + 160
23 UIFoundation 0x0000000197d2ebb4 <redacted> + 92
24 UIKit 0x0000000197e60500 <redacted> + 252
25 UIKit 0x0000000197e603e4 <redacted> + 196
26 Button 0x0000000100061e24 _TFFC6Button14ViewController12submitTappedFT_T_U_FTGSqV10Foundation4Data_GSqCSo11URLResponse_GSqPs5Error___T_ + 784
27 Button 0x0000000100062468 _TTRXFo_oGSqV10Foundation4Data_oGSqCSo11URLResponse_oGSqPs5Error____XFdCb_dGSqCSo6NSData_dGSqS1__dGSqCSo7NSError___ + 224
28 CFNetwork 0x000000019228c34c <redacted> + 32
29 CFNetwork 0x00000001922a4048 <redacted> + 148
30 Foundation 0x0000000192751814 <redacted> + 16
31 Foundation 0x0000000192696770 <redacted> + 96
32 Foundation 0x0000000192686b28 <redacted> + 612
33 Foundation 0x0000000192753bb0 <redacted> + 228
34 libdispatch.dylib 0x0000000100a6da10 _dispatch_client_callout + 16
35 libdispatch.dylib 0x0000000100a7b2e8 _dispatch_queue_serial_drain + 1140
36 libdispatch.dylib 0x0000000100a71634 _dispatch_queue_invoke + 852
37 libdispatch.dylib 0x0000000100a7d630 _dispatch_root_queue_drain + 552
38 libdispatch.dylib 0x0000000100a7d39c _dispatch_worker_thread3 + 140
39 libsystem_pthread.dylib 0x0000000190d43100 _pthread_wqthread + 1096
40 libsystem_pthread.dylib 0x0000000190d42cac start_wqthread + 4
)
2017-06-21 14:20:16.088842-0400 Button[958:197756] *** Assertion failure in void _UIPerformResizeOfTextViewForTextContainer(NSLayoutManager *, UIView<NSTextContainerView> *, NSTextContainer *, NSUInteger)(), /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIFoundation/UIFoundation-491.7/UIFoundation/TextSystem/NSLayoutManager_Private.m:1577
2017-06-21 14:20:16.089727-0400 Button[958:197756] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
*** First throw call stack:
(0x191c82fe0 0x1906e4538 0x191c82eb4 0x19271a78c 0x197cd93f8 0x197cd90d8 0x197d07e88 0x197d0b188 0x197d2f474 0x197d2ebb4 0x197e60500 0x197e603e4 0x100061e24 0x100062468 0x19228c34c 0x1922a4048 0x192751814 0x192696770 0x192686b28 0x192753bb0 0x100a6da10 0x100a7b2e8 0x100a71634 0x100a7d630 0x100a7d39c 0x190d43100 0x190d42cac)
libc++abi.dylib: terminating with uncaught exception of type NSException
It seems like I can't assign self.submitView.text to anything inside the scope of "task". Any ideas why?
Edit:
Here is what the server returns upon sending a request.
"{\"raw\":\"schedule for John in New York on Monday\",\"location\":\"New York\",\"date\":\"Mon, June 26\",\"name\":\"john\",\"errorResponse\":null,\"stripTest\":\"for John in new york on Monday\"}"
This is what my code looks like:
do {
let info = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String : Any]
DispatchQueue.main.async {
self.submitView.text = info?["name"] as! String
}
} catch {
DispatchQueue.main.async {
self.submitView.text = "ERROR"
}
}
"ERROR" is no longer printing, but "info" seems to have 0 key/value pairs, thus nothing prints.
UPDATE:
print(try? JSONSerialization.jsonObject(with: data, options: [])) prints "nil"
The latter prints:
Optional("\"{\\\"raw\\\":\\\"Schedule for John in New York on Monday\\\",\\\"location\\\":\\\"new york\\\",\\\"date\\\":\\\"6/26/2017 12:00:00 AM\\\",\\\"name\\\":\\\"john\\\",\\\"errorResponse\\\":null,\\\"stripTest\\\":\\\" for john in new york on monday \\\"}\"")
This isn't what prints when I assign it to a text view in the UI.. is it converting the escape sequences to literal characters?
You are setting the text property of the UITextField on a background thread.
You need to ensure that all UI work is done on the main thread, You can do this using DispatchQueue...
DispatchQueue.main.async {
self.submitView.text = jsonString
}
Update:
The data is literally the data that was sent over the network, you will need to convert and deserialise it.
You can do this using the JSON Serialisation class like so:
// I create some dummy JSON string for example purposes
let data = "{ \"name\": \"John Smith\"}".data(using: String.Encoding.utf8)!
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:AnyObject] {
print(jsonObject["name"] as! String)
}
This tries to convert the data into a JSON object, which in Swift is usually cast to a dictionary object.
There are loads of resources on networking with Swift available. One I highly recommend would be iOS Networking with Swift this would take a fair bit of time to go through and learn so here is something that is alot quicker to get through Working with JSON in Swift
UPDATE
This code works for me:
let data = "{\"raw\":\"schedule for John in New York on Monday\",\"location\":\"New York\",\"date\":\"Mon, June 26\",\"name\":\"john\",\"errorResponse\":null,\"stripTest\":\"for John in new york on Monday\"}".data(using: String.Encoding.utf8)!
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:AnyObject] {
print(jsonObject)
print(jsonObject["name"] as! String)
}
I'm getting these crash reports from XCode (I pasted one at the bottom) and I can't reproduce the error or figure out what the problem is.
From the backtrace I'm thinking it could be related to the decodeObject stuff… I did have some problems with the new decodeBool and decodeDouble functions, so to be sure I made an NSCoder extension:
func safeDecodeDouble(key: String) -> Double? {
if self.containsValue(forKey: key) {
if let value = self.decodeObject(forKey: key) as? Double {
return value
}
return self.decodeDouble(forKey: key)
}
return nil
}
func safeDecodeBool(key: String) -> Bool? {
if self.containsValue(forKey: key) {
if let value = self.decodeObject(forKey: key) as? Bool {
return value
}
return self.decodeBool(forKey: key)
}
return nil
}
func safeDecodeDate(key: String) -> Date? {
if self.containsValue(forKey: key) {
if let value = self.decodeObject(forKey: key) as? Date {
return value
}
}
return nil
}
func safeDecodeString(key: String) -> String? {
if self.containsValue(forKey: key) {
if let value = self.decodeObject(forKey: key) as? String {
return value
}
}
return nil
}
Now I only use the safeDecode functions in my code, but I'm still getting those same crash reports.
The third item in the backtrace says [UIStoryboard storyboardWithName:bundle:], but I don't know what that could have to do with anything. Also every crash report has something about state restoration and decodeObject, so I'm thinking the problem isn't the storyboard instantiation per se.
At the same time I never have any problems when I test state restoration on the simulator or on my devices, so apparently it's only happening to some people.
Any ideas on what could be going on here?
Last Exception Backtrace:
0 CoreFoundation 0x18a146fd8 __exceptionPreprocess + 124 (NSException.m:165)
1 libobjc.A.dylib 0x188ba8538 objc_exception_throw + 56 (objc-exception.mm:521)
2 UIKit 0x190a36850 +[UIStoryboard storyboardWithName:bundle:] + 776 (UIStoryboard.m:99)
3 UIKit 0x190bfacfc -[_UIStoryboardProxy initWithCoder:] + 192 (UIStateRestorationSupport.m:324)
4 Foundation 0x18ab96420 _decodeObjectBinary + 2076 (NSKeyedArchiver.m:2304)
5 Foundation 0x18ab95b58 _decodeObject + 308 (NSKeyedArchiver.m:2467)
6 UIKit 0x1903e62ec -[UIStateRestorationKeyedUnarchiver decodeObjectForKey:] + 88 (UIStateRestorationSupport.m:454)
7 UIKit 0x1903e5ba8 -[UIApplication(StateRestoration) _restoreApplicationPreservationStateWithSessionIdentifier:beginHandler:completionHandler:] + 4772 (UIApplication.m:13551)
8 UIKit 0x1902e76b4 -[UIApplication(StateRestoration) _doRestorationIfNecessary] + 244 (UIApplication.m:13979)
9 UIKit 0x1902e7288 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 296 (UIApplication.m:1792)
10 UIKit 0x1904f3800 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3452 (UIApplication.m:2129)
11 UIKit 0x1904f92a8 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1684 (UIApplication.m:3625)
12 UIKit 0x19050dde0 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke.3151 + 48 (UIApplication.m:10365)
13 UIKit 0x1904f653c -[UIApplication workspaceDidEndTransaction:] + 168 (UIApplication.m:2990)
14 FrontBoardServices 0x18bcef884 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36 (FBSSerialQueue.m:158)
15 FrontBoardServices 0x18bcef6f0 -[FBSSerialQueue _performNext] + 176 (FBSSerialQueue.m:177)
16 FrontBoardServices 0x18bcefaa0 -[FBSSerialQueue _performNextFromRunLoopSource] + 56 (FBSSerialQueue.m:206)
17 CoreFoundation 0x18a0f5424 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1943)
18 CoreFoundation 0x18a0f4d94 __CFRunLoopDoSources0 + 540 (CFRunLoop.c:1989)
19 CoreFoundation 0x18a0f29a0 __CFRunLoopRun + 744 (CFRunLoop.c:2821)
20 CoreFoundation 0x18a022d94 CFRunLoopRunSpecific + 424 (CFRunLoop.c:3113)
21 UIKit 0x1902e045c -[UIApplication _run] + 652 (UIApplication.m:2658)
22 UIKit 0x1902db130 UIApplicationMain + 208 (UIApplication.m:4089)
23 Leio 0x1000b6ecc main + 120 (BookInfoTableViewController.swift:19)
24 libdyld.dylib 0x18903159c start + 4
Edit: Here's a different crash report that showed up and suggests there really is a problem with state restoration. I can never reproduce it myself, though. :/
0 CoreFoundation 0x18385adb0 __exceptionPreprocess + 124 (NSException.m:162)
1 libobjc.A.dylib 0x182ebff80 objc_exception_throw + 56 (objc-exception.mm:531)
2 Foundation 0x1841d4704 -[NSCoder(Exceptions) __failWithException:] + 132 (NSCoder.m:544)
3 Foundation 0x1841d48bc -[NSCoder(Exceptions) __failWithExceptionName:errorCode:format:] + 440 (NSCoder.m:580)
4 Foundation 0x1841a33b8 _decodeObjectBinary + 2996 (NSKeyedArchiver.m:2173)
5 Foundation 0x1841a274c _decodeObject + 304 (NSKeyedArchiver.m:2313)
6 UIKit 0x188b3e20c -[UIStateRestorationKeyedUnarchiver decodeObjectForKey:] + 88 (UIStateRestorationSupport.m:454)
7 UIKit 0x188b3daf8 -[UIApplication(StateRestoration) _restoreApplicationPreservationStateWithSessionIdentifier:beginHandler:completionHandler:] + 4908 (UIApplication.m:15043)
8 UIKit 0x188a32d44 -[UIApplication(StateRestoration) _doRestorationIfNecessary] + 244 (UIApplication.m:15471)
9 UIKit 0x188a32964 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 308 (UIApplication.m:1860)
10 UIKit 0x188c62184 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2904 (UIApplication.m:2112)
11 UIKit 0x188c665f0 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1684 (UIApplication.m:3355)
12 UIKit 0x188c63764 -[UIApplication workspaceDidEndTransaction:] + 168 (UIApplication.m:2748)
13 FrontBoardServices 0x1851fb7ac __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36 (FBSSerialQueue.m:158)
14 FrontBoardServices 0x1851fb618 -[FBSSerialQueue _performNext] + 168 (FBSSerialQueue.m:177)
15 FrontBoardServices 0x1851fb9c8 -[FBSSerialQueue _performNextFromRunLoopSource] + 56 (FBSSerialQueue.m:206)
16 CoreFoundation 0x18381109c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CFRunLoop.c:1761)
17 CoreFoundation 0x183810b30 __CFRunLoopDoSources0 + 540 (CFRunLoop.c:1807)
18 CoreFoundation 0x18380e830 __CFRunLoopRun + 724 (CFRunLoop.c:2536)
19 CoreFoundation 0x183738c50 CFRunLoopRunSpecific + 384 (CFRunLoop.c:2814)
20 UIKit 0x188a2b94c -[UIApplication _run] + 460 (UIApplication.m:2578)
21 UIKit 0x188a26088 UIApplicationMain + 204 (UIApplication.m:3772)
22 Leio 0x1000f6ecc main + 120 (BookInfoTableViewController.swift:19)
23 libdyld.dylib 0x1832d68b8 start + 4 (start_glue.s:78)