NSManagedObjectModel(contentsOf:) is incorrectly marked as returning non-optional
Originator: | arkadiusz.holko | ||
Number: | rdar://33773917 | Date Originated: | 08-Aug-2017 02:27 PM |
Status: | Closed | Resolved: | |
Product: | iOS + SDK | Product Version: | iOS 11 beta 5 |
Classification: | Serious Bug | Reproducible: | Always |
Summary: In iOS 11 Beta 5 there was a change in NSManagedObjectModel’s API: From - (nullable instancetype)initWithContentsOfURL:(nonnull NSURL *)url To - (nonnull instancetype)initWithContentsOfURL:(nonnull NSURL *)url This is incorrect - we can get a nil when we pass an incorrect URL. I noticed this because I’m observing issues with failures to load .omo file under iOS 11 (related radar: 33573724). Steps to Reproduce: 1. Create a new empty iOS project (e.g. from a “Single View App” template). 2. Add this line somewhere `let model = NSManagedObjectModel(contentsOf: URL(fileURLWithPath: ""))` so that it executes when the app runs (e.g. in `application(:didFinishLaunchingWithOptions:)`). 3. Run the app in Simulator or on a device. Expected Results: According to the nullability annotation, `model` variable should never be nil. Actual Results: `model` variable is actually nil, even though the API tells that it shouldn’t be. These messages are printed to the standard output: ``` 2017-08-08 14:10:29.239494+0200 ManagedObjectModelRadar[3172:45012499] [error] error: Failed to load NSManagedObjectModel with URL './ -- file:///' CoreData: error: Failed to load NSManagedObjectModel with URL './ -- file:///' ``` If the initializer can return nil, it should be annotated properly. If there are some errors, they should be returned by throwing (in Swift), not printing to the stdout. Version: iOS 11 beta 5 Notes: Can be worked around by manually annotating the variable type as optional, e.g.: ``` let model: NSManagedObjectModel? = NSManagedObjectModel(contentsOf: url) ``` I haven’t looked into those but I think similar issues occur in other CD-related changes in iOS 11 beta 5: http://codeworkshop.net/objc-diff/sdkdiffs/ios/11.0b5/CoreData.html
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!