small medium large xlarge

Back to: All Forums  Core Data
Generic-user-small
15 Nov 2009, 11:37
Jens (2 posts)

Hello,

I own the printed version of the book and I got a few problems.

First thing I noticed is that there are wrong Code Snippets.

For example on page 114:

- (NSString*)metadataFilename;
{
  .....
}

I am pretty sure that the semicolon is not meant to be there. There are a lot of other code samples where this happened.

The next thing that annoys me is that you never really know where to put the provided samples in your code.

The snippet on page 115 is a perfect example for that. I had to search the web (see here) to finally get a clue. It is meant to be in the updateMetadataForObjects method which is part of the return statement of the last code snippet on page 114. But this is with no word mentioned in the text that you have to first create this method :(

I would suggest to change the little grey boxes at the beginning of a snippet to not just hint the source code file where the snippet is meant to be written in but also the method name.

It’s also not mentioned that you need to import the PPRecipe.h. I mean this may be obvious for a lot of coders but beginners may fail to jump to that conclusion.

Furthermore the properties in the code sample on page 112 are never “initiated” (don’t know how to call this) in PPRecipe.m via @dynamic. Also the NSStrings kPPImagePath, kPPObjectID, kPPServes are never set.

And again the web helped (see here)

This is the missing code:

NSString *kPPImagePath = @"kPPImagePath";
NSString *kPPObjectID = @"kPPObjectID";
NSString *kPPServes = @"kPPServes";

And I think I found another wrong code snippet on page 116. The second code snippet:

for (NSString *filename in updatedObjects) {
	if (![object isKindOfClass:[PPRecipe class]]) {
		continue;
	}
	PPRecipe *recipe = (PPRecipe*)object;
	NSDictionary *metadata = [recipe metadata];
	filePath = [recipe metadataFilename];
	filePath = [path stringByAppendingPathComponent:filePath];
	[metadata writeToFile:filePath atomically:YES];
	[fileManager changeFileAttributes:attributesDictionary 
							   atPath:filePath];
}

But “object” is never declared in this context. I guess could be better:

for (NSManagedObject *object in updatedObjects) {
	if (![object isKindOfClass:[PPRecipe class]]) {
		continue;
	}
	PPRecipe *recipe = (PPRecipe*)object;
	NSDictionary *metadata = [recipe metadata];
	filePath = [recipe metadataFilename];
	filePath = [path stringByAppendingPathComponent:filePath];
	[metadata writeToFile:filePath atomically:YES];
	[fileManager changeFileAttributes:attributesDictionary 
							   atPath:filePath];
}

Nevertheless I am happy to have bought the book. It’s really great!

Greetings Jens

Avatarsmall_pragsmall
17 Nov 2009, 17:20
Marcus S. Zarra (284 posts)

First, the semi-colon works perfectly fine where it is and actually is of great assistance when you frequently write the implementation before the header. It allows you to copy and paste the method signature back and forth. Try it, it compiles just fine.

Second, review the example code that is part of the book. You can download it from this site. In there you will find that the strings are initialized in the PPRecipe.m file.

The sample code is part of the book. If we were to add every line of code from the sample projects to the book then you would be carrying around a 1000 page book instead of the concise tomb you now have. I highly suggest you download the samples which are referenced in the book and review them for the complete code. The code displayed in the text is the relevant portions of the whole with the goal of removing the noise so that the subject matter can be focused on.

BTW, everywhere you see a reference to download the sample you can be assured that the snippet of code does compile when in context. It is actually not copied into the book which can produce errors but the text is generated from the source. Therefore all of the projects compile and all of the snippets do work, again in context.

Generic-user-small
17 Nov 2009, 23:50
Jens (2 posts)

Hi Marcus,

thank you for your reply. I now found the code after I looked at the last page of the book under “Visit us Online”. Maybe you should give the reader a hint in chapter 1.2. I would never have found the url by myself since I would never have looked at the last page of the book. Or am I that blind and it’s already mentioned in one of the chapters? I have the version 2009-9-22 of the book ordered via Amazon.

I mean you say that there are references in the book but I didn’t found a single one in chapter 7.

But I am still happy to have bought the book ;) Helped me a lot so far.

Avatarsmall_pragsmall
19 Nov 2009, 02:14
Marcus S. Zarra (284 posts)

Every code sample has a path at the start of the code sample that tells you what example project that it is a part of and what file within that sample project the snippet is from. This is standardized across the PragProg books so it is not explicitly explained in the text of the book.

Also, in the PDF version of the book, I believe the links send you directly to the source code online for downloading.

Generic-user-small
18 Feb 2010, 05:43
Alexei Vinidiktov (1 post)

Marcus, not all the projects compile. The Spotlight project didn’t compile. It gave me 4 error messages.

And one of them is the error mentioned by Jens (object undeclared)

for (NSString *filename in updatedObjects) {
    if (![object isKindOfClass:[PPRecipe class]]) {
        continue;
    }

Here are the error messages that I got.

The first one was:

/Users/alexei/Projects/Core Data/code/Spotlight/v1_to_v2.xcmappingmodel:0:0 Attribute Mapping “RecipeIngredientToRecipeIngredient”.name – destination entity doesn’t have property named name

The second:

/Users/alexei/Projects/Core Data/code/Spotlight/v1_to_v2.xcmappingmodel:0:0 Attribute Mapping “RecipeIngredientToRecipeIngredient”.unitOfMeasure – destination entity doesn’t have property named unitOfMeasure

The third:

/Users/alexei/Projects/Core Data/code/Spotlight/AppDelegate.m:260:0 /Users/alexei/Projects/Core Data/code/Spotlight/AppDelegate.m:260: error: ‘object’ undeclared (first use in this function)

That’s the error mentioned by Jens.

And the fourth:

/Users/alexei/Projects/Core Data/code/Spotlight/PPRecipe.m:23:0 /Users/alexei/Projects/Core Data/code/Spotlight/PPRecipe.m:23: error: ‘kMDItemTistle’ undeclared (first use in this function)

This one’s just a typo. Should be kMDItemTitle

Avatarsmall_pragsmall
18 Feb 2010, 16:59
Marcus S. Zarra (284 posts)

I just did a search of the book and I am not seeing the typo “kMDItemTistle” anywhere in the book. What page do you see that typo on?

As for the mapping issues you are having, those mapping models are generated by Xcode. Specifically that first model, we do not manipulate it in any way in the book. I would recommend reviewing the versioning chapter as perhaps a step was missed.

Also you can download the sample code from this website and compare it to your to see what is different.

You must be logged in to comment