Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:25:28 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Chapter 19 Exercise 4 Question  (Read 387 times)
J2000lbs
Newbie
*
Posts: 6


Email
« on: February 26, 2018, 01:46:30 PM »

I'm still new at programming, and very new to Objective-C.  I'm trying to archive the address book and then unarchive it.  It's not working correctly and I need to get this to work correctly before writing the last part of the exercise, the command line entry.

My problem is that when I print information from the unarchived data, there are many null values.  I've checked and double checked the code and it seems to follow what is in the book and on line.  I'm stuck and I want to learn how to fix this...

Here is what I have - I'm only including the parts of the code that pertain to the achieving operation.

AddressCard.h

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface AddressCard : NSObject<NSMutableCopying, NSCoding>

@property (weak, nonatomic) NSString *firstName, *lastName, *email,
*streetAddress, *city, *state, *zip, *country, *phoneNumber;

-(instancetype) initWithFirstName: (NSString *) theFirstName
 andLastName: (NSString *) theLastName
andEmail: (NSString *) theEmail
 address: (NSString *) theAddress
city: (NSString *) theCity
state: (NSString *) theState
 zipCode: (NSString *) theZipCode
 country: (NSString *) theCountry
phone: (NSString *) thePhoneNumber;

-(BOOL) isEqual:(AddressCard *) theCard;

-(BOOL) isMatch: (NSString *) theName;

-(void) print;
-(void) listPrint;

@end

AddressCard.m

Code: (Objective-C)
#import "AddressCard.h"

@implementation AddressCard

@synthesize firstName, lastName, email, streetAddress, city, state, zip,
country, phoneNumber;

//----------------------------------------------------------------------------------

-(instancetype) initWithFirstName: (NSString *) theFirstName
 andLastName: (NSString *) theLastName
andEmail: (NSString *) theEmail
 address: (NSString *) theAddress
city: (NSString *) theCity
state: (NSString *) theState
 zipCode: (NSString *) theZipCode
 country: (NSString *) theCountry
phone: (NSString *) thePhoneNumber
{
if (self = [super init]) {
if (firstName != theFirstName && lastName != theLastName && email != theEmail) {
firstName = [NSString stringWithString:theFirstName];
lastName = [NSString stringWithString:theLastName];
email = [NSString stringWithString:theEmail];
streetAddress = [NSString stringWithString:theAddress];
city = [NSString stringWithString:theCity];
state = [NSString stringWithString:theState];
zip = [NSString stringWithString:theZipCode];
country = [NSString stringWithString:theCountry];
phoneNumber = [NSString stringWithString:thePhoneNumber];
}
}
return self;
}

//----------------------------------------------------------------------------------

-(instancetype) init
{
return [self initWithFirstName:@"NoName"
  andLastName:@"NoName"
 andEmail:@"NoEmail"
  address:@"NoAddress"
 city:@"NoCity"
state:@"NoState"
  zipCode:@"NoZipCode"
  country:@"NoCountry"
 phone:@"NoPhoneNumber"
];
}

// **** NSCoding protocol methods *****  =======================================================

-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:firstName forKey:@"AddressCardFirstName"];
[aCoder encodeObject:lastName forKey:@"AddressBookLastName"];
[aCoder encodeObject:email forKey:@"AddressBookEmail"];
[aCoder encodeObject:streetAddress forKey:@"AddressBookStreetAddress"];
[aCoder encodeObject:city forKey:@"AddressBookCity"];
[aCoder encodeObject:state forKey:@"AddressBookState"];
[aCoder encodeObject:zip forKey:@"AddressBookZip"];
[aCoder encodeObject:country forKey:@"AddressBookCountry"];
[aCoder encodeObject:phoneNumber forKey:@"AddressBookPhoneNumber"];
}

//----------------------------------------------------------------------------------

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self=[super init]) {
firstName = [aDecoder decodeObjectForKey:@"AddressCardFirstName"];
lastName = [aDecoder decodeObjectForKey:@"AddressBookLastName"];
email = [aDecoder decodeObjectForKey:@"AddressBookEmail"];
streetAddress = [aDecoder decodeObjectForKey:@"AddressBookStreetAddress"];
city = [aDecoder decodeObjectForKey:@"AddressBookCity"];
state = [aDecoder decodeObjectForKey:@"AddressBookState"];
zip = [aDecoder decodeObjectForKey:@"AddressBookZip"];
country = [aDecoder decodeObjectForKey:@"AddressBookCountry"];
phoneNumber = [aDecoder decodeObjectForKey:@"AddressBookPhoneNumber"];
}

return self;
}

//----------------------------------------------------------------------------------



@end


AddressBook.h

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "AddressCard.h"

@interface AddressBook : NSObject <NSMutableCopying, NSCoding>

@property (nonatomic, strong) NSString *bookName; // changed to strong from copy
@property (nonatomic, strong) NSMutableArray *book;

-(instancetype) initWithName: (NSString *) name;

-(void) addCard: (AddressCard *) theCard;
-(void) removeCard: (AddressCard *) theCard;
-(BOOL) removeName: (NSString *) theName;
-(void) sort;


-(NSMutableArray *) lookup: (NSString *) theName;

-(NSUInteger) entries;
-(void) list;


@end

AddressBook.m

Code: (Objective-C)
#import "AddressBook.h"

@implementation AddressBook

@synthesize bookName, book;

//----------------------------------------------------------------------------------

-(instancetype) initWithName:(NSString *)name
{
if (self = [super init]) {
bookName = [NSString stringWithString:name];
book = [NSMutableArray array];
}
return self;
}

//----------------------------------------------------------------------------------

-(instancetype) init
{
return [self initWithName:@"NoName"];
}

//----------------------------------------------------------------------------------

-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:bookName forKey:@"AddressBookBookName"];
[aCoder encodeObject:book forKey:@"AddressBookBook"];
}

//----------------------------------------------------------------------------------

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
bookName = [aDecoder decodeObjectForKey:@"AddressBookBookName"];
book = [aDecoder decodeObjectForKey:@"AddressBookBook"];
}
return self;
}

//----------------------------------------------------------------------------------

main.m

Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "AddressBook.h"


int main(int argc, const char * argv[]) {
@autoreleasepool {
// create address cards
AddressCard *card1 = [[AddressCard alloc]initWithFirstName:@"John"
  andLastName:@"Doe"
 andEmail:@"jDoe@mail.com"
  address:@"123 Any Street"
 city:@"Any Town"
state:@"OH"
  zipCode:@"12345"
  country:@"USA"
phone:@"800-555-1212"];
AddressCard *card2 = [[AddressCard alloc]initWithFirstName:@"Jane"
  andLastName:@"Doe"
 andEmail:@"jaDoe@mail.com"
  address:@"2123 Any Street"
 city:@"Any Town"
state:@"OH"
  zipCode:@"12345"
  country:@"USA"
phone:@"800-555-1212"];
AddressCard *card3 = [[AddressCard alloc]initWithFirstName:@"Alias"
  andLastName:@"Smith"
 andEmail:@"aSmith@mail.com"
  address:@"123 Last Stand"
 city:@"No Where"
state:@"AZ"
  zipCode:@"12345"
  country:@"USA"
phone:@"800-555-1212"];
AddressCard *card4 = [[AddressCard alloc]initWithFirstName:@"And"
  andLastName:@"Jones"
 andEmail:@"aJones@mail.com"
  address:@"123 Green Acres"
 city:@"Mayberry"
state:@"NC"
  zipCode:@"12345"
  country:@"USA"
phone:@"800-555-1212"];
AddressCard *card5 = [[AddressCard alloc]initWithFirstName:@"Bill"
andLastName:@"Hicock"
  andEmail:@"wbHicock@mail.com"
address:@"123 Dodge Street"
  city:@"Cleveland"
 state:@"OH"
zipCode:@"12345"
country:@"USA"
 phone:@"800-555-1212"];
AddressCard *card6 = [[AddressCard alloc]initWithFirstName:@"Wilbur"
andLastName:@"Wright"
  andEmail:@"wWright@mail.com"
address:@"123 KittyHawk Way"
  city:@"KittyHawk"
 state:@"NC"
zipCode:@"12345"
country:@"USA"
 phone:@"800-555-1212"];

AddressBook *myBook = [[AddressBook alloc]initWithName:@"My Address Book"];

printf("\nEntries in address book before adding cards: %li\n", [myBook entries]);

// add cards to book
[myBook addCard:card1];
[myBook addCard:card2];
[myBook addCard:card3];
[myBook addCard:card4];
[myBook addCard:card5];
[myBook addCard:card6];

printf("\nEntries in address book after adding cards: %li\n", [myBook entries]);

[myBook list];

for (AddressCard *nextCard in myBook.book)
[nextCard print];


// ****  Testing Chapter 19 archiving ****************************************
printf("\n\nTesting Chapter 19 archiving *************************\n\n");
if ([NSKeyedArchiver archiveRootObject:myBook toFile:@"/Users/...my name here.../testFiles/addrbook.archive"] == NO)
printf("\n\narchiving failed\n\n");

AddressBook *unArchivedBook;
unArchivedBook = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/...my name here.../testFiles/addrbook.archive"];
if (unArchivedBook == nil) {
printf("\n\nError: Failed to download from archive.\n\n");
return 1;
}
printf("\nEntries in the unarchived address book: %li\n\n", [unArchivedBook entries]);
[unArchivedBook list];
for (AddressCard *nextCard in unArchivedBook.book)
[nextCard print];


printf("\n\n******* end of archiving test *************************\n\n");
// ******* end of archiving test **************************************************************



results:

Entries in address book before adding cards: 0

Entries in address book after adding cards: 6
2018-02-26 14:18:48.183306-0600 Chapter 19 Exercise 4 AddressBook Archive[23711:2209582]
======== Contents of: My Address Book ==========

Doe, John               jDoe@mail.com                  
Doe, Jane               jaDoe@mail.com                  
Smith, Alias              aSmith@mail.com                
Jones, And                aJones@mail.com                
Hicock, Bill               wbHicock@mail.com              
Wright, Wilbur             wWright@mail.com                
====================================================


==================================
|                                |
|  John Doe                      |
|  jDoe@mail.com                 |
|  123 Any Street                |
|  Any Town      OH 12345           |
|  USA                           |
|  800-555-1212                  |
|      O                 O       |
==================================


==================================
|                                |
|  Jane Doe                      |
|  jaDoe@mail.com                |
|  2123 Any Street               |
|  Any Town      OH 12345           |
|  USA                           |
|  800-555-1212                  |
|      O                 O       |
==================================


==================================
|                                |
|  Alias Smith                    |
|  aSmith@mail.com               |
|  123 Last Stand                |
|  No Where      AZ 12345           |
|  USA                           |
|  800-555-1212                  |
|      O                 O       |
==================================


==================================
|                                |
|  And Jones                    |
|  aJones@mail.com               |
|  123 Green Acres               |
|  Mayberry      NC 12345           |
|  USA                           |
|  800-555-1212                  |
|      O                 O       |
==================================


==================================
|                                |
|  Bill Hicock                   |
|  wbHicock@mail.com             |
|  123 Dodge Street              |
|  Cleveland     OH 12345           |
|  USA                           |
|  800-555-1212                  |
|      O                 O       |
==================================


==================================
|                                |
|  Wilbur Wright                   |
|  wWright@mail.com              |
|  123 KittyHawk Way             |
|  KittyHawk     NC 12345           |
|  USA                           |
|  800-555-1212                  |
|      O                 O       |
==================================



Testing Chapter 19 archiving *************************


Entries in the unarchived address book: 6

2018-02-26 14:18:48.186274-0600 Chapter 19 Exercise 4 AddressBook Archive[23711:2209582]
======== Contents of: My Address Book ==========

Doe, John               (null)                          
Doe, Jane               (null)                          
Smith, Alias              (null)                          
Jones, And                (null)                          
Hicock, Bill               (null)                          
Wright, Wilbur             (null)                          
====================================================


==================================
|                                |
|  John Doe                      |
|  (null)                        |
|  (null)                        |
|  Any Town      OH 12345           |
|  USA                           |
|  (null)                        |
|      O                 O       |
==================================


==================================
|                                |
|  Jane Doe                      |
|  (null)                        |
|  (null)                        |
|  Any Town      OH 12345           |
|  USA                           |
|  (null)                        |
|      O                 O       |
==================================


==================================
|                                |
|  Alias Smith                    |
|  (null)                        |
|  (null)                        |
|  No Where      AZ 12345           |
|  USA                           |
|  (null)                        |
|      O                 O       |
==================================


==================================
|                                |
|  And Jones                    |
|  (null)                        |
|  (null)                        |
|  Mayberry      NC 12345           |
|  USA                           |
|  (null)                        |
|      O                 O       |
==================================


==================================
|                                |
|  Bill Hicock                   |
|  (null)                        |
|  (null)                        |
|  Cleveland     OH 12345           |
|  USA                           |
|  (null)                        |
|      O                 O       |
==================================


==================================
|                                |
|  Wilbur Wright                   |
|  (null)                        |
|  (null)                        |
|  KittyHawk     NC 12345           |
|  USA                           |
|  (null)                        |
|      O                 O       |
==================================


Program ended with exit code: 0
« Last Edit: February 26, 2018, 10:28:48 PM by J2000lbs » Logged
J2000lbs
Newbie
*
Posts: 6


Email
« Reply #1 on: February 27, 2018, 09:27:44 AM »

Another Duh moment.  I figured it out.  For some reason I had the instance variables of AddressCard as weak.  I can't remember why right now, it might have had something to do with the copy protocol, I'll have to check to make sure that still works (I need to comment better!  I'm learning).  I changed it to copy and it works correctly.  I've been reading the Apple documentation, Archives and Serializations Programming Guide, and saw in there that weak objects are archived differently.

Thanks to all who took a look at the post.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.