2016-04-17 1 views
0

これは、汎用のLinkedListを作成するために使用する2つのクラス。一般的なデータコンテナクラスから作成されたオブジェクトに、Objective-Cの特定の型(NSStringなど)のデータしか含めることを指定する方法:

SListクラスの特定のインスタンスを作成するとき、そのクラスにリンクされるすべてのLinkedListObjectsを特定の型にするように指定する方法を教えてください。例:NSStringの場合。

私はObjective-Cで見た "タイプヒント"機能を模倣しようとしています。たとえば:NSArray < NSString *> * someArray;

@interface SList : NSObject 
@property LinkedListNode *head ; 
@property NSUInteger size ; 

-(void) insertFront:(id)newItem ; 
@end 


@interface LinkedListNode : NSObject 
@property LinkedListNode *nextNode ; 
@property id nodeData ; 

-(void) insertAfter:(id)data ; 

-(instancetype) initWithNextNode:(LinkedListNode *)nextNode andData:(id)nodeData ; 
-(instancetype) initWithData:(id)nodeData ; 
@end 

答えて

1

NSArray.hヘッダーに表示される内容をエミュレートします。 ObjectTypeを正しく使用する必要があります。このような

何か:

@interface SList<ObjectType> : NSObject 
@property LinkedListNode<ObjectType> *head ; 
@property NSUInteger size ; 

-(void) insertFront:(ObjectType)newItem ; 
@end 


@interface LinkedListNode<ObjectType> : NSObject 
@property LinkedListNode *nextNode ; 
@property ObjectType nodeData ; 

-(void) insertAfter:(ObjectType)data ; 

-(instancetype) initWithNextNode:(LinkedListNode *)nextNode andData:(ObjectType)nodeData ; 
-(instancetype) initWithData:(ObjectType)nodeData ; 
@end 

次に、あなたはそれをこのような何か作成します。答えを@rmaddy

SList<NSString *> *list = [[SList alloc] init]; 
[list insertFrom:@"some string"]; 
+0

おかげで(魚)や魚にする方法教えて! –

+0

私が見ることの1つは、実装でObjectTypeを使用できないことです。実装ではidを使用する必要があります。これをあなたの答えに加えたいかもしれません。 –