Bounds Checking on NSArray

Hi all,

In swift-corelibs-foundation/Foundation/NSArray.swift on line 347 there is a comment that says TODO: Bounds checking . Since that just call objectAtIndex, I think the bounds checking should be done inside of objectAtIndex. My question, though, is what should be returned if the index is out of the array bounds? The methods don’t throw, so I don’t think we can raise an exception. And I don’t think we can return nil because the method is supposed to return a non-optional AnyObject. What about using assert or precondition?

I’m super excited to be a part of this community!

Thanks,
Daniel Strokis

Hi there Daniel,

Hi all,

In swift-corelibs-foundation/Foundation/NSArray.swift on line 347 there is a comment that says TODO: Bounds checking . Since that just call objectAtIndex, I think the bounds checking should be done inside of objectAtIndex.

Note we have another pull request open in this area too: [Need Feedback]: Added bounds checking to NSArray subscript by KaiCode2 · Pull Request #15 · apple/swift-corelibs-foundation · GitHub

I asked there to rebase into a single commit; once that’s done, maybe we can accept that and then move it to the implementation of objectAtIndex.

My question, though, is what should be returned if the index is out of the array bounds? The methods don’t throw, so I don’t think we can raise an exception. And I don’t think we can return nil because the method is supposed to return a non-optional AnyObject. What about using assert or precondition?

We’ve made a decision to use fatalError(“description that makes sense”) in cases of programmer error. Obj-C Foundation would have thrown an exception (since we use ObjC exceptions exclusively for indication of programmer error there).

I’m super excited to be a part of this community!

Glad to have you here.

- Tony

···

On Dec 3, 2015, at 7:14 PM, Daniel Strokis <dstrokis@icloud.com> wrote:

Thanks,
Daniel Strokis

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev