0

As you can tell by my question I am new to Objective C. I can access static properties such as NSString but it seems like I am unable to access arrays or even other properties, must be doing something wrong. Basically, I have a Model file with my main properties.

Here is my model file called ListingManager.h

#import <Foundation/Foundation.h>

@interface ListingManager : NSObject
@property (nonatomic, strong) NSString *listingTitle;
@property (nonatomic, strong) NSString *listingDescription;
@property (nonatomic, strong) NSString *listingPrice;
@property (nonatomic, readonly) NSString *datePosted;
@property (nonatomic, retain) NSMutableArray *lists;
@property (nonatomic) BOOL *viewed;
@end

ListingManager.m

#import "ListingManager.h"
@implementation ListingManager

@synthesize lists;
@synthesize listingPrice;
@synthesize listingDescription;
@synthesize listingTitle;
@synthesize datePosted;

@end

Here is another view controller where I am using it ListingTableViewController.m

#import "ListingTableViewController.h"


@interface ListingTableViewController ()
@property (nonatomic, strong) ListingManager *manageListings;
@end

@implementation ListingTableViewController

@synthesize manageListings;


- (void) loadInitialData {

    ListingManager *listing1 = [[ListingManager alloc] init];
    listing1.listingTitle = @"Title here";
    listing1.listingDescription = @"another description..Whatever it is";
    listing1.listingPrice = @"100";
    [self.manageListings.lists addObject:listing1];
    NSLog(@"%@", listing1); //for debugging purposes

    ListingManager *listing2 = [[ListingManager alloc] init];
    listing2.listingTitle = @"Title here";
    listing2.listingDescription = @"a long description here";
    listing2.listingPrice = @"50";
    [self.manageListings.lists addObject:listing2];

    ListingManager *listing3 = [[ListingManager alloc] init];
    listing3.listingTitle = @"Cool stuff";
    listing3.listingDescription = @"Another descitpion righ there";
    listing3.listingPrice = @"90";
    [self.manageListings.lists addObject:listing3];

}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.listings= [[NSMutableArray alloc] init];
    self.manageListings = [[ListingManager alloc] init];

    [self.backdropTableView reloadData]; //reload table view

    [self loadInitialData]; //load inital static data


}
/*
- (void)viewWillAppear:(BOOL)animated
{
    [self.backdropTableView reloadData];
}
*/
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.manageListings.lists count]; //this here is not returning anything
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *custom = @"WXCustomCell";

    WXTableCell *cell = (WXTableCell *)[tableView dequeueReusableCellWithIdentifier:custom];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WXCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
    cell.titleLabel.text = listingManager.listingTitle;
    cell.descriptionlabel.text = listingManager.listingDescription;
    cell.thumbnail.image = [UIImage imageNamed:@"nothumb.gif"];
    cell.price.text = @"$50";
    cell.dateLabel.text = @"N/A";
    NSLog(@"%@", cell.titleLabel.text);

    //use for later
    /*
    NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
    [DateFormatter setDateFormat:@"MM/dd/yyy HH:mm:ss"];
    NSLog(@"%@",[DateFormatter stringFromDate:[NSDate date]]);
    */
    return cell;
}

//Call segue
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self performSegueWithIdentifier:@"showDetail" sender:nil];


}

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showDetail"]) {
    // Get the new view controller using [segue destinationViewController].
    NSIndexPath *indexPath = [self.backdropTableView indexPathForSelectedRow];
    DetailsViewController *vc = segue.destinationViewController;
    // Pass the selected object to the new view controller.
        ListingManager *listingManager = [self.manageListings.lists objectAtIndex:indexPath.row];
        vc.titleName = listingManager.listingTitle;
        vc.descriptionDetail = listingManager.listingDescription;


    }
}


@end

For example when I return '[self.manageListings.lists count]', it doesn't return anything, if I hardcode it to 3 though, I get the 3 initial items, with the title, description, and title being null. There must be something I am doing wrong connecting my model and VC.

suntzu
  • 65
  • 3
  • 13

3 Answers3

1

In Objective-C, you have manually alloc memory to objects. When you declare a iVar (property) like this @property (nonatomic, retain) NSMutableArray *lists;, all the runtime and compiler does is to create a pointer memory for that property and set the memory value to 0x00. Hence the array memory is not yet allocated. You should instantiate memory like this

self.manageListings = [[ListingManager alloc] init];
self.manageListings.lists = [[NSMutableArray alloc] init];
[self.manageListings.lists addObject:listing1];

Sending a message to a nil object just returns and hence you don't observe any crash.

Dunes Buggy
  • 1,779
  • 1
  • 21
  • 41
0

May be it is of because you reload the table before init data in viewDidLoad

[self.backdropTableView reloadData]; //reload table view

[self loadInitialData]; //load inital static data

insteed of this try this

[self loadInitialData]; //load inital static data

[self.backdropTableView reloadData]; //reload table view
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
guru
  • 2,727
  • 3
  • 27
  • 39
0

You sholud allocate memory, as pointed by iRavi. But it's better practice to allocate inside object init method. Override it in this manner:

- (id) init {

   if (self = [super init]) // not ==
   {
      _lists = [NSMutableArray new];
   }
   return self;
}

_lists - an autogenerated iVar, if you omit @synthesize

m8labs
  • 3,671
  • 2
  • 30
  • 32