Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Subclassing issue with NSMutableArray and Java

Subclassing issue with NSMutableArray and Java
Thread Tools
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jan 22, 2002, 05:49 PM
 
Alright, for convenience, I made a subclass of NSMutableArray called NSMSArray (Next Step Mutable String Array). Now, all you other Coco-Java developers, try compiling this and making an instance of NSMSArray with the the static method arrayWithTokenizer.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
import java.util.StringTokenizer;


public class NSMSArray extends NSMutableArray
{

public static NSMSArray arrayWithTokenizer(StringTokenizer a)
{
String[] array = new String[a.countTokens()];
for(int i=<font color = blue>0</font>; i&lt;array.length; i++) array[i] = a.nextToken();
return new NSMSArray(array);
}

public NSMSArray(Object[] a)
{
super(a);
}

public String stringAtIndex(int i)
{
return (String)this.objectAtIndex(i);
}

public String combinedString()
{
int count = this.count();
String answer = <font color = red>""</font>;
for(int i=<font color = blue>0</font>; i&lt;count; i++) answer+=this.stringAtIndex(i);
return answer;
}

}

</font>[/code]

Notice this:
NSInvalidArgumentException: *** initialization method -initWithObjects:count: cannot be sent to an abstract object of class NSMSArray: Create a concrete instance!
After noticing this, does anyone know why this is happening or how I could fix the problem?

Thanks,
F-bacher
     
Junior Member
Join Date: Mar 2001
Status: Offline
Reply With Quote
Jan 22, 2002, 08:36 PM
 
You have to implement the primitive methods of these abstract classes. You need to implement:

addObject
insertObjectAtIndex
removeLastObject
removeObjectAtIndex
replaceObjectAtIndex

You also need to implement the primitive methods of NSArray:

count
objectAtIndex

Now you know why subclassing Cocoa abstract classes is a PITA. But since you aren't overriding any method, I can't help but wonder why you're subclassing NSMutableArray in the first place. Why not just make it an NSObject subclass?

-Peter
     
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Jan 22, 2002, 08:49 PM
 
Why are you using a factory method here?
Why not overload the constructor?

Try this:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
import java.util.StringTokenizer;


public class NSMSArray extends NSMutableArray
{

public NSMSArray(StringTokenizer a)
{
String[] array = new String[a.countTokens()];
for(int i=<font color = blue>0</font>; i&lt;array.length; i++) array[i] = a.nextToken();
this.NSMSArray(array);
}

public NSMSArray(Object[] a)
{
super(a);
}

public String stringAtIndex(int i)
{
return (String)this.objectAtIndex(i);
}

public String combinedString()
{
int count = this.count();
String answer = <font color = red>""</font>;
for(int i=<font color = blue>0</font>; i&lt;count; i++) answer+=this.stringAtIndex(i);
return answer;
}
}
</font>[/code]

then in your code, just instantiate the NSMSArray as an object like this,
where st is some previously defined string tokenizer:

NSMSArray nsmsa = new NSMSArray(st);

[ 01-22-2002: Message edited by: Kristoff ]
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Jan 22, 2002, 10:32 PM
 
Wixar's got the key: I needed to implement all the rest of the methods in the abstract class... which would have sucked. So I just made my class a regular subclass of Object and had it implement that methods I needed, like count(), replaceObjectAtIndex(), and objectAtIndex(), and had them pass the args onto a instance of NSMutableArray.

Yeah, I could have overriden the constructor, but that doesn't really fix my problem.

Thanks,
F-bacher
     
Forum Regular
Join Date: Aug 2001
Location: Vienna, Austria
Status: Offline
Reply With Quote
Jan 23, 2002, 05:43 AM
 
Also note that you should never begin a class name with NS*, because that's reserved for Apple.

Of course that doesn't fully apply to Cocoa/Java early adaptors, because only ObjC doesn't support namespaces, but it might be better to follow the convention.

And if you want to know why NSMutableArray is an abstract class, read the docs about class clusters.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:02 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2