Empty Simulator View


#1

Hello

Although my code is compiled accordingly, and I don’t even have a warnings of a semantic problem from xcode, my simulator screen is still empty and the BNRItems aren’t being shown.

I have added the code of all the various files below. I’ve spent quite some time on figuring this out, but I don’t seem to find the problem.

Thank you.

[code]//
// HomepwnerAppDelegate.h
// Homepwner
//
// Created by Serge Zehnder on 13.11.13.
// Copyright © 2013 com.bignerdranch. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HomepwnerAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

  • (void)saveContext;
  • (NSURL *)applicationDocumentsDirectory;

@end
[/code]

[code]//
// HomepwnerAppDelegate.m
// Homepwner
//
// Created by Serge Zehnder on 13.11.13.
// Copyright © 2013 com.bignerdranch. All rights reserved.
//

#import “HomepwnerAppDelegate.h”
#import “ItemsViewController.h”

@implementation HomepwnerAppDelegate
@synthesize window;

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    // Create a ItemsViewController
    ItemsViewController *itemsViewController = [[ItemsViewController alloc] init];
    // Place ItemsViewController’s table view in the window hierarchy
    [window setRootViewController:itemsViewController];

    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
    {
    // Saves changes in the application’s managed object context before the application terminates.
    [self saveContext];
    }

  • (void)saveContext
    {
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
    if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    NSLog(@“Unresolved error %@, %@”, error, [error userInfo]);
    abort();
    }
    }
    }

#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn’t already exist, it is created and bound to the persistent store coordinator for the application.

  • (NSManagedObjectContext *)managedObjectContext
    {
    if (_managedObjectContext != nil) {
    return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
    }

// Returns the managed object model for the application.
// If the model doesn’t already exist, it is created from the application’s model.

  • (NSManagedObjectModel *)managedObjectModel
    {
    if (_managedObjectModel != nil) {
    return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@“Homepwner” withExtension:@“momd”];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL];
    return _managedObjectModel;
    }

// Returns the persistent store coordinator for the application.
// If the coordinator doesn’t already exist, it is created and the application’s store added to it.

  • (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    {
    if (_persistentStoreCoordinator != nil) {
    return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@“Homepwner.sqlite”];

    NSError error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    /

    Replace this implementation with code to handle the error appropriately.

       abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
       
       Typical reasons for an error here include:
       * The persistent store is not accessible;
       * The schema for the persistent store is incompatible with current managed object model.
       Check the error message to determine what the actual problem was.
       
       
       If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
       
       If you encounter schema incompatibility errors during development, you can reduce their frequency by:
       * Simply deleting the existing store:
       [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
       
       * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
       @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
       
       Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
       
       */
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      abort();
    

    }

    return _persistentStoreCoordinator;
    }

#pragma mark - Application’s Documents directory

// Returns the URL to the application’s Documents directory.

  • (NSURL *)applicationDocumentsDirectory
    {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    }

@end
[/code]

[code]//
// ItemsViewController.h
// Homepwner
//
// Created by Serge Zehnder on 13.11.13.
// Copyright © 2013 com.bignerdranch. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”
#import “BNRItemStore.h”

@interface ItemsViewController : UITableViewController

-(id)init;
-(id)initWithStyle:(UITableViewStyle)style;

@end
[/code]

[code]//
// ItemsViewController.m
// Homepwner
//
// Created by Serge Zehnder on 13.11.13.
// Copyright © 2013 com.bignerdranch. All rights reserved.
//

#import “ItemsViewController.h”

@implementation ItemsViewController

  • (id)init
    {
    // Call the superclass’s designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
    }
    return self;
    }

  • (id)initWithStyle:(UITableViewStyle)style
    {
    return [self init];
    }

  • (NSInteger)tableView:(UITableView *)tableView
    numberOfRowsInSection:(NSInteger)section
    {
    return [[[BNRItemStore sharedStore] allItems] count];
    }

  • (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // Create an instance of UITableViewCell, with default appearance
    UITableViewCell *cell =
    [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
    reuseIdentifier:@“UITableViewCell”];
    // Set the text on the cell with the description of the item
    // that is at the nth index of items, where n = row this cell
    // will appear in on the tableview
    BNRItem *p = [[[BNRItemStore sharedStore] allItems]
    objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[p description]];
    return cell;
    }

@end
[/code]

[code]//
// BNRItem.h
// RandomPossessions
//
// Created by Serge Zehnder on 14.03.13.
// Copyright © 2013 Serge Zehnder. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface BNRItem : NSObject
{
NSString *itemName;
}

  • (id)randomItem;
  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber;

@property (nonatomic, strong) BNRItem *containedItem;
@property (nonatomic, weak) BNRItem *container;
@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, readonly, strong) NSDate *dateCreated;
@end[/code]

[code]//
// BNRItem.m
// RandomPossessions
//
// Created by Serge Zehnder on 14.03.13.
// Copyright © 2013 Serge Zehnder. All rights reserved.
//

#import “BNRItem.h”

@implementation BNRItem
@synthesize itemName, containedItem, container, serialNumber, valueInDollars, dateCreated;

  • (id)randomItem
    {
    // Create an array of three adjectives
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”,
    @“Rusty”,
    @“Shiny”, nil];
    // Create an array of three nouns
    NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”,
    @“Spork”,
    @“Mac”, nil];
    // Get the index of a random adjective/noun from the lists
    // Note: The % operator, called the modulo operator, gives
    // you the remainder. So adjectiveIndex is a random number
    // from 0 to 2 inclusive.
    NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];
    // Note that NSInteger is not an object, but a type definition
    // for “unsigned long"
    NSString *randomName = [NSString stringWithFormat:@”%@ %@",
    [randomAdjectiveList objectAtIndex:adjectiveIndex],
    [randomNounList objectAtIndex:nounIndex]];
    int randomValue = rand() % 100;
    NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10,
    ‘A’ + rand() % 26,
    ‘0’ + rand() % 10];
    BNRItem *newItem = [[self alloc] initWithItemName:randomName
    valueInDollars:randomValue
    serialNumber:randomSerialNumber];
    return newItem;
    }
  • (void)setContainedItem:(BNRItem *)i
    {
    containedItem = i;
    [i setContainer:self];
    }

  • (id)initWithItemName:(NSString *)name
    valueInDollars:(int)value
    serialNumber:(NSString *)sNumber
    {
    // Call the superclass’s designated initializer
    self = [super init];
    // Give the instance variables initial values
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc] init];
    // Return the address of the newly initialized object
    return self;

}

  • (NSString *)description
    {
    NSString *descriptionString = [[NSString alloc] initWithFormat:@"\n%@ (%@):\n Worth $%d",
    itemName,
    serialNumber,
    valueInDollars
    ];

    return descriptionString;

}

  • (void)dealloc
    {
    NSLog(@“Destroyed: %@”, self);
    }

@end
[/code]

[code]//
// BNRItemStore.h
// Homepwner
//
// Created by Serge Zehnder on 13.11.13.
// Copyright © 2013 com.bignerdranch. All rights reserved.
//

#import <Foundation/Foundation.h>
#import “BNRItem.h”

@interface BNRItemStore : NSObject
{
NSMutableArray *allItems;
}
// Notice that this is a class method and prefixed with a + instead of a -

+(BNRItemStore *)sharedStore;
-(NSArray *)allItems;
-(BNRItem *)createItem;

@end
[/code]

[code]//
// BNRItemStore.m
// Homepwner
//
// Created by Serge Zehnder on 13.11.13.
// Copyright © 2013 com.bignerdranch. All rights reserved.
//

#import “BNRItemStore.h”
#import “BNRItem.h”

@implementation BNRItemStore

  • (id)allocWithZone:(NSZone *)zone
    {
    return [self sharedStore];
    }

  • (BNRItemStore *)sharedStore
    {
    static BNRItemStore *sharedStore = nil;
    if (!sharedStore)
    sharedStore = [[super allocWithZone:nil] init];

    return sharedStore;
    }

  • (NSArray *)allItems
    {
    return allItems;
    }

  • (BNRItem *)createItem
    {
    BNRItem *p = [BNRItem randomItem];

    [allItems addObject:p];

    return p;
    }

  • (id)init
    {
    self = [super init];
    if (self)
    {
    allItems = [[NSMutableArray alloc] init];
    }
    return self;
    }

@end
[/code]