-4

I want to create an Objective-C Class of type NSArray but i dont know how to initialize it or how to add data in.

I did the following:

1- Create a new File

enter image description here

2-I had chosen Objective-C Classenter image description here

3-I had chosen a Subclass of of type NSArray enter image description here

Two Files Appear:

.h:

#import <Foundation/Foundation.h>

@interface ArrayClass : NSArray

@end

.m

#import "ArrayClass.h"

@implementation ArrayClass

@end

My question is: What is the next steps.

1- what methods to call in this class.

2- how to initialize the array.

3- is -(id)init Enough for it

EDIT: i have a parser in my ViewController and i need to initialize another one with a different XML link, so thats why i need to have a new class with a NSArray type

Thanks in advance.

Community
  • 1
  • 1
Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • http://stackoverflow.com/questions/5752443/retrieving-class-object-from-nsarray-and-adding-to-self and https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html – iPatel Mar 20 '13 at 11:47
  • 4
    Are you sure you want an actual *subclass* of NSArray and not just an *instance*? – DrummerB Mar 20 '13 at 11:47
  • 1
    What exactly do you want to achieve ? You will add methods according to your needs. You initialize the array in the normal way you initialize objects. The only reason to implement -(id)init is if you want to override its behaviour(remember, -(id)init is inherited from the parent class). – Petar Mar 20 '13 at 11:48
  • 1
    what you want to do by creating a class of NSArray? – Dhara Mar 20 '13 at 11:49
  • 2
    Have you considered actually reading the documentation? (And also maybe you should study up on the distinction between "class" and "instance".) – Hot Licks Mar 20 '13 at 11:56
  • 1
    Why do you want make a subclass of nsarray? If you want to add some more features to nsarray you can do this by making a category or extension of nsarray. – Exploring Mar 20 '13 at 12:00
  • 1
    @DrummerB yes im sure, see the edit – Mutawe Mar 20 '13 at 12:09
  • 1
    @Mutawe your added explanation explains nothing. – vikingosegundo Mar 20 '13 at 12:18
  • 1
    In 4 years of professional objective-c development I subclassed NSArray exactly once — and only as a [sample code](http://stackoverflow.com/a/8198852/106435). So if you think, you need to subclass it and there is no other way, you are on the wrong track. – vikingosegundo Mar 20 '13 at 12:27
  • Please learn more about programming before you attempt working with Objective-C again. You need to understand the fundamentals. – Hot Licks Mar 20 '13 at 15:59
  • The OP himself voted to close. And since then no edit was performed. So whoever is voting to reopen: please explain your motivation. – vikingosegundo Mar 21 '13 at 13:24

4 Answers4

2

I'm going to just post this so that it's stated by someone.

NSArray is a class cluster, a wrapper around an internal implementation. The internal details of it change depending on the content and size of the array so that it's optimized for performance. As a result of this it is not a good idea to subclass NSArray (or NSDictionary).

I suppose you could extend the class by adding a category on it, this is a better and more 'Cocoa' way to add functionality. There are some guides in Apple developer docs on design patterns which give assistance on this.

Maybe restate the question so we are all clear as we are confused as to whether you do actually want to subclass it or just create one.

Cocoadelica
  • 3,006
  • 27
  • 30
0

If you're simply trying to use an NSArray just do this:

NSArray  *myArray = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];

No need to do what you're doing unless you want to change the actual structure and extend NSArray.

Liam George Betsworth
  • 18,373
  • 5
  • 39
  • 42
  • i have a parser in my ViewController and i need to initialize another one with a different XML link, so thats why i need to have a new class with a NSArray type – Mutawe Mar 20 '13 at 12:08
  • Hey Mutawe. I appreciate the clarification. Sadly it doesn't explain quite clearly enough of us to help you :) You say you have a parser in your view controller ( I presume to parse XML ) and you need to create another one to parse some different XML. Could you post the line of code that creates the original parser perhaps? – Cocoadelica Mar 24 '13 at 16:31
0

check out this answer if you are really trying to extend NSArray

if you are not intending to extend the functionality of an NSArray, you are probably looking to create and instance of NSArray, which is as simple as going NSArray *arrayName = [[NSArray alloc] init] within some area where you need an array,

Community
  • 1
  • 1
Fonix
  • 11,447
  • 3
  • 45
  • 74
0

Or if you want an array you can add data to dynamically you are going to want

NSMutableArray *arr = [[NSMutableArray alloc] init];

It all depends what you want.

Kenny Bambridge
  • 407
  • 4
  • 14