Resolved Getting the length of a MutableArray

Discussion in 'iOS Programming' started by ghall, Feb 27, 2014.

  1. ghall, Feb 27, 2014
    Last edited: Mar 2, 2014

    ghall macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #1
    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.
    '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.
     
  2. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #2
    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.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    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.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    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.
     
  5. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #5
    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];
    
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    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.
     
  7. grandM, Feb 28, 2014
    Last edited by a moderator: Feb 28, 2014

    grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #7
    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;
    }
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    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.
     
  9. ghall thread starter macrumors 68040

    ghall

    Joined:
    Jun 27, 2006
    Location:
    Rhode Island
    #9
    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.
     

Share This Page