I don’t have a clue why this error is showing. I have the following class:
#import <Foundation/Foundation.h>
#import "Answer.h"
@interface Question : NSObject {
NSString* qId;
NSString* qTitle;
NSString* qNumber;
NSString* sectionId;
NSString* qType;
Answer* answer;
}
@property (nonatomic, retain) NSString* qId;
@property (nonatomic, retain) NSString* qTitle;
@property (nonatomic, retain) NSString* qNumber;
@property (nonatomic, retain) NSString* sectionId;
@property (nonatomic, retain) NSString* qType;
@property (nonatomic, retain) Answer* answer;
#import "Question.h"
@implementation Question
@synthesize qId, qTitle, qNumber, sectionId, qType, answer;
-(id)init
{
if (self = [super init])
{
// Initialization code here
answer = [[Answer alloc]init];
}
return self;
}
-(void) dealloc{
[answer release];
[super dealloc];
}
@end
I have a property of type Question in the following class:
#import <Foundation/Foundation.h>
#import "TextInputQuestion.h"
@interface TextInputQuestionViewController : UIViewController {
Question *question;
IBOutlet UILabel *questionTitle;
IBOutlet UILabel *inputLabel;
IBOutlet UITextField *inputAnswer;
}
@property (nonatomic, retain) UILabel *questionTitle;
@property (nonatomic, retain) UILabel *inputLabel;
@property (nonatomic, retain) Question *question;
@property (nonatomic, retain) UITextField *inputAnswer;
- (void) addButton:(id)sender isLast:(BOOL)last;
- (void) setQuestionId:(NSString*)quId withTitle:(NSString*)quTitle number:(NSString*)quNum section:(NSString*)sId questionType:(NSString*)qt;
@end
I synthesize Question* question in the .m file.
In my QuestionnaireVieController class:
#import <UIKit/UIKit.h>
#import "JSONKit.h";
#import "dbConnector.h"
#import "SliderQuestionViewController.h";
#import "TextInputQuestionViewController.h";
#import "MainMenuProtocol.h";
@interface QuestionnaireViewController : UIViewController {
NSDictionary* questions;
NSMutableArray* questionArray;
NSMutableArray* answerArray;
dbConnector* db;
NSInteger currQNum;
NSString* qaTitle;
NSString* secId;
id<MainMenuProtocol>delegate;
}
@property(nonatomic, retain) NSDictionary* questions;
@property(nonatomic, retain) NSMutableArray* questionArray;
@property(nonatomic, retain) NSMutableArray* answerArray;
@property(nonatomic, retain) dbConnector* db;
@property(nonatomic) NSInteger currQNum;
@property(nonatomic, retain) NSString* qaTitle;
@property(nonatomic, retain) NSString* secId;
@property(nonatomic, retain) id <MainMenuProtocol> delegate;
-(void) setQuestions;
-(void) startQuestion:(NSInteger)index isLast:(BOOL)last;
-(void) loadQuestions;
-(void) initialise;
-(void) finishQuestionnaire:(id)sender;
-(void) switchViews:(id)sender;
@end
I use the following method in the .m file to populate the questionArray with different types of viewcontroller:
//assigns JSON to question objects
-(void) setQuestions {
questionArray = [[NSMutableArray alloc] init];
for (NSDictionary *q in self.questions) {
/* Create Question object and populate it */
id question;
if([[q objectForKey:@"type"] isEqualToString:@"Slider"]){
question = [[SliderQuestionViewController alloc]init];
//set min max values
}else if([[q objectForKey:@"type"] isEqualToString:@"Option"]){
}else if([[q objectForKey:@"type"] isEqualToString:@"TextInput"]){
question = [[TextInputQuestionViewController alloc]init];
}else if([[q objectForKey:@"type"] isEqualToString:@"ImagePicker"]){
}else{
//comments
}
[questionArray addObject:question];
[question setQuestionId:[q objectForKey:@"questionId"] withTitle:[q objectForKey:@"question"] number:[q objectForKey:@"questionNumber"] section:[q objectForKey:@"sectionId"] questionType: [q objectForKey:@"type"]];
[question release];
}
}
When I do the following in the above .m file:
NSLog(@"%@", [questionArray objectAtIndex:0]);
I get:
<TextInputQuestionViewController: 0x6062e90>
When I try to do:
NSLog(@"%@", [questionArray objectAtIndex:0].question);
I get an error:
request for member 'question' in something not a structure or union
Why on earth can it not access the question variable? Especially when it actually logs my controller object. am I missing something here…
You’re running into the madness that is dot-notation. Switch it to this, and I bet it works (or at least you get a useful error):
Dot-notation is a slapped-on addition in ObjC2, and the compiler still can’t always figure it out, especially when you’re trying to pass a message to an
id(likeobjectAtIndex:returns). The compiler gets confused, thinks this this is the dot you use for astructrather than a message in disguise, and bails.EDIT
This, btw, is actual Objective-C. Dot-notation is just a sloppy way of writing this. In particular, it’s important to understand that
foo.bar=bazis exactly “send the messagesetBar:tofoo.” The default behavior for that is to assign a property using the declared setter semantics, but that’s just the default. You can overridesetBar:to do anything. You can even reconfigure the message dispatch system to not call the methodsetBar:(this is not an obscure feature, it’s used by Core Data and Core Animation). Objective-C is a dynamic language, and unfortunately dot-notation tends to muddy this by making it look like Java.