-1

Very simple question. Is it possible to create a class which is a list by it self? I mean: I do

taskList *taskList1 = [[taskList alloc] init];

And than simply:

taskList1 addObject:[task1]

May seem stupid, but I'm totally new to O-C syntax

I'd need two methods:

-(instancetype) init;

which just initialize as an empty list

+(instancetype)taskList;

to allocate taskList instance

and last thing: In interface i use:

@interface taskList : NSObject

or

@interface taskList : NSMuttableArray

I got stuck on something specific, didn't I? I'm sorry that I bother you with my programming level. Alright, I gave up, just last question, because I have to finish it very soon. I changed my approach I added

@property NSMutableArray *list;

Why does this:

taskList *TL1 =[taskList initTaskList];
task *task1 = [[task alloc] init];
task *task2 = [[task alloc] init];
TL1.list addObject:[task1];

doesn't work, I have "Expected identifier" Error

Filip
  • 57
  • 2
  • 10
  • 2
    ugh… so… what? it is possible to create a class which represents a list. It's also possible (although not advisable) to add a collection to itself. It's also possible (although not advisable) to subclass Foundation's collections. What do you want to do? – The Paramagnetic Croissant Feb 13 '15 at 18:15
  • 1
    It is definitely possible to create a class that behaves like `NSMutableArray`. Both of your approaches are valid, depending on how much functionality you wish to change. – Sergey Kalinichenko Feb 13 '15 at 18:17
  • Can anyone explain how? It's one-2 lines, I'd really appreciate – Filip Feb 13 '15 at 18:18
  • 1
    Do not subclass `NSMutableArray`. Your `TaskList` class would use a mutable array. – rmaddy Feb 13 '15 at 18:27
  • Ok thanks, now I know what not to do. Can anyone tell what I should? I still don't really understand the O-C syntax, I'm used to Java – Filip Feb 13 '15 at 18:29
  • It's not documented, but the `[...]` notation is implemented by some calls to (uh) undocumented methods inside the array and dictionary classes. Presumably if you implemented those methods in your class and somehow faked out the compiler you'd be able to use the notation on your class. – Hot Licks Feb 13 '15 at 18:34
  • 4
    If you don't understand basic Objective-C syntax, I would politely suggest that you step back from this task and work with some tutorials on the language first. SO is not the place to learn a language. SO is more helpful when you understand the basics and you get stuck on something specific. – rmaddy Feb 13 '15 at 19:00
  • [Good resources for learning ObjC](http://stackoverflow.com/q/1374660) – jscs Feb 13 '15 at 19:06

1 Answers1

1

If you read the subclassing notes on NSArray / NSMutableArray you'll see that Apple recommend against subclassing them because they are a class cluster. (i.e. what you really get when you ask for one is an undocumented subclass, and the initialiser decides which undocumented subclass to return to you based on some undocumented qualifiers..

So just make an NSObject subclass which owns a (private) strong property of type NSMutableArray, and publish an api to access that array..

eg

       #import "modelList.h"
    //dont worry header is empty, its up to you to do that..  this is a subclass on NSObject



    @interface modelList()

            @property (strong, nonatomic) NSMutableArray *backingArray;

            @end

            @implementation modelList
            @synthesize backingArray = _backingArray;
            -(instancetype )init{
              if (self = [super init]) {
                [self setBackingArray:[[NSMutableArray alloc]init]];
              }
              return self;
            }


    //public API (the stuff that is prototyped in the header..)
            -(id)objectAtIndex:(NSUInteger )index{
              return [self.backingArray objectAtIndex:index];
            }
            -(BOOL )containsObject:(id)object{
              return [self.backingArray containsObject:object];
            }
            -(void )addObject:(id)object{

          //example application, qualifying object..
              if ([object conformsToProtocol:@protocol(NSCoding)]) {
                [self.backingArray addObject:object];
               }
             }
            -(NSUInteger )count{
              return [self.backingArray count];
             }
      //etc etc declare publicly the things you need to get the job done


            @end

so far this is just a face for a mutable array obviously, but it gives you a place for whatever other model logic you need. good luck

Jef
  • 4,728
  • 2
  • 25
  • 33