Console trouble


#1

I’m redoing tis chapter and I find that as soon as I do the first steps in chapter 14 (page 284) the console stops printing.
Even the messages from previous chapters, like:

  • (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
    {
    NSLog(@“user dismissed popover”);
    imagePickerPopover = nil;
    }

doesn`t print any more!!

Any clues?

Thanks!!


#2

Could you paste the code you have just added which stops NSLog’s printing to the console?

Also add it in between tags please.

Nick
http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#3

Here it goes:

In BNRItem.h just the protocol

[code]#import <Foundation/Foundation.h>

@interface BNRItem : NSObject

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

@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSString *serialNumber;
@property (nonatomic) int valueInDollars;
@property (nonatomic, strong, readonly) NSDate *dateCreated;
@property (nonatomic, copy) NSString *imageKey;

@property (nonatomic, strong) BNRItem *containedItem;
@property (nonatomic, weak) BNRItem *container;

@end
[/code]

in BNRItem.m the encodeWithCoder and initWithCoder

[code]#import “BNRItem.h”

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

  • (id)randomItem
    {
    NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@“Fluffy”, @“Rusty”, @“Shiny”, nil];

    NSArray *randomNounList = [NSArray arrayWithObjects:@“Bear”, @“Sporf”, @“Mac”, nil];

    NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
    NSInteger nounIndex = rand() % [randomNounList count];

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

    int randonValue = 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:randonValue
    serialNumber:randomSerialNumber];
    return newItem;
    }

  • (id)initWithItemName:(NSString *)name valueInDollars:(int)value serialNumber:(NSString *)sNumber
    {
    self = [super init];

    if (self){
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    dateCreated = [[NSDate alloc]init];
    }
    return self;
    }

  • (id)initItemName:(NSString *)name serialNumber:(NSString *)sNumber
    {
    return [self initWithItemName:name
    valueInDollars:0
    serialNumber:sNumber];

}

  • (id)init
    {
    return [self initWithItemName:@“Item"
    valueInDollars:0
    serialNumber:@”"];
    }

  • (void)setContainedItem:(BNRItem *)i
    {
    containedItem = i;
    [i setContainer:self];
    }

  • (NSString *)description
    {
    NSString *descriptionString = [[NSString alloc]initWithFormat:@"%@ (%@): Worth $%d, recorded on %@",
    itemName, serialNumber, valueInDollars, dateCreated];
    return descriptionString;
    }

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

  • (void)encodeWithCoder:(NSCoder *)aCoder
    {
    [aCoder encodeObject:itemName forKey:@“itemName”];
    [aCoder encodeObject:serialNumber forKey:@“serialNumber”];
    [aCoder encodeObject:dateCreated forKey:@“dateCreated”];
    [aCoder encodeObject:imageKey forKey:@“imageKey”];

    [aCoder encodeInt:valueInDollars forKey:@“valueInDollars”];
    }

  • (id)initWithCoder:(NSCoder *)aDecoder
    {
    self = [super init];
    if (self) {
    [self setItemName:[aDecoder decodeObjectForKey:@“itemName”]];
    [self setSerialNumber:[aDecoder decodeObjectForKey:@“serialNumber”]];
    [self setImageKey:[aDecoder decodeObjectForKey:@“imageKey”]];

      [self setValueInDollars:[aDecoder decodeIntForKey:@"valueInDollars"]];
      
      dateCreated = [aDecoder decodeObjectForKey:@"dateCreated"];
    

    }
    return self;
    }
    @end[/code]

in BNRItemStore.h the itemArchivePath and the saveChanges :

[code]#import <Foundation/Foundation.h>

@class BNRItem;

@interface BNRItemStore : NSObject
{
NSMutableArray *allItems;
}

  • (BNRItemStore *)sharedStore;
  • (NSArray *)allItems;

  • (BNRItem *)creatItem;

  • (void)removeItem:(BNRItem *)p;

  • (void)moveItemAtIndex:(int)from toIndex:(int)to;

  • (NSString *)itemArchivePath;

  • (BOOL)saveChanges;
    @end
    [/code]

in BNRItemStore.m the implementation of those:

#import "BNRItemStore.h"
#import "BNRItem.h"
#import "BNRImageStore.h"

@implementation BNRItemStore

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

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

- (id)init
{
    self = [super init];
    if (self) {
        NSString *path = [self itemArchivePath];
        
        allItems = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        
        if (!allItems) {
            allItems = [[NSMutableArray alloc] init];
        }
    }
    return self;
}

- (NSArray *)allItems
{
    return allItems;
}

- (BNRItem *)creatItem
{
    BNRItem *p = [[BNRItem alloc] init];
    
    [allItems addObject:p];
    
    return p;
}

- (void)removeItem:(BNRItem *)p
{
    NSString *key = [p imageKey];
    [[BNRImageStore sharedStore] deleteImageForKey:key];
    
    [allItems removeObjectIdenticalTo:p];
}

- (void)moveItemAtIndex:(int)from toIndex:(int)to
{
    if (from == to) {
        return;
    }
    
    BNRItem *p = [allItems objectAtIndex:from];
    
    [allItems removeObjectAtIndex:from];
    
    [allItems insertObject:p atIndex:to];
}

- (NSString *)itemArchivePath
{
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentDirectory = [documentDirectories objectAtIndex:0];
    
    return [documentDirectory stringByAppendingPathComponent:@"items.archive"];
}

- (BOOL)saveChanges
{
    NSString *path = [self itemArchivePath];
    return [NSKeyedArchiver archiveRootObject:allItems toFile:path];
}
@end

and finally in HomepwnerAppDelegate the #import for BNRItemStore and the code in applicationDidEnterBackground:

#import "HomepwnerAppDelegate.h"
#import "ItemsViewController.h"
#import "BNRItemStore.h"

@implementation HomepwnerAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    ItemsViewController *itemsViewController = [[ItemsViewController alloc] init];
    
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:itemsViewController];
    
    [[self window] setRootViewController:navController];
    
    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
{
    BOOL success = [[BNRItemStore sharedStore] saveChanges];
    if (success) {
        NSLog(@"Saved all of the BNRItems");
    } else {
        NSLog(@"Could not save any of the BNRItems");
    }
}

- (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

…from then on nothing is printed on the log, and it was working just perfectly before that!!

Saludo!


#4

Fixed!!..it was just a configuration problem in XCode…:S


#5

I had a similar problem… for some reason my console wasn’t activated.

If someone reading this has the problem of nothing printing in the console… select View when in X-Code and go down to Debug Area, and to the right there you should see Activate Console, or you can just press Shift + Cmd + C


#6

I can not get the encodeWithCoder method called.
shouldnt the NSKeyedArchiver…do the calling?


#7

well, it looks like it was showing a path that didnt exist. There was no Documentation directory, but the path in the log showed one. After I created the directory it worked.