[swift-corelibs-dev] NSAttributedString attributesAtIndex return an optional.

Philippe Hausler phausler at apple.com
Tue Dec 8 09:59:07 CST 2015


NSException != throw in swift…
NSExceptions should be treated as assert, fatalError, or precondition (precondition in this case is probably preferred)


> On Dec 8, 2015, at 7:31 AM, James Lee via swift-corelibs-dev <swift-corelibs-dev at swift.org> wrote:
> 
> 
>> 
>> Personally, if I indexed past the end of an attributed string, I would expect a precondition to fail and (since Swift can't catch assertion failures) my app to crash. (And from what I can tell, the Apple Foundation versions of these APIs do not return an optional.)
>> 
>> -- 
>> Brent Royal-Gordon
>> Architechies
>> 
> 
> It is worth noting that the current OSX documentation does raise an NSRangeException in the case discussed above. With the throw capabilities in Swift 2 this makes sense.
> 
> As you have said, it seems a lot of Foundation API's do not return Optionals. I feel this a hangover from ObjC where an empty object (in this case an NSDictionary) would be returned. To me this is what Optionals are there to replace. 
> _______________________________________________
> swift-corelibs-dev mailing list
> swift-corelibs-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-corelibs-dev



More information about the swift-corelibs-dev mailing list