Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
ViewController Class
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableDictionary *content = [NSMutableDictionary dictionaryWithCapacity:1];
    Generator *temp = [[Generator alloc] init];
    [temp generateContent:content];
}

Content Class
Code:
#import "Content.h"

@implementation Content
@synthesize QuestionName = _QuestionName;
@synthesize Question = _Question;
@end

Generator Class
Code:
- (NSMutableDictionary *) generateContent: (NSMutableDictionary *) content 
{
    content = [NSMutableDictionary dictionaryWithCapacity:1];
    
Content *first = [[Content alloc] init];
first.QuestionName = @"Scenario 1";
first.Question = @"This 57 year old cook worker presented to the out of hours GP service having splashed a oven cleaner in his left eye. His work colleagues irrigated his eye in the sink and have driven him promptly  to see you. After instillation of proxymetacaine his visual acuity was 6/9.  \nShould you...";

[content setObject:first forKey:first];

Content *second = [[Content alloc] init];
second.QuestionName = @"Scenario 2";
second.Question = @"A 19 year old lady presented to her GP with a one week history of redness, mild discomfort, watering and blurred vision in her right eye.  The patient has a history of cold sores around her mouth and states that her ophthalmologist has given her steroid drops in the past. Instillation of flourescein gives the above lesion.  \nWhich is the correct treatment?";

[content setObject:second forKey:second];

return content;
 
You left out important details in your question like:

What should it do?
What does it do right now?
 
Generator Class
Code:
- (NSMutableDictionary *) generateContent: (NSMutableDictionary *) content 
{
[COLOR="Red"]    content = [NSMutableDictionary dictionaryWithCapacity:1];
[/COLOR]
If content is an NSMutableDictionary object passed in by the caller, what does the red-hilited do to the passed-in object? What did you intend this code to do?


Code:
[content setObject:first forKey:first];
Any time I see code where the object is the same as the key, I immediately think that an NSSet might be better. My second thought is that whoever wrote the code didn't understand the difference between an NSDictionary, an NSSet, and an NSArray. I don't know which to think here, because you haven't described what you expect this code to do.

Code:
return content;
Given that generateContent: returns a NSMutableDictionary, what does the caller of this method need to do? Does it do that?
 
It is very unlikely that these lines are correct:

[content setObject:first forKey:first];
[content setObject:second forKey:second];

Why would you set the object and key as the same things? It will be very difficult to recover the object by key...
 
Made some changes

ViewController Class
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableDictionary *content = [NSMutableDictionary    dictionaryWithCapacity:1];
    Generator *temp = [[Generator alloc] init];
    [temp generateContent:content];
[COLOR="Red"]Content *myfirst = [content setObjectForKey:@"first"];[/COLOR]

self.questionLabel.text = myfirst.question;

}

Content Class
Code:
#import "Content.h"

@implementation Content
@synthesize QuestionName = _QuestionName;
@synthesize Question = _Question;
@end

Generator Class
Code:
- (NSMutableDictionary *) generateContent: (NSMutableDictionary *) content 
{
[COLOR="red"]removed instatiation[/COLOR]
    // content = [NSMutableDictionary dictionaryWithCapacity:1];
    
Content *first = [[Content alloc] init];
first.QuestionName = @"Scenario 1";
first.Question = @"This 57 year old cook worker presented to the out of hours GP service having splashed a oven cleaner in his left eye. His work colleagues irrigated his eye in the sink and have driven him promptly  to see you. After instillation of proxymetacaine his visual acuity was 6/9.  \nShould you...";

[content setObject:first forKey:first];

Content *second = [[Content alloc] init];
second.QuestionName = @"Scenario 2";
second.Question = @"A 19 year old lady presented to her GP with a one week history of redness, mild discomfort, watering and blurred vision in her right eye.  The patient has a history of cold sores around her mouth and states that her ophthalmologist has given her steroid drops in the past. Instillation of flourescein gives the above lesion.  \nWhich is the correct treatment?";

[content setObject:second forKey:second];

return content;

I realise this is bad code, but I'm trying to get my head around some concepts.

I'm trying to generate a dictionary of questions and question names and then call upon those names and assign them to a label.

----------

screenshot20120111at163.png


When I click 'begin', I get this in console and sigabrt in main function.

2012-01-11 17:40:01.397 Quiz2[1596:707] -[Content copyWithZone:]: unrecognized selector sent to instance 0x38cbf0
2012-01-11 17:40:01.401 Quiz2[1596:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Content copyWithZone:]: unrecognized selector sent to instance 0x38cbf0'
*** First throw call stack:
(0x37e3e8bf 0x319241e5 0x37e41acb 0x37e40945 0x37d9b680 0x37dab6cf 0x3777 0x42a7 0x3522f7ff 0x3523bc39 0x3523baa9 0x3523b98f 0x3523b211 0x3523af53 0x3522f673 0x3522f349 0x37d98435 0x352119eb 0x352119a7 0x35211985 0x352116f5 0x3521202d 0x3521050f 0x3520ff01 0x351f64ed 0x351f5d2d 0x37913df3 0x37e12553 0x37e124f5 0x37e11343 0x37d944dd 0x37d943a5 0x37912fcd 0x35224743 0x2bf5 0x2b8c)
terminate called throwing an exception(gdb)
 
I realise this is bad code, but I'm trying to get my head around some concepts.

I'm trying to generate a dictionary of questions and question names and then call upon those names and assign them to a label.

You need to solve the problem conceptually before you can solve it in code.

To solve the problem conceptually Break It Down.

Suppose your collection of questions has just one Question&Answer object in it. How do you intend to retrieve that Q&A object?

Are the Q&A objects in the collection arranged by sequential position, e.g. index 0, index 1, etc.? If so, then an NSArray is a suitable collection.

If you insist on using a dictionary, then how do intend to retrieve each Q&A object? That is, exactly what value will you use as the key? If you can't think of what to use as the key, then maybe you shouldn't use a dictionary. You can't use the object you want to retrieve as the key, because if you had the key, then you already have the object you wanted, and the dictionary would serve no purpose. Conceptually, a dictionary associates an object you have (the key) with another object you want (the value). If the key and value are identical, then what you have is already what you want.

Problems that need dictionaries are generally pretty obvious about what should be the key and what should be the value associated with the key. If it's not obvious, you probably have the wrong kind of collection.

Once you've answer the question of which collection type to use, then that will determine how you retrieve each Q&A object. If it's an NSArray, then the retrieval is done by providing an integer index representing the object's position in the array. If it's an NSDictionary, then retrieval is done by a key object, so you have to answer the question of how to generate the correct key.

After you've got the retrieval value (index or key), you apply that to the collection to retrieve the specific Q&A object you want. Arrays and dictionaries have different methods for this. You'll need to use the appropriate method.

Now go back to the start and consider a collection with one Q&A object in it. You should be able to precisely answer the question of how you retrieve the desired object. If you know that, then you should also be able to figure out how to add more objects to the collection, and how they will be retrieved.


If this isn't the kind of conceptual breakdown you intended, then you need to explain what you're trying to accomplish in more detailed language. In fact, you probably need to write it down in detail, so there's a very clear understanding, for you and for us, of exactly what you want to accomplish.
 
Last edited:
Thank you

I'm so happy I found MacRumors!

Thank you for this.

I simply changed my keys so that they didn't match the name of the objects. I will definitely revise my collection strategy.

It's such a good feeling when code works isn't it! [even if it's crappy code]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.