iOS Getting the length of a MutableArray

ghall

macrumors 68040
Original poster
Jun 27, 2006
3,781
1
Rhode Island
I am trying to figure out how to get the number of objects in an NSMutableArray for purposes of populating a UITableView. I was able to make an NSArray with some dummy data to test that everything in the tableview is hooked up correctly, and that works fine. So my issue is getting the length from the array. For the dummy data I'm able to do this easily.

Code:
return [self.testArray count];
However when I change my code to this:
Code:
return [self.foodCart count];
things start getting wonky and throws up an error.
No visible @interface for 'Total' declares the selector 'count'
'Total' is the name of the class I use to keep a running total of the floats in the array.

I'm sorry if this isn't very clear, I only have about 3 months of experience with doing this stuff in my spare time. If something doesn't make sense I can try to clarify.
 
Last edited:

grandM

macrumors 65816
Oct 14, 2013
1,100
57
hmmm

Are you sure you declared foodCart as an NsArray? Seems as declared as an instance of Class Total.

Could also be you removed the connection to the property.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,427
4,432
Pale blue comma
Post the file "Total.h". That is, post the entire interface for the Total class.

It would also help to post the interface for whatever class contains the testArray and foodCart properties, so we have some context. Otherwise we have to guess at what your isolated lines of code mean.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,565
3,975
In this code:

Code:
[self.foodCart count];
What type is self? What type is foodCart?

I have no idea what type self is, but I suspect the type of foodCart is a Total and you should share with us Total.h, as chown suggested.
 

ghall

macrumors 68040
Original poster
Jun 27, 2006
3,781
1
Rhode Island
Code:
#import <Foundation/Foundation.h>
#import "Item.h"

@interface Total : NSObject{
    NSMutableArray *_shoppingCart;
}

@property int listLength;

-(float)cartTotal;

-(void)addItem:(Item *)item;

-(NSString *)itemDescription;
-(NSString *)itemTotal;


@end
This is my entire Total.h file.

It's called in my ViewController.m as
Code:
self.foodCart = [[Total alloc] init];
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,565
3,975
Code:
#import <Foundation/Foundation.h>
#import "Item.h"

@interface Total : NSObject{
    NSMutableArray *_shoppingCart;
}

@property int listLength;

-(float)cartTotal;

-(void)addItem:(Item *)item;

-(NSString *)itemDescription;
-(NSString *)itemTotal;


@end
This is my entire Total.h file.

It's called in my ViewController.m as
Code:
self.foodCart = [[Total alloc] init];
Your problem then is that Total doesn't have a count method, exactly as the error message said. You need to add a count method to your header if you want a count method to be available as you had in your code causing the error. You'll also need to implement the count method for Total if you haven't already.
 

grandM

macrumors 65816
Oct 14, 2013
1,100
57
hmm

Code:
Total t1 = [[Total alloc] init];
int number = [t1.shoppingCart count];
try this

ps: also loose underscore in :
Code:
@interface Total : NSObject{
    NSMutableArray *_shoppingCart;
}
so:
Code:
@interface Total : NSObject{
    NSMutableArray *shoppingCart;
}
add:
Code:
-(NSMutableArray *)shoppingCart
{
    if (!_shoppingCart) _shoppingCart =  [ [NSMutableArray alloc]init];
    return _shoppingCart;
}
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
8,427
4,432
Pale blue comma
Your Total class has this property:
Code:
@property int listLength;
If that property is intended to return the length of the _shoppingCart array, then simply rename the property to count:
Code:
@property int count;
You'll also need to rename the method in your @implementation.

If that isn't the intent of listLength, then ignore this.


Some of what I and others have posted are guesses, based on the code you've posted. If you want suggestions other than guesses, please post more code, i.e. the implementation of Total, the interfaces and implementation of Item, etc.

You should also describe your intent in defining these classes. Sometimes intent is clear from code. Sometimes it's not.


I suspect you are confused on the distinction between has-a and is-a:
http://en.wikipedia.org/wiki/Has-a
http://en.wikipedia.org/wiki/Is-a

A Car class has four tires. A Motorcycle class has two tires. The relationship between car and tires or motorcycle and tires is has-a.

If there's a Vehicle class that is the superclass of Car and Motorcycle, then a Car is a Vehicle, and a Motorcycle is a vehicle. A Boat class might be an example of a Vehicle subclass that has no tires.

Your Total class has-a NSMutableArray as an instance var. This does not meant that a Total is-a NSMutableArray.

Your Total class is-a NSObject because NSObject is given as the superclass. This means it has all the methods and instance variables that NSObject has.
 

ghall

macrumors 68040
Original poster
Jun 27, 2006
3,781
1
Rhode Island
Thanks for all the help everyone. Chown33 especially helped clear up a lot of confusion that I was having. I've got it working now, I think it was just that disconnect I was having there.