Trouble closing windows, saving and opening in chapter 26


#1

When I build and run VocalTextEdit at the end of the chapter (before attempting the Silver or Gold challenge), I have three problems:

(1) Whenever I click the red circle with an x in it in the top left corner of a VocalTextEdit window in the attempt to close a document, the computer ‘thinks’ for a few seconds and nothing changes. The following lengthy error message gets logged in XCode:

2018-06-22 21:25:16.891136-0400 VocalTextEdit[2072:93500] *** Assertion failure in -[NSVBSavePanel viewWillInvalidate:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1561.40.112/Nav.subproj/OpenAndSavePanelRemote/NSVBOpenAndSavePanels.m:387
2018-06-22 21:25:16.902858-0400 VocalTextEdit[2072:93500] -[NSVBSavePanel init] caught non-fatal NSInternalInconsistencyException ‘bridge absent’ with backtrace (
0 CoreFoundation 0x00007fff470316bb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff6e240942 objc_exception_throw + 48
2 CoreFoundation 0x00007fff470372a2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fff4917c340 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 AppKit 0x00007fff447ac102 -[NSVBSavePanel viewWillInvalidate:] + 188
5 ViewBridge 0x00007fff6ba626f2 -[NSRemoteView invalidate:] + 292
6 ViewBridge 0x00007fff6ba6fe88 -[NSRemoteView _advanceToConfigPhaseLegacy] + 1111
7 ViewBridge 0x00007fff6ba70d1b -[NSRemoteView _viewServiceMarshalProxy:withDetailedErrorHandler:] + 230
8 ViewBridge 0x00007fff6ba71104 -[NSRemoteView _viewServiceMarshalProxy:withErrorHandler:] + 78
9 ViewBridge 0x00007fff6ba63194 -[NSRemoteView bridge] + 227
10 AppKit 0x00007fff44767a52 -[NSVBSavePanel init] + 292
11 AppKit 0x00007fff447675fd +[NSSavePanel _crunchyRawUnbonedPanel] + 72
12 AppKit 0x00007fff450dbaf6 -[NSDocument(NSDocumentSaving) _preparedSavePanelForOperation:] + 263
13 AppKit 0x00007fff450dc558 __104-[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke_2 + 317
14 AppKit 0x00007fff4486edc2 -[NSDocument _commitEditingThenContinue:] + 472
15 AppKit 0x00007fff4486ebe1 -[NSDocument _commitEditingWithDelegate:didSomethingSelector:contextInfo:thenContinue:] + 94
16 AppKit 0x00007fff450dc40c __104-[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke + 337
17 AppKit 0x00007fff450dc2b5 -[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:] + 99
18 AppKit 0x00007fff450d95d2 __86-[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:]_block_invoke_3 + 130
19 AppKit 0x00007fff44b1e4f0 -[NSDocument(NSDocumentSerializationAPIs) _continueActivityUsingBlock:] + 304
20 AppKit 0x00007fff450d9409 __86-[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:]_block_invoke_2 + 482
21 AppKit 0x00007fff4486edc2 -[NSDocument _commitEditingThenContinue:] + 472
22 AppKit 0x00007fff4486ebe1 -[NSDocument _commitEditingWithDelegate:didSomethingSelector:contextInfo:thenContinue:] + 94
23 AppKit 0x00007fff450d9218 __86-[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:]_block_invoke + 329
24 AppKit 0x00007fff450d90c9 -[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:] + 95
25 AppKit 0x00007fff44a6efa3 __75-[NSDocument canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:]_block_invoke_2.2660 + 143
26 AppKit 0x00007fff44b1e4f0 -[NSDocument(NSDocumentSerializationAPIs) _continueActivityUsingBlock:] + 304
27 AppKit 0x00007fff4486ddc1 __75-[NSDocument canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:]_block_invoke + 577
28 AppKit 0x00007fff44b1cd98 -[NSDocument(NSDocumentSerializationAPIs) _performActivity:] + 1565
29 AppKit 0x00007fff44b1e316 -[NSDocument(NSDocumentSerializationAPIs) performActivityWithSynchronousWaiting:usingBlock:cancellationHandler:] + 448
30 AppKit 0x00007fff4486db21 -[NSDocument canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:] + 286
31 AppKit 0x00007fff4482d4cb -[NSWindow __close] + 281
32 AppKit 0x00007fff44cd2a43 -[NSApplication(NSResponder) sendAction:to:from:] + 312
33 AppKit 0x00007fff4477853f -[NSControl sendAction:to:] + 86
34 AppKit 0x00007fff44778467 __26-[NSCell _sendActionFrom:]_block_invoke + 136
35 AppKit 0x00007fff4477836d -[NSCell _sendActionFrom:] + 183
36 AppKit 0x00007fff447b9688 -[NSButtonCell _sendActionFrom:] + 97
37 AppKit 0x00007fff44776bd6 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2438
38 AppKit 0x00007fff447b93cf -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 777
39 AppKit 0x00007fff44775670 -[NSControl mouseDown:] + 965
40 AppKit 0x00007fff4480fbd9 -[_NSThemeWidget mouseDown:] + 86
41 AppKit 0x00007fff44e71d6d -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5891
42 AppKit 0x00007fff44e6e9c4 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2359
43 AppKit 0x00007fff44e6dc70 -[NSWindow(NSEventRouting) sendEvent:] + 497
44 AppKit 0x00007fff44ccf236 -[NSApplication(NSEvent) sendEvent:] + 2462
45 AppKit 0x00007fff4452f8b5 -[NSApplication run] + 812
46 AppKit 0x00007fff444fea72 NSApplicationMain + 804
47 VocalTextEdit 0x000000010000338d main + 13
48 libdyld.dylib 0x00007fff6ee57015 start + 1
)

(2) When I click “Save” from the File menu when VocalTextEdit is running, nothing seems to happen. But the following lengthy error message is logged in XCode:

2018-06-22 21:26:55.633154-0400 VocalTextEdit[2077:94479] *** Assertion failure in -[NSVBSavePanel viewWillInvalidate:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1561.40.112/Nav.subproj/OpenAndSavePanelRemote/NSVBOpenAndSavePanels.m:387
2018-06-22 21:26:55.645240-0400 VocalTextEdit[2077:94479] -[NSVBSavePanel init] caught non-fatal NSInternalInconsistencyException ‘bridge absent’ with backtrace (
0 CoreFoundation 0x00007fff470316bb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fff6e240942 objc_exception_throw + 48
2 CoreFoundation 0x00007fff470372a2 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fff4917c340 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
4 AppKit 0x00007fff447ac102 -[NSVBSavePanel viewWillInvalidate:] + 188
5 ViewBridge 0x00007fff6ba626f2 -[NSRemoteView invalidate:] + 292
6 ViewBridge 0x00007fff6ba6fe88 -[NSRemoteView _advanceToConfigPhaseLegacy] + 1111
7 ViewBridge 0x00007fff6ba70d1b -[NSRemoteView _viewServiceMarshalProxy:withDetailedErrorHandler:] + 230
8 ViewBridge 0x00007fff6ba71104 -[NSRemoteView _viewServiceMarshalProxy:withErrorHandler:] + 78
9 ViewBridge 0x00007fff6ba63194 -[NSRemoteView bridge] + 227
10 AppKit 0x00007fff44767a52 -[NSVBSavePanel init] + 292
11 AppKit 0x00007fff447675fd +[NSSavePanel _crunchyRawUnbonedPanel] + 72
12 AppKit 0x00007fff450dbaf6 -[NSDocument(NSDocumentSaving) _preparedSavePanelForOperation:] + 263
13 AppKit 0x00007fff450dc558 __104-[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke_2 + 317
14 AppKit 0x00007fff4486edc2 -[NSDocument _commitEditingThenContinue:] + 472
15 AppKit 0x00007fff4486ebe1 -[NSDocument _commitEditingWithDelegate:didSomethingSelector:contextInfo:thenContinue:] + 94
16 AppKit 0x00007fff450dc40c __104-[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:]_block_invoke + 337
17 AppKit 0x00007fff450dc2b5 -[NSDocument(NSDocumentSaving) _runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:] + 99
18 AppKit 0x00007fff450d95d2 __86-[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:]_block_invoke_3 + 130
19 AppKit 0x00007fff44b1e4f0 -[NSDocument(NSDocumentSerializationAPIs) _continueActivityUsingBlock:] + 304
20 AppKit 0x00007fff450d9409 __86-[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:]_block_invoke_2 + 482
21 AppKit 0x00007fff4486edc2 -[NSDocument _commitEditingThenContinue:] + 472
22 AppKit 0x00007fff4486ebe1 -[NSDocument _commitEditingWithDelegate:didSomethingSelector:contextInfo:thenContinue:] + 94
23 AppKit 0x00007fff450d9218 __86-[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:]_block_invoke + 329
24 AppKit 0x00007fff44b1cd98 -[NSDocument(NSDocumentSerializationAPIs) _performActivity:] + 1565
25 AppKit 0x00007fff44b1e316 -[NSDocument(NSDocumentSerializationAPIs) performActivityWithSynchronousWaiting:usingBlock:cancellationHandler:] + 448
26 AppKit 0x00007fff450d90c9 -[NSDocument(NSDocumentSaving) _saveDocumentWithDelegate:didSaveSelector:contextInfo:] + 95
27 AppKit 0x00007fff44cd2a43 -[NSApplication(NSResponder) sendAction:to:from:] + 312
28 AppKit 0x00007fff44767213 -[NSMenuItem _corePerformAction] + 323
29 AppKit 0x00007fff44766f9b -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 114
30 AppKit 0x00007fff447ecc7a -[NSMenu performActionForItemAtIndex:] + 133
31 AppKit 0x00007fff447ecbe5 -[NSMenu _internalPerformActionForItemAtIndex:] + 94
32 AppKit 0x00007fff447eca15 -[NSCarbonMenuImpl _carbonCommandProcessEvent:handlerCallRef:] + 106
33 AppKit 0x00007fff446b32a2 NSSLMMenuEventHandler + 882
34 HIToolbox 0x00007fff46261904 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1541
35 HIToolbox 0x00007fff46260c4d _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 374
36 HIToolbox 0x00007fff46275f21 SendEventToEventTarget + 39
37 HIToolbox 0x00007fff462bf3f9 _ZL18SendHICommandEventjPK9HICommandjjhPKvP20OpaqueEventTargetRefS5_PP14OpaqueEventRef + 411
38 HIToolbox 0x00007fff462e82ee SendMenuCommandWithContextAndModifiers + 49
39 HIToolbox 0x00007fff462e82aa SendMenuItemSelectedEvent + 188
40 HIToolbox 0x00007fff462e8184 ZL19FinishMenuSelectionP13SelectionDataP10MenuResultS2 + 96
41 HIToolbox 0x00007fff462e8b26 _ZL14MenuSelectCoreP8MenuData5PointdjPP13OpaqueMenuRefPt + 559
42 HIToolbox 0x00007fff462e8866 _HandleMenuSelection2 + 472
43 AppKit 0x00007fff446a4acf _NSHandleCarbonMenuEvent + 244
44 AppKit 0x00007fff448fedde _DPSEventHandledByCarbon + 54
45 AppKit 0x00007fff44cd05e2 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 914
46 AppKit 0x00007fff4452f885 -[NSApplication run] + 764
47 AppKit 0x00007fff444fea72 NSApplicationMain + 804
48 VocalTextEdit 0x000000010000338d main + 13
49 libdyld.dylib 0x00007fff6ee57015 start + 1
)

(3) Finally, I can’t load any documents, but I suspect that’s because I haven’t managed to save them in the first place.

The “speak” and “stop” features work as they should. Besides that, some sort of autosaving must be going on . . . because any text left in the field of VocalTextEdit at the time I stop the program reappears there the next time I build and run the program.

Elucidating this may be too much trouble! But if anyone would like to give it a go, I’d be grateful! Here is all my code from Document.Swift:

//
// Document.swift
// VocalTextEdit
//
// Created by MacBook Air on 6/22/18.
// Copyright © 2018 BigNerdRanch. All rights reserved.
//

import Cocoa

class Document: NSDocument {
enum Error: Swift.Error, LocalizedError {
case UTF8Encoding
case UTF8Decoding

    var failureReason: String? {
        switch self {
        case .UTF8Encoding: return "File cannot be encoded in UTF-8."
        case .UTF8Decoding: return "File is not valid UTF-8."
        }
    }
}

var contents: String = ""

override class var autosavesInPlace: Bool {
    return true
}

override func makeWindowControllers() {
    // Returns the Storyboard that contains your Document window.
    let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
    let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController
    
    let viewController = windowController.contentViewController as! ViewController
    viewController.textView.string = contents
    
    self.addWindowController(windowController)
}

override func data(ofType typeName: String) throws -> Data {
    let windowController = windowControllers[0]
    let viewController = windowController.contentViewController as! ViewController
    let contents = viewController.textView.string ?? ""
    
    guard let data = contents.data(using: .utf8) else {
        throw Document.Error.UTF8Encoding
    }
    
    return data
}

override func read(from data: Data, ofType typeName: String) throws {
    guard let contents = String(data: data, encoding: .utf8) else {
        throw Document.Error.UTF8Decoding
}

self.contents = contents

}

}


#2

Did you grant the application the required permission to save files?

Because applications by default are sand-boxed, you need to grand them permissions to access system resources.