(NSString *) description over ride not being called?


#1

I’m having trouble getting a description string to display as textLabel in my program, I set a breakpoint at (NSString *)description…

here’s my code:
ZCSItem.m

[code]#import “ZCSItem.h”

@implementation ZCSItem

  • (instancetype) lastItem
    {
    ZCSItem * lastItem = [[self alloc] initWithItemName:@“Last Item!“
    valueInDollars:0
    serialNo:@””];

    return lastItem;
    }

  • (instancetype) randomItem
    {

    //adjectives to be used in the creation of random items
    NSArray * randomAdjectives = @[@“Shiney”,
    @“Horrific”,
    @“Bad”,
    @“Gnarly”];
    // nouns to be used in the creation of random items
    NSArray * randomNouns = @[@“Thing”,
    @“Toaster”,
    @“Spoon”,
    @“Yeti”];
    NSString * serialString = [NSString stringWithFormat:@"%d%d%d%d%d",
    ‘A’ + arc4random() % 10,
    ‘0’ + arc4random() % 25,
    ‘A’ + arc4random() % 10,
    ‘0’ + arc4random() % 25,
    ‘A’ + arc4random() % 10
    ];
    NSInteger nounIndex = arc4random() % [randomAdjectives count];
    NSInteger adjectiveIndex = arc4random() % [randomNouns count];

    int randomValue = arc4random() % 100;

    NSString * randomName = [NSString stringWithFormat:@"%@ %@", [randomNouns objectAtIndex:nounIndex], [randomAdjectives objectAtIndex:adjectiveIndex]];

    ZCSItem * createdItem = [[self alloc] initWithItemName:randomName
    valueInDollars:randomValue
    serialNo:serialString];

    return createdItem;

}

  • (instancetype) initWithItemName:(NSString *)name valueInDollars:(int)value serialNo:(NSString *)serialNo
    {
    self = [super init];

    if (self) {
    _itemName = name;
    _value = value;
    _serialNo = serialNo;

      _dateCreated = [[NSDate alloc] init];
    

    }

    return self;
    }

  • (NSString *)description
    {
    NSString *descriptionString =
    [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
    self.itemName,
    self.serialNo,
    self.value,
    self.dateCreated];
    return descriptionString;

}

  • (instancetype) initWithItemName:(NSString *)name
    {
    return [self initWithItemName:name valueInDollars:0 serialNo:@""];
    }

  • (instancetype) init
    {
    return [self initWithItemName:@“Item”];
    }

@end
[/code]

ZCSItemStore.m

[code]#import “ZCSItemStore.h”

#import “ZCSItem.h”

@implementation ZCSItemStore

  • (instancetype) sharedStore {
    static ZCSItemStore *sharedStore;

    if (!sharedStore) {
    sharedStore = [[self alloc] initPrivate];
    }

    return sharedStore;
    }

  • (instancetype) init {
    [NSException raise:@“Singleton” format:@“Use + [ZCSItemStore sharedStore]”];
    return nil;
    }

  • (instancetype) initPrivate {

    if (self) {
    _itemsOver50 = [[NSMutableArray alloc] init];
    _itemsUnder50 = [[NSMutableArray alloc] init];
    }
    return self;
    }

  • (ZCSItem *) createLastItem
    {
    ZCSItem * lastItem = [ZCSItem lastItem];

    return lastItem;
    }

  • (ZCSItem *) createItem
    {
    ZCSItem * newItem = [ZCSItem randomItem];

    if (newItem.value > 50) {
    [self.itemsOver50 addObject:newItem];
    } else {
    [self.itemsUnder50 addObject:newItem];
    }

    return newItem;
    }

@end
[/code]

ZCSItemsViewController.m

[code]
#import “ZCSItemsViewController.h”
#import “ZCSItem.h”
#import “ZCSItemStore.h”

@implementation ZCSItemsViewController

  • (instancetype) init
    {
    self = [super initWithStyle:UITableViewStylePlain];

    if (self) {
    for (int i = 0; i < 5; i++) {
    [[ZCSItemStore sharedStore] createItem];
    }
    }
    return self;
    }

  • (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"
    forIndexPath:indexPath];

    // get the array of items and which item is being pointed at
    // and set the description of the uitableviewcell
    NSArray * items = [[ZCSItemStore sharedStore] allItems];
    ZCSItem * item = items[indexPath.row];

    cell.textLabel.text = [item description];
    cell.textLabel.textColor = [UIColor blackColor];

    return cell;

}

  • (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    ZCSItemStore *itemStore = [ZCSItemStore sharedStore];

    if (section == 0) {
    return [[itemStore itemsUnder50] count];
    } else {
    return [[itemStore itemsOver50] count];
    }
    }

  • (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
    {
    return 2;
    }

  • (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    if (section == 0) {
    return @“Under 50”;
    } else {
    return @“Over 50”;
    }
    }

  • (void)viewDidLoad
    {
    [super viewDidLoad];

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@“UITableViewCell”];
}

  • (instancetype)initWithStyle:(UITableViewStyle)style
    {

    return [self init];
    }

@end[/code]

AppDelegate.m

#import "AppDelegate.h"
#import "ZCSItemsViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    ZCSItemsViewController * ivc = [[ZCSItemsViewController alloc] init];
    
    self.window.rootViewController = ivc;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

any help would be appreciated, I have no idea why it isn’t being called…


#2

Two things:

  1. In ZCSItemStore.m, you’re not initializing the object via self = [super init]

  2. In ZCSItemsViewController.m, you’re still trying to pull the item to show from allItems…but allItems isn’t being populated – it’s now just itemsUnder50 and itemsOver50. So the view controller needs to check the indexPath to see WHICH SECTION -tableView:cellForRowAtIndexPath: wants to populate, and use the appropriate array. (indexPath.row and indexPath.section are the non-obvious fields to check.)