You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On NSArray and NSMutableArray, add a custom subscript that duplicates the [safe:] subscript that OTCore already implements on standard Swift collections.
The implementation checks that the passed index is safe (it is not out-of-bounds).
If the index is valid, then the element at that index is returned.
If the index is not valid, then nil is returned.
Problem
I could not find a way to make the compiler happy when both subscripts use the same label "safe:". The nature of Objective-C objects is making this elusive to figure out.
The following implementation does function as expected (with unique subscript labels) but the goal is to use the same labels.
As soon as you make the subscript labels the same, it will not compile.
extensionNSArray{open subscript(safe index:Int)->Any?{get}}extensionNSMutableArray{// Compiler Error: "Overriding non-@objc declarations from extensions is not supported"open subscript(safe index:Int)->Any?{getset}}
However, making both subscripts @objc and having the NSMutableArray declared override will allow the code to compile, but the functions are never actually executed when the subscript is called. Instead, the underlying subscript executes (which of course produces EXC_BAD_INSTRUCTION when an index is out-of-bounds since that is default behavior of NSArray and NSMutableArray.
I believe the goal of implementing this with identical subscript labels [safe:] for both NSArray and NSMutableArray is possible, but it will require more research and assistance.
The text was updated successfully, but these errors were encountered:
Currently, these are implemented in the library as:
NSArray:
letarray=NSArray()letgetVal=nsArray[safe:1]
NSMutableArray:
letarray=NSMutableArray()// both getters work ([safe:] is inherited from NSArray):letgetVal=array[safe:1]letgetVal=array[safeMutable:1]// setter:array[safeMutable:1]= newValue
But the goal is to unify them into a single [safe:] subscript for both NSArray (get) and NSMutableArray (get and set).
Objective
On
NSArray
andNSMutableArray
, add a custom subscript that duplicates the[safe:]
subscript that OTCore already implements on standard Swift collections.Interface
Implementation
index
is safe (it is not out-of-bounds).nil
is returned.Problem
I could not find a way to make the compiler happy when both subscripts use the same label "
safe:
". The nature of Objective-C objects is making this elusive to figure out.The following implementation does function as expected (with unique subscript labels) but the goal is to use the same labels.
As soon as you make the subscript labels the same, it will not compile.
However, making both subscripts
@objc
and having theNSMutableArray
declaredoverride
will allow the code to compile, but the functions are never actually executed when the subscript is called. Instead, the underlying subscript executes (which of course producesEXC_BAD_INSTRUCTION
when anindex
is out-of-bounds since that is default behavior ofNSArray
andNSMutableArray
.Next Steps
I believe the goal of implementing this with identical subscript labels
[safe:]
for bothNSArray
andNSMutableArray
is possible, but it will require more research and assistance.The text was updated successfully, but these errors were encountered: