2

I'm adding a category to NSData as follows:

// PacketCategories.h
@interface NSData(PacketSplit)
  - (NSArray *)splitTransferredPackets:(NSData **)leftover;
@end

// PacketCategories.m
@implementation NSData(PacketSplit)
- (NSArray *)splitTransferredPackets:(NSData **)leftover {

    NSMutableArray *ret = [NSMutableArray array];
    const unsigned char *beginning = [self bytes];
    const unsigned char *offset = [self bytes];
    NSInteger bytesEnd = (NSInteger)offset + [self length];

    while ((NSInteger)offset < bytesEnd) {
        uint64_t dataSize[1];
        NSInteger dataSizeStart = offset - beginning;
        NSInteger dataStart = dataSizeStart + sizeof(uint64_t);

        NSRange headerRange = NSMakeRange(dataSizeStart, sizeof(uint64_t));
        [self getBytes:dataSize range:headerRange];


        if (dataStart + dataSize[0] + (NSInteger)offset > bytesEnd) {
            NSInteger lengthOfRemainingData = [self length] - dataSizeStart;
            NSRange dataRange = NSMakeRange(dataSizeStart, lengthOfRemainingData);
            *leftover = [self subdataWithRange:dataRange]; 

            return ret;
        }

        NSRange dataRange = NSMakeRange(dataStart, dataSize[0]);
        NSData *parsedData = [self subdataWithRange:dataRange];

        [ret addObject:parsedData];
        offset = offset + dataSize[0] + sizeof(uint64_t);
    }
    return ret;
}
@end

And then trying to call that category:

#import "PacketCategories.h"

NSMutableData *data = [NSMutableData data];
// Read some data
[data appendBytes:buffer length:bytesRead];
NSArray *dataPackets = [data splitTransferredPackets:&readLeftover];

Which gets the following error:

-[NSConcreteMutableData splitTransferredPackets:]: unrecognized selector sent to instance 0x6e6f7b0 [ERROR] The application has crashed with an unhandled exception. Stack trace:

Any ideas? Does NSConcreteMutableData not inherit from NSData?

Other suggested answers (Objective-C Category Causing unrecognized selector) have suggested that the file isn't linked in, which is not possible because other categories defined in this file are used just fine.

Thanks

Community
  • 1
  • 1
Scott Montgomerie
  • 1,780
  • 1
  • 16
  • 17

2 Answers2

6

Ookay. I had the same problem, but with a different outcome. Briefly, the problem was in my project file. The category files showed up in the project navigator - I can load/edit, etc. But the linker did not know to link with them. The way I found this was pulling on another thread - creating a dummy concrete class in my category files to force the linker to include them. No luck. Then I tried to instantiate an instance of the dummy class in my app. Eureka - I now get a linker error! So, I simply removed and re-added the category files to the project and now all is well. Not sure how the project file got out of whack ( svn merge? ), but there it is.

svenyonson
  • 183
  • 2
  • 8
  • This pointed me in the right direction to find the solution to the problem I was facing... I had forgot to add the category to my tests target and thus my tests were failing. – Adil Hussain Dec 05 '16 at 17:39
5

Delete the Category files and add them again checking the target. Solved for me.

idiogo
  • 193
  • 3
  • 5