Chapter 8 : Programming Basics in Objective-C

This forum contains all the assignments and questions readers may have for each chapter.

Moderator: gary.bennett

Chapter 8 : Programming Basics in Objective-C

Postby gary.bennett » Sat Aug 14, 2010 3:38 pm

Programming Basics in Objective-C
Topics Covered:

    Objective-C Containers
    NSArray
    NSDictionary
    NSSet

Weekly Q&A for Objective-C for Absolute Beginners Book

Purpose: Answer questions about Objective-C and OOP from book or other related info.
This Webinar is held every week on Wednesday, from:
6:30 PM - 7:30 PM PST
Register Now at:
https://www2.gotomeeting.com/register/763610930
Once registered you will receive an email confirming your registration with information you need to join the Webinar.
System Requirements
PC-based attendees
Required: Windows® 7, Vista, XP or 2003 Server
Macintosh®-based attendees
Required: Mac OS® X 10.4.11 (Tiger®) or newer
Attachments
Ch08 - MyBookstore.zip
Chapter 8 Source Code
(45.57 KiB) Downloaded 502 times
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Zorro » Tue Dec 06, 2011 7:44 pm

What happened to this board? I am in the midst of learning with the last (1st Edition) of this book and now it looks like the posts are gone and there is no more help relevant to the first edition. What is a person to do? It would be nice to leave the first edition board intact out of respect to those who were already learning from the 1st edition.

Please help.

Thank you.
Zorro
 
Posts: 0
Joined: Tue Dec 06, 2011 7:39 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby gary.bennett » Tue Dec 06, 2011 7:52 pm

zorro wrote:What happened to this board? I am in the midst of learning with the last (1st Edition) of this book and now it looks like the posts are gone and there is no more help relevant to the first edition. What is a person to do? It would be nice to leave the first edition board intact out of respect to those who were already learning from the 1st edition.

Please help.

Thank you.


Well, these issues always occur on forums when moving from one edition and technology to the next. People were starting to complain that examples shown from before Xcode 4.2 and iOS 5 didn't look the same with their version of the compiler and SDK. Plus, the chapter numbers are different. I had to make a jump and delineation at some point so not confuse people between the two books. Sorry if this caused issues as you move forward.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Zorro » Wed Dec 07, 2011 10:38 am

gary.bennett wrote:Well, these issues always occur on forums when moving from one edition and technology to the next. People were starting to complain that examples shown from before Xcode 4.2 and iOS 5 didn't look the same with their version of the compiler and SDK. Plus, the chapter numbers are different. I had to make a jump and delineation at some point so not confuse people between the two books. Sorry if this caused issues as you move forward.


Okay, I admit... your strategy worked. I broke down and bought the Kindle (cheaper and electronic) version of the 2nd edition from Amazon today. Currently I was using the Books24x7 through my employer to access the first edition. They (Books24x7) told me it could take quite a while to get your newest edition licensed and electronically available on their website. Even though I don't really like "changing horses in the middle of the stream" this was my first dive into this language and I want to make sure the content matches my current version of Xcode, so I can get this training right. I tried/looked at other books on Books24x7 and the way you teach this is just right for me.

I have 20+ years experience in programming from Cobol, PL/1, C, and VB.NET among other scripting languages. Most of my background has been procedural-style programming with limited exposure (mostly with VB.NET) to OOP. Your book was right on target with what I need and was well worth my more recent investment in the e-version of your latest (2nd) edition. What I am saying is I don't mind the "nudge" to purchase the latest edition that I got when you revamped your forum content.

In fact, I appreciate your keeping the training material timely and up to date. I am sure my learning experience will now be much more relevant without having to translate dated material into today's technology.
Zorro
 
Posts: 0
Joined: Tue Dec 06, 2011 7:39 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby gary.bennett » Wed Dec 07, 2011 2:53 pm

Thanks for your thoughts and kind remarks. I wasn't trying to get you to buy the book, but thanks. :-) If you ever have any questions, email me directly. gary at xcelme dot com

Thanks
Gary
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Zorro » Wed Dec 07, 2011 5:05 pm

gary.bennett wrote:Thanks for your thoughts and kind remarks. I wasn't trying to get you to buy the book, but thanks. :-) If you ever have any questions, email me directly. gary at xcelme dot com

Thanks
Gary


I know your goal was not to try to get me to buy the book. The effect of the situation with the changeover in the forum content, was a catalyst in moving me in the right direction to purchase a more up-to-date version relevant to today's iOS programming environment. I just played up the effect on me a bit in my post. :P It definitely served me well to make this move and I am happy to have more relevant content as a result.

Thanks!
Zorro
 
Posts: 0
Joined: Tue Dec 06, 2011 7:39 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Zorro » Thu Dec 22, 2011 9:30 am

Are there any plans to post the code as it would appear after completing the exercise at the end of the chapter? This would be a "nice to have" for all chapters in the book with Objective-C code-related exercises. I think it is beneficial as a "help" when attempting to do the exercises in the book.

Thanks!
Zorro
 
Posts: 0
Joined: Tue Dec 06, 2011 7:39 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Zorro » Thu Dec 22, 2011 4:03 pm

I am totally frustrated. I was able to figure most of the exercises out, except for the more advanced "sort" challenge and how to set up a separate method for handling initialization per the Bookstore implementation. I could really use some code examples for how to do this. I know there is a downloadable package of code for the chapter code project. How about some code for the exercises? It would really help folks like me be able to "visualize" how to do some of these more challenging exercises.

Thank you!
Zorro
 
Posts: 0
Joined: Tue Dec 06, 2011 7:39 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Zorro » Sat Dec 24, 2011 8:03 am

This chapter proved to be a veritable challenge (almost "brickwall", but I don't give up that easy). Anyway, I figured out the exercises. Right or wrong in the way I did it, I surmounted one challenge I had to create a separate method to initialize the list of Book objects and also surmounted the even tougher challenge (tougher is a mild term considering my experience with this obstacle).

Anyway, here's what the affected section of code from the MasterViewController.m looks like afterwards:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"My iBookstore", @"My iBookstore");
self.myBookstore = [[Bookstore alloc] init];
[self.myBookstore loadBooks];
[self.myBookstore sortBooks:@"title"];
}
return self;
}


Here's my Bookstore.h:

#import <Foundation/Foundation.h>
#import "Book.h"

@interface Bookstore : NSObject

@property (strong, nonatomic) NSMutableArray* theBookStore;

- (NSUInteger) count;
- (Book*) bookAtIndex:(NSUInteger) index;
- (void) loadBooks;
- (void) sortBooks:(NSString *) bookKey;

@end


Here's the implementation of the Bookstore (Bookstore.m):
#import "Bookstore.h"

@implementation Bookstore

@synthesize theBookStore;

- (id) init
{
self = [super init];

if (self)
{
self.theBookStore = [[NSMutableArray alloc] init];
}

return self;
}

- (NSUInteger) count
{
return theBookStore.count;
}

- (Book *)bookAtIndex:(NSUInteger)index
{
return [theBookStore objectAtIndex:index];
}

- (void) loadBooks
{
Book *newBook = [[Book alloc] init];
newBook.title = @"Objective-C for Absolute Beginners: iPhone, iPad and Mac Programming Made Easy";
newBook.author = @"Bennett, Fisher and Lees";
newBook.isbn = @"1430236531";
newBook.description = @"iOS Programming made easy.";
[theBookStore addObject:newBook];

newBook = [[Book alloc] init];
newBook.title = @"A Farewell To Arms";
newBook.author = @"Ernest Hemingway";
newBook.isbn = @"0848821440";
newBook.description = @"The story of an affair between an English nurse and an American soldier on the Italian front during World War I.";
[theBookStore addObject:newBook];

newBook = [[Book alloc] init];
newBook.title = @"Moby Dick";
newBook.author = @"Herman Melville";
newBook.isbn = @"0099511185";
newBook.description = @"When Ishmael sets sail on the whaling ship Pequod one cold Christmas Day, he has no idea of the horrors awaiting him out on the vast and merciless ocean. The ship’s strange captain, Ahab, is in the grip of an obsession to hunt down the famous white whale, Moby Dick, and will stop at nothing on his quest to annihilate his nemesis.";
[theBookStore addObject:newBook];

newBook = [[Book alloc] init];
newBook.title = @"A Christmas Carol";
newBook.author = @"Charles Dickens";
newBook.isbn = @"1456407872";
newBook.description = @"The tale begins on Christmas Eve seven years after the death of Ebenezer Scrooge's business partner Jacob Marley. Scrooge is established within the first stave (chapter) as a greedy and stingy businessman who has no place in his life for kindness, compassion, charity, or benevolence. After being warned by Marley's ghost to change his ways, Scrooge is visited by three additional ghosts each in its turn who accompany him to various scenes with the hope of achieving his transformation.";
[theBookStore addObject:newBook];

newBook = nil;
}

- (void) sortBooks:(NSString *)bookKey
{
//Sort collection of books by title
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:bookKey ascending:YES];
NSArray * descriptors = [NSMutableArray arrayWithObject:valueDescriptor];
NSArray * sortedBooks = [theBookStore sortedArrayUsingDescriptors:descriptors];

//Clear unsorted book inventory and reload with sorted list of books
[theBookStore removeAllObjects];
[theBookStore addObjectsFromArray:(sortedBooks)];
}
@end


I certainly welcome feedback for an improved, slicker way to accomplish this. However, after hours and hours of frustration spanning multiple days (the sometimes steep price of learning), I felt elated to finally arrive at this point!

Hope this helps someone else.
Zorro
 
Posts: 0
Joined: Tue Dec 06, 2011 7:39 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Pro » Mon Feb 27, 2012 6:34 am

Good day. :D
While implementing the example code from this chapter,I got a mistake.When I touch the field on the iPhone simulator,the following output occurs:

MyBookstore[2356:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DPDetailViewController 0x68906b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key author.'
*** First throw call stack:
(0x13be052 0x154fd0a 0x13bdf11 0x9b5032 0x926f7b 0x926eeb 0x941d60 0x23491a 0x13bfe1a 0x1329821 0x23346e 0xdae2c 0xdb3a9 0xdb5cb 0xdb941 0xed47d 0xed66f 0xed93b 0xee3df 0xee986 0xee5a4 0x31b2 0xa571d 0xa5952 0x92d86d 0x1392966 0x1392407 0x12f57c0 0x12f4db4 0x12f4ccb 0x12a7879 0x12a793e 0x15a9b 0x2238 0x2195)
terminate called throwing an exceptionCurrent language: auto; currently objective-c

I've checked thoroughly several times,all is done correct (I mean the code).Where should I look for mistake?
Thanks in advance.
Pro
 
Posts: 0
Joined: Sun Feb 26, 2012 6:04 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby gary.bennett » Fri Mar 02, 2012 10:01 am

Pro wrote:Good day. :D
While implementing the example code from this chapter,I got a mistake.When I touch the field on the iPhone simulator,the following output occurs:

MyBookstore[2356:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DPDetailViewController 0x68906b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key author.'
*** First throw call stack:
(0x13be052 0x154fd0a 0x13bdf11 0x9b5032 0x926f7b 0x926eeb 0x941d60 0x23491a 0x13bfe1a 0x1329821 0x23346e 0xdae2c 0xdb3a9 0xdb5cb 0xdb941 0xed47d 0xed66f 0xed93b 0xee3df 0xee986 0xee5a4 0x31b2 0xa571d 0xa5952 0x92d86d 0x1392966 0x1392407 0x12f57c0 0x12f4db4 0x12f4ccb 0x12a7879 0x12a793e 0x15a9b 0x2238 0x2195)
terminate called throwing an exceptionCurrent language: auto; currently objective-c

I've checked thoroughly several times,all is done correct (I mean the code).Where should I look for mistake?
Thanks in advance.


Look at your code where you accessing a NSDictionary. Do you have the key spelled correctly?
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 8 :Problems with exercise

Postby kennysmith » Mon Mar 12, 2012 7:47 pm

I am very new to the programming world!

I have been having trouble with the myBookStore exercise in chapter 8. When I go to put the code into the MasterViewController.m the books says the particular code starts on line #83 but it actually starts on line 68. I have tried 3 times, all from scratch and have not been able to get the program to work. Please help me understand what I am doing wrong.

Thanks!
kennysmith
 
Posts: 0
Joined: Mon Mar 12, 2012 1:11 pm

Re: Chapter 8 :Problems with exercise

Postby gary.bennett » Mon Mar 12, 2012 8:16 pm

kennysmith wrote:I am very new to the programming world!

I have been having trouble with the myBookStore exercise in chapter 8. When I go to put the code into the MasterViewController.m the books says the particular code starts on line #83 but it actually starts on line 68. I have tried 3 times, all from scratch and have not been able to get the program to work. Please help me understand what I am doing wrong.

Thanks!


Kenny, it is not so important as to the line number you start coding in. The differences in Xcode versions and the code the templates produce can cause differences. What is important is the code.
Hope that helps.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby ymarcus93 » Sun Apr 15, 2012 8:14 am

Zorro wrote:This chapter proved to be a veritable challenge (almost "brickwall", but I don't give up that easy). Anyway, I figured out the exercises. Right or wrong in the way I did it, I surmounted one challenge I had to create a separate method to initialize the list of Book objects and also surmounted the even tougher challenge (tougher is a mild term considering my experience with this obstacle).

Anyway, here's what the affected section of code from the MasterViewController.m looks like afterwards:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"My iBookstore", @"My iBookstore");
self.myBookstore = [[Bookstore alloc] init];
[self.myBookstore loadBooks];
[self.myBookstore sortBooks:@"title"];
}
return self;
}


Here's my Bookstore.h:

#import <Foundation/Foundation.h>
#import "Book.h"

@interface Bookstore : NSObject

@property (strong, nonatomic) NSMutableArray* theBookStore;

- (NSUInteger) count;
- (Book*) bookAtIndex:(NSUInteger) index;
- (void) loadBooks;
- (void) sortBooks:(NSString *) bookKey;

@end


Here's the implementation of the Bookstore (Bookstore.m):
#import "Bookstore.h"

@implementation Bookstore

@synthesize theBookStore;

- (id) init
{
self = [super init];

if (self)
{
self.theBookStore = [[NSMutableArray alloc] init];
}

return self;
}

- (NSUInteger) count
{
return theBookStore.count;
}

- (Book *)bookAtIndex:(NSUInteger)index
{
return [theBookStore objectAtIndex:index];
}

- (void) loadBooks
{
Book *newBook = [[Book alloc] init];
newBook.title = @"Objective-C for Absolute Beginners: iPhone, iPad and Mac Programming Made Easy";
newBook.author = @"Bennett, Fisher and Lees";
newBook.isbn = @"1430236531";
newBook.description = @"iOS Programming made easy.";
[theBookStore addObject:newBook];

newBook = [[Book alloc] init];
newBook.title = @"A Farewell To Arms";
newBook.author = @"Ernest Hemingway";
newBook.isbn = @"0848821440";
newBook.description = @"The story of an affair between an English nurse and an American soldier on the Italian front during World War I.";
[theBookStore addObject:newBook];

newBook = [[Book alloc] init];
newBook.title = @"Moby Dick";
newBook.author = @"Herman Melville";
newBook.isbn = @"0099511185";
newBook.description = @"When Ishmael sets sail on the whaling ship Pequod one cold Christmas Day, he has no idea of the horrors awaiting him out on the vast and merciless ocean. The ship’s strange captain, Ahab, is in the grip of an obsession to hunt down the famous white whale, Moby Dick, and will stop at nothing on his quest to annihilate his nemesis.";
[theBookStore addObject:newBook];

newBook = [[Book alloc] init];
newBook.title = @"A Christmas Carol";
newBook.author = @"Charles Dickens";
newBook.isbn = @"1456407872";
newBook.description = @"The tale begins on Christmas Eve seven years after the death of Ebenezer Scrooge's business partner Jacob Marley. Scrooge is established within the first stave (chapter) as a greedy and stingy businessman who has no place in his life for kindness, compassion, charity, or benevolence. After being warned by Marley's ghost to change his ways, Scrooge is visited by three additional ghosts each in its turn who accompany him to various scenes with the hope of achieving his transformation.";
[theBookStore addObject:newBook];

newBook = nil;
}

- (void) sortBooks:(NSString *)bookKey
{
//Sort collection of books by title
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:bookKey ascending:YES];
NSArray * descriptors = [NSMutableArray arrayWithObject:valueDescriptor];
NSArray * sortedBooks = [theBookStore sortedArrayUsingDescriptors:descriptors];

//Clear unsorted book inventory and reload with sorted list of books
[theBookStore removeAllObjects];
[theBookStore addObjectsFromArray:(sortedBooks)];
}
@end


I certainly welcome feedback for an improved, slicker way to accomplish this. However, after hours and hours of frustration spanning multiple days (the sometimes steep price of learning), I felt elated to finally arrive at this point!

Hope this helps someone else.


Hello Zorro. Thank you a lot for posting what you did. It helped me very much. Though when I compile the program it succeeds, but then gives me an error. The error occurs in the MasterViewController.m file. It's on line 94 which is different for you probably, so I'll just paste the line in for you.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


*cell.textLabel.text = [self.myBookstore bookatIndex:indexPath.row].title;
return cell;
}

When I run it gives me an error on the line with a star ( I put the * there). This probably happens because before I compile the program, it says in my implementation file (Bookstore.m) that it is an incomplete implementation. I copied and pasted everything you did+and then changed the books to be the books I want and added a few more attributes. I'm sure I copied everything you did, and the attributes are correct. It's just that one line (the * line). Please help me.

If the code above isn't enough, I can send the entire project for you to look at.
ymarcus93
 
Posts: 0
Joined: Tue Apr 10, 2012 1:13 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby joseph.toronto » Sat Jun 30, 2012 4:59 pm

I typed everything in exactly how the book shows it and I'm getting a compiler error stating "Incompatible pointer types passing 'NSString *' to parameter of type 'UItextView *' on this line of the configureView method:

self.descriptionTextView = theBook.description;


It wants something other than an NSString? I can't quite figure this one out.
joseph.toronto
 
Posts: 4
Joined: Sat Jun 23, 2012 12:29 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby gary.bennett » Mon Jul 02, 2012 9:48 am

joseph.toronto wrote:I typed everything in exactly how the book shows it and I'm getting a compiler error stating "Incompatible pointer types passing 'NSString *' to parameter of type 'UItextView *' on this line of the configureView method:

self.descriptionTextView = theBook.description;


It wants something other than an NSString? I can't quite figure this one out.



Hi Joseph. I can promise you use didn't type it in as in the book. Did you look at the source code that is provided with the book and compare with yours?
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby floortje » Tue Oct 30, 2012 3:13 am

I've been working on Chapter 8 and I struggled with setting up the bookstore as described in the chapter (not done any of the assignments yet). I made it work in the end, but I'm getting two issue warnings.

'unused variable object'

it's because of this
Code: Select all
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Book *selectedBook = [self.myBookStore bookAtIndex:indexPath.row];
   
    if (!self.detailViewController) {
        self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    }
     :?  NSDate *object = _objects[indexPath.row];
    self.detailViewController.detailItem = selectedBook;
    [self.navigationController pushViewController:self.detailViewController animated:YES];
}


If I delete the code where I put the : ? in front, I loose the warnings and the program still runs.
I compared my code with the code from the zip.file in this thread and discovered that line isn't even there. But the book doesn't say I should delete the line. Is this because this piece of code is only in the newer Xcode? And if so... what does it do, and should I leave it or should I delete it?

There's also another difference I found, and I want to make sure if I need to incorporate it or not, or what the effect is when I don't:
in the bookstore implementation file, after you put the new books in the array, in the zip file it says
Code: Select all
newBook = nil;

In the book this doesn't seem to appear.
floortje
 
Posts: 0
Joined: Fri Oct 26, 2012 2:57 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby SaveMe » Mon Feb 18, 2013 7:55 am

There's a small thingie in this chapter I am curious about.
We've declared a property in the bookstore.h file

Code: Select all
//Define properties
@property (strong, nonatomic) NSMutableArray *theBookStore;


In the implementation file we use this property a lot. What confuses me is that sometimes we
access it like

Code: Select all
[self.theBookStore addObject:newBook]


A regular [receiver message] piece of code. But sometimes we are able to do things like

Code: Select all
return theBookStore.count;


Where naturally [thebookstore count] also works. BUT

Code: Select all
return [theBookStore objectAtIndex:index]


This works, but thebookstore.objectAtIndex is not. In fact the method isn't even available this way.

I find this confusing, and I am inclined to hold on to [receiver message] in all cases. Why this difference and why also the difference in methods accessible ?
SaveMe
 
Posts: 0
Joined: Wed Feb 13, 2013 9:13 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby bgilbert » Thu Mar 21, 2013 2:01 am

Hi,

In Listing 8-15 Gary say's "Also, don't forget to add the descriptionTextView to the @synthesize statement in the DetailViewController.m file as shown below:". Trouble was I didn't have a @synthesize statement in my DetailViewController.m file. I took a punt and placed it as follows. When I ran the build I got an error message stating "!Missing context for property implementation declaration".


//
// DetailViewController.m
// MyBookstore
//
// Created by Brian Gilbert on 20/03/13.
// Copyright (c) 2013 Brian Gilbert. All rights reserved.
//

#import "DetailViewController.h"

@synthesize titleLable, authorLabel, descriptionTextView;

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController


#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;

// Update the view.
[self configureView];
}
bgilbert
 
Posts: 0
Joined: Mon Mar 18, 2013 4:42 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby moyses.filipe » Sun Jun 16, 2013 5:21 pm

Gary, as I told you I am really new to development and I've been struggling to get to chapter 8 but now I am completely lost.

The code you attached to the post has so much more than the one we got from the book and even more than my Master-Detail Application template.

I am using xCode 4.6.3

Really, I can see that your code works but I don't get the concepts. Is this how's supposed to be? this chapter thought me so much but when the app didn't work I kind of lost all the fundamentals I've learned, simply because I can't understand where did all that code came from.

Please help out!

Thanks,
Filipe
moyses.filipe
 
Posts: 1
Joined: Sun Jun 09, 2013 5:35 pm

Re: Chapter 8 : Programming Basics in Objective-C

Postby Landre » Wed Aug 21, 2013 12:43 am

Hi,

I had the same problem of bgilbert.
I solved it writing the @synthesize just after @implementation, like this:


#import "DetailViewController.h"

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController

@synthesize titleLabel, authorLabel, descriptionTextView;

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;

// Update the view.
[self configureView];
}
}

etc...

Might be a general rule? I'm trying to figure out...
I'm completelly new to programation as well.

Hope this helps.
Landre
Landre
 
Posts: 0
Joined: Tue Aug 20, 2013 7:13 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby Landre » Wed Aug 21, 2013 1:01 am

Hi!
I have a question about Listing 8-7.
We defined the -(id) init method in the Bookstore.m file, but we didn't write it in the interface (.h) file, like we did with all the others methods. Why it is not necessary?
I mean, I still didn't finish the program, but there's not error message so far, so I suppose the -(id) init method must not be necessary in the .h file. Is this true?

Thanks.
Landre
Landre
 
Posts: 0
Joined: Tue Aug 20, 2013 7:13 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby Landre » Wed Aug 21, 2013 6:37 am

Please help!

I wrote all the code just like in the book, but at running the app, the simulator shows the list, but when I click on a title, it crashes and in XCode the following error message appear in main.m:

//
// main.m
// myBookstore
//
// Created by Landre on 20/08/13.
// Copyright (c) 2013 Landre. All rights reserved.
//

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

Error message: Thread1: signal SIGABRT

2013-08-21 15:31:41.131 myBookstore[806:11303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "DetailViewController" nib but the view outlet was not set.'
*** First throw call stack:
(0x1c95012 0x10d2e7e 0x1c94deb 0xf78c8 0xf7dc8 0xf7ff8 0xf8232 0xf84da 0x10f8e5 0x10f9cb 0x10fc76 0x10fd71 0x11089b 0x110e93 0x110a88 0x33c4 0xc6285 0xc64ed 0xad05b3 0x1c54376 0x1c53e06 0x1c3ba82 0x1c3af44 0x1c3ae1b 0x1bef7e3 0x1bef668 0x16ffc 0x1ecd 0x1df5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)

What's going on?
Thanks!
Landre
 
Posts: 0
Joined: Tue Aug 20, 2013 7:13 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby Landre » Mon Aug 26, 2013 8:49 am

Landre wrote:Hi!
I have a question about Listing 8-7.
We defined the -(id) init method in the Bookstore.m file, but we didn't write it in the interface (.h) file, like we did with all the others methods. Why it is not necessary?
I mean, I still didn't finish the program, but there's not error message so far, so I suppose the -(id) init method must not be necessary in the .h file. Is this true?

Thanks.
Landre


Got it! I answer to myself, just in case someone else is at the beginning of programmation, like me.
The answer is that when overwriting a method, we don't need to declare it in the header file because it's already declared by its superclass.
Another example is the description method. If we define a description method, there no need to declare it in the .h file.
Am I right?
Landre
 
Posts: 0
Joined: Tue Aug 20, 2013 7:13 am

Re: Chapter 8 : Programming Basics in Objective-C

Postby lelouch5069 » Wed Nov 06, 2013 6:37 am

1.Any guys know about the ways to initialize the project "book" in stead of using the way of "init"?
2.Question 5 after the lesson, how to set the text with the "detailTextLabel"?
I search the developer file with two results:
detailTextLabel
A detail text label for the view. (read-only)

@property(nonatomic, readonly, retain) UILabel *detailTextLabel
Discussion
This property is only used for tables configured with UITableViewStyleGrouped.

Accessing the value in this property causes the view to create a default label for displaying a detail text string. If you are managing the content of the view yourself by adding subviews to the contentView property, you should not access this property.

The label is sized to fit the content view area in the best way possible based on the size of the string. Its size is also adjusted depending on whether there is a primary text label present.


detailTextLabel
Returns the secondary label of the table cell if one exists. (read-only)

@property(nonatomic, readonly, retain) UILabel *detailTextLabel
Discussion
Holds the secondary (or detail) label of the cell. UITableViewCell adds an appropriate label when you create the cell in a style that supports secondary labels. If the style doesn’t support detail labels, nil is returned. See “UITableViewCellStyle” for descriptions of the main label in currently defined cell styles.


Which one is suit for the question? I have no idea about the instruction above and could you kindly teach us how to solve the problem by reading the developer's file ?
lelouch5069
 
Posts: 0
Joined: Mon Oct 28, 2013 6:15 am


Return to Book -> Objective-C for Absolute Beginners: (2nd Edition) iPhone and Mac Programming Made Easy

cron