Creazione di categoria NSInteger
-
24-10-2019 - |
Domanda
Quando si cerca di creare una categoria per NSInteger, il compilatore si lamenta che "Non riesci a trovare dichiarazione di interfaccia per 'NSInteger'". Non è possibile creare una categoria di NSInteger?
Un semplice test mostrerà l'errore del compilatore:
#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
questo dovrebbe essere possibile?
Grazie!
Soluzione
NSInteger non è un'interfaccia. Quindi non è possibile.
Dal link qui sopra:
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:
Ma si può fare una categoria di NSNumber
. Penso che sia la cosa che si vuole fare
Altri suggerimenti
NSInteger
è semplicemente un typedef per int
, un tipo primitivo. Dal momento che non è un oggetto, non sarà in grado di aggiungere una categoria ad esso.
Si potrebbe invece aggiungere questo alla NSNumber
o NSValue
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow