Chapter 7, page 347 cant dispatch .NSManagedObjectContextDidSave

Type ‘String’ has no member ‘NSManagedObjectContextDidSave’

forNotification: .NSManagedObjectContextDidSave,
object: coreDataStack.mainContext) {
notification in
return true

I change it to

expectation(forNotification: NSNotification.Name.NSManagedObjectContextDidSave.rawValue, object: coreDataStack.mainContext) { notification in
return true

The test fails with following error message:
XCTAssertNil failed: “Error Code=0 “(null)”” - Save did not occur

1 Like

I saw the error

Type ‘String’ has no member ‘NSManagedObjectContextDidSave’

Looked up and found this thread, but no working answer.

I looked at the finished project and it builds and runs.

I then ran FileMerge between my working version and the final project.

I noticed in the project.pbxproj noticed this difference:


The version of the book I have is suppose to be swift 4.0 I did in Xcode:

  • select project file
  • select the CampgroundManagerTests target
  • select the “Build Settings” group
  • type “swift” in the search window
  • scroll down to “Swift Language Version” change it to “4.0”

The test then builds and runs using the code from the book:

expectation(forNotification: .NSManagedObjectContextDidSave,
    object: coreDataStack.mainContext) {
    notification in
    return true

Thank you @bobcampbell! It solved the compile error.
The save test still doesn’t work though but I believe that the problem is somewhere else in my code!
Thanks again!

This error blew an hour of my time, thank you Bob!! I went through the same steps as you, with the exception of the FileMerge, that was a great idea. Thanks for the very clear steps as to resolve.

Even after following @bobcampbell’s instructions I could not get my program to pass the save tests. Various attempts at finding differences between my code and the finished version of the code (which works) did not find any so I decided to start over with the chapter. I trashed my entire 07-unit-testing folder and extracted a new copy from the .zip file.

The first thing I did after opening the starter project was check the language used for the test project. It was already set to Swift 4.0. When I added the Swift file TestCoreDataStack the test project’s language setting changed to Swift 3.0. I reset it back to 4.0 then went through the chapter’s exercises again. This time everything worked properly.

I don’t know why the language changed when the new .swift file was added (Xcode bug maybe?) but, making sure to reset it before adding any new code seems to have resolved my issues.

If anyone else is still having problems passing the save test(s) after making the language change, try starting from a fresh copy of the project.

Thanks @bobcampbell solved my problem and the tests pass now. This was the first thread that showed up in google search so 2 seconds to solve the problem woohoo :slight_smile:

This topic was automatically closed after 166 days. New replies are no longer allowed.