2

When trying to create a category for NSInteger, the compiler complains that it "Cannot find interface declaration for 'NSInteger'". Is it not possible to create an NSInteger category?

A simple test will show the compiler error:

#import <Foundation/Foundation.h>

@interface NSInteger (NSInteger_Extensions)

-(NSInteger)simpleTest;

@end



#import "NSInteger_Extensions.h"

@implementation NSInteger (NSInteger_Extensions)

-(NSInteger)simpleTest {
    return self + 5;
}

@end

Should this be possible?

Thanks!

Hooligancat
  • 3,588
  • 1
  • 37
  • 55

2 Answers2

9

NSinteger is not an interface. So it's not possible.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html

From the link above:

Used to describe an integer.

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

PS: But you can make a category of NSNumber. I think it's the thing you want to do

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • Doh! Thanks. What a fool I am. – Hooligancat Mar 15 '11 at 21:59
  • Ah - - now that's a good point. I was literally just about to write a function that takes my int, does it's work and returns it but a NSNumber category is actually a little more useful for me. Thanks! – Hooligancat Mar 15 '11 at 22:04
  • 1
    You probably really do not want to add methods to `NSNumber`. If you really think you do, then make sure you prefix 'em with something unique; `-hooli_myMagicMethod`. – bbum Mar 16 '11 at 04:11
6

NSInteger is simply a typedef for int, a primitive type. Since it's not an object, you won't be able to add a category to it.

You could instead add this to NSNumber or NSValue.

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82