In my Objective-C class's .h file, I have created a Category for NSIndexPath like this:
@interface NSIndexPath (YVTableView)
@property (nonatomic, assign) NSInteger subRow;
@end
and in that class's .m file, I have implemented that like:
static void *SubRowObjectKey;
@implementation NSIndexPath (YVTableView)
@dynamic subRow;
- (NSInteger)subRow
{
id subRowObj = objc_getAssociatedObject(self, SubRowObjectKey);
return [subRowObj integerValue];
}
- (void)setSubRow:(NSInteger)subRow
{
id subRowObj = [NSNumber numberWithInteger:subRow];
objc_setAssociatedObject(self, SubRowObjectKey, subRowObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
Now, when I am accessing the subRow property of NSIndexPath in Swift 3 using IndexPath then its giving me error:
Value of type 'IndexPath' has no member 'subRow'
If I am trying to access it using type casting
let subIndexPath = indexPath as NSIndexPath
let subRowIndex = subIndexPath.subRow
its always returning me '0' as 'subRow' value. May be its because IndexPath is value type and NSIndexPath is reference type.
I have implemented UITableViewDelegate to my Custom Delegate using Objective-C and implemented it to Swift class but in Swift where I am implementing the Custom Delegate methods, I am facing this issue.
I also tried to use NSIndexPath instead of IndexPath in my Custom Delegate implementation (Swift code) but it was giving me the error "Type YVViewController does not conform to protocol, Candidate has non matching-type".
Here is my Custom Delegate declaration :
@protocol YVTableViewDelegate <UITableViewDelegate>
@required
- (UITableViewCell *)tableView:(SKSTableView *)tableView cellForSubRowAtIndexPath:(NSIndexPath *)indexPath;
@end
Its working perfectly fine with Swift 2.x but after migrating to Swift 3, I am facing this issue.
The
subRowassociated with anNSIndexPathwill not be associated with anIndexPathcasted from thatNSIndexPathbecause they are not the same "object".IndexPathis a value type in Swift, defined with thestructkeyword, so it cannot have an Objective-C associated object.So even if you have set a value to
subRowof anNSIndexPathin Objective-C code, the value is lost when thatNSIndexPathis casted to Swift'sIndexPath. That's why when you again cast theIndexPathback toNSIndexPath, thesubRowvalue is always 0, which is the default value.In your case, you need to declare the protocol in Swift and specify the index path parameter type as
NSIndexPath.