<div dir="ltr">Apologies if this has been discussed, I couldn't find any mention in the list or on <a href="https://github.com/apple/swift-3-api-guidelines-review">https://github.com/apple/swift-3-api-guidelines-review</a>.<br><br>The NSIncrementalStore method:<br><br> func referenceObjectForObjectID(_ objectID: NSManagedObjectID) -> AnyObject<br><br>Can thrown an exception, but it's not marked as throws. From the documentation:<br><br>"This method raises an NSInvalidArgumentException if the object ID was not created by the receiving store."<br><br>NSExpression has a constantValue property:<div><br></div> var constantValue: AnyObject { get }<div><br></div><div>that can raise an exception at runtime, but it's not marked as throws. From the documentation:<br><br>'Accessing this property raises an exception if it is not applicable to the expression."<br><br>The NSExpression constantValue property can also return nil at runtime, but it's return type is AnyObject; not an optional.<br><br>If you construct an NSComparisonPredicate with a format string like:<br><br> "property == nil"<br><br>and you examine the expression returned by predicate.rightExpression. You can't call expression.constantValue from Swift because it'll crash with EXC_BAD_ACCESS. You also can't protect against a nil value because the return type is not optional.<br><br><br>To protect Swift against both of these situations, you have to use Objective-C wrapper functions. For example to protect against exceptions:</div><div><br></div>NSException *_Nullable IADCatchObjCException(void (^_Nullable block)())<br>{<br> NSException *exception = nil;<br> @try {<br> if (block) {<br> block();<br> }<br> } @catch (NSException *e) {<br> exception = e;<br> }<br> return exception;<br>} <div><br></div><div>To protect against a surprise nil at runtime:</div><div><br></div>inline _Nullable id IADMaybeNilValue(_Nullable id value)<br>{<br> return value ?: nil;<br>}<div><br></div><div>I was hoping this could be improved for the Swift API?</div><div><br>--<br>Andrew Tetlaw<br><div><div>
</div></div></div></div>