I'm adapting This tutorial to my app, and I've got it boiled down to one last error, which is stopping me in my tracks. The program is unable to find a property in another file, but that property is clearly defined. Here is the code in question:
The actual error line:
for (DTContact *dtc in _dtContact.contact) {
the .h for the file, and items in question:
#import <UIKit/UIKit.h>
#class XMLTestViewController;
#class DTCXMLResponse;
#interface XMLTestController : UIViewController{
UIWindow *window;
XMLTestViewController *viewController;
DTCXMLResponse *_dtContact;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet XMLTestViewController *viewController;
#property (nonatomic, retain) DTCXMLResponse *dtContact;
#property (nonatomic, retain) IBOutlet UIButton *mybutton;
-(IBAction)buttonClicked;
#end
It's having issues with the _dtContact.contact. It can't find the contact in the file DTCXMLResponse. Here is the .h file and the section of the .m:
.h
#import <Foundation/Foundation.h>
#interface DTContactXMLResponse : NSObject {
NSMutableArray *_contact;
}
#property (nonatomic, retain) NSMutableArray *contact;
#end
.m
#import "DTCXMLResponse.h"
#implementation DTContactXMLResponse
#synthesize contact = _contact;
- (id)init {
if ((self = [super init])) {
self.contact = [[NSMutableArray alloc] init];
}
return self;
}
#end
So theres that. As you can see, I have 'contact' propertied in the DTCXMLResponse.h, and linked in the .m.
This error usually points out that Xcode can not recognize your symbol.
I can assume this is DTContact.
Try to insert this in your .h file:
#import DTContact.h
Its not relevant to ur case but I was getting the same error. I googled for a solution but the problem was in my code. I was using different class object as I was copy pasting similar snippet of code in my project. So here is the problem that I had for the same error :
For my classA , I had some code snippet like :
ManagedObjectOfClassA * managedObjectOfClassA = [NSEntityDescription insertNewObjectForEntityForName:#"ManagedObjectOfClassA" inManagedObjectContext:managedObjectContext];
managedObjectOfClassA.somePropertyA = sameValue; //somePropertyA is an attribute of ManagedObjectOfClassA
And similar code for class B :
ManagedObjectOfClassA *managedObjectOfClassB = [NSEntityDescription insertNewObjectForEntityForName:#"ManagedObjectOfClassB" inManagedObjectContext:managedObjectContext];
managedObjectOfClassB.somePropertyB = someValue;////somePropertyB is an attribute of ManagedObjectOfClassB
If u look closely, the mistake was in assigning the right entities to the corresponding objects in class B.
So the problem is in code of class B. And the correct code would be :
ManagedObjectOfClassB *managedObjectOfClassB = [NSEntityDescription insertNewObjectForEntityForName:#"ManagedObjectOfClassB" inManagedObjectContext:managedObjectContext];
managedObjectOfClassB.somePropertyB.someValue;
I hope that helps someone.
Related
I'm pretty new to classes in objective-c. I'm testing out one I made unhiding an image from the class on the click of a button, everything compiles and runs but it's not working out for me, nothing shows up when I click. Am I doing this all wrong? Tried to include everything I think is relevant in the code below, obviously the images for the other parts of the class are there but I didn't include them below seeing as I'm just testing one image from it.
question.m file -
#import "Question.h"
#implementation Question
#synthesize image;
#end
question.h -
#import <UIKit/UIKit.h>
#import "ViewController.h"
#ifndef Question_h
#define Question_h
#endif /* Question_h */
#interface Question : NSObject
{
UIImageView *image;
}
#property(nonatomic, retain) UIImageView *image;
#end
#class Question;
#interface Controller : NSObject
{
Question *firstQuestion;
Question *secondQuestion;
Question *thirdQuestion;
}
#property(nonatomic, retain) Question *firstQuestion;
#property(nonatomic, retain) Question *secondQuestion;
#property(nonatomic, retain) Question *thirdQuestion;
-(void) Question;
ViewController.m -
#import "ViewController.h"
#import "Question.h"
#synthesize question1, q2, q3, firstaid;
- (void)viewDidLoad {
[super viewDidLoad];
firstTierQuestions = [[NSMutableArray alloc] initWithCapacity:100];
}
- (IBAction)Question:(id)sender {
Question *firstQuestion = [[Question alloc] init];
firstQuestion.image = question1;
firstQuestion.a1 = _answer1;
firstQuestion.a2 = _answer2;
[firstTierQuestions addObject:firstQuestion];
- (IBAction)generator:(id)sender {
for (Question *firstQuestion in firstTierQuestions) {
[firstQuestion.image setHidden:NO];
}
}
viewController.h -
#import "question.h"
#interface ViewController : UIViewController
{
IBOutlet UIImageView *question1;
NSMutableArray *firstTierQuestions;
}
#property (strong, nonatomic) IBOutlet UIButton *generator;
#property (strong, nonatomic) IBOutlet UIImageView *question1;
- (IBAction)generator:(id)sender;
#property (nonatomic, strong) NSObject *Question;
#end
This question already has answers here:
Errors when making circular reference imports
(2 answers)
Closed 8 years ago.
I have two custom NSObjects which reference each other, as below:
.h file class 1
#import <Foundation/Foundation.h>
#import "MyChildClass.h"
#interface MyParentClass : NSObject
#property (nonatomic, strong) NSString *Name;
#property (nonatomic) MyChildClass *myChild;
#end
.m file class 1:
#import "MyParentClass.h"
#implementation MyParentClass
#end
and...
.h file class 2:
#import <Foundation/Foundation.h>
#import "MyParentClass.h"
#interface MyChildClass : NSObject
#property (nonatomic) MyParentClass *myParent;
#end
.m file class 2:
#import "MyChildClass.h"
#implementation MyChildClass
#end
When I try and implement those as below, the project will not build, with the error "Arc Restrictions: Implicit conversion of an Objective-C pointer to 'int *' is disallowed with ARC"
View Controller implementation code:
MyParentClass *newParent = [[MyParentClass alloc] init];
MyChildClass *newChild = [[MyChildClass alloc] init];
newParent.Name = #"Johnny";
newParent.myChild = newChild;
newChild.myParent = newParent;
However, when I nest a second example into single .h/.m files, the project will build:
Nested .h file:
#import <Foundation/Foundation.h>
#class MyNestedClass;
#interface MyChildNestedClass : NSObject
#property (nonatomic, strong) MyNestedClass *myNested;
#property (nonatomic, strong) NSString *ChildName;
#end
#interface MyNestedClass : NSObject
#property (nonatomic, strong) MyChildNestedClass *myChild;
#property (nonatomic, strong) NSString *ParentName;
#end
Nested .m file:
#import "MyNestedClass.h"
#implementation MyChildNestedClass
#end
#implementation MyNestedClass
#end
I guess that by nesting the objects into a single file, I am somehow tricking ARC and that really I shouldn't be creating these circular references.
Could anyone suggest the correct way of nesting types like this?
What I am trying to achieve, is the following pseudo-scenario: Parent has a property of Child. Child has a property of the parent it is the child of.
Am I finding this difficult because I'm not supposed to do it (circular referencing), or am I missing something glaringly obvious?
Many thanks!
Do not #import "MyChildClass.h" in the parent class' header file, change it to
#class MyChildClass
and add the #import in the source file (.m)
I'm adapting This tutorial to my app, and I've got it boiled down to one last error, which is stopping me in my tracks. The program is unable to find a property in another file, but that property is clearly defined. Here is the code in question:
The actual error line:
for (DTContact *dtc in _dtContact.contact) {
the .h for the file, and items in question:
#import <UIKit/UIKit.h>
#class XMLTestViewController;
#class DTCXMLResponse;
#interface XMLTestController : UIViewController{
UIWindow *window;
XMLTestViewController *viewController;
DTCXMLResponse *_dtContact;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet XMLTestViewController *viewController;
#property (nonatomic, retain) DTCXMLResponse *dtContact;
#property (nonatomic, retain) IBOutlet UIButton *mybutton;
-(IBAction)buttonClicked;
#end
It's having issues with the _dtContact.contact. It can't find the contact in the file DTCXMLResponse. Here is the .h file and the section of the .m:
.h
#import <Foundation/Foundation.h>
#interface DTContactXMLResponse : NSObject {
NSMutableArray *_contact;
}
#property (nonatomic, retain) NSMutableArray *contact;
#end
.m
#import "DTCXMLResponse.h"
#implementation DTContactXMLResponse
#synthesize contact = _contact;
- (id)init {
if ((self = [super init])) {
self.contact = [[NSMutableArray alloc] init];
}
return self;
}
#end
So theres that. As you can see, I have 'contact' propertied in the DTCXMLResponse.h, and linked in the .m.
This error usually points out that Xcode can not recognize your symbol.
I can assume this is DTContact.
Try to insert this in your .h file:
#import DTContact.h
Its not relevant to ur case but I was getting the same error. I googled for a solution but the problem was in my code. I was using different class object as I was copy pasting similar snippet of code in my project. So here is the problem that I had for the same error :
For my classA , I had some code snippet like :
ManagedObjectOfClassA * managedObjectOfClassA = [NSEntityDescription insertNewObjectForEntityForName:#"ManagedObjectOfClassA" inManagedObjectContext:managedObjectContext];
managedObjectOfClassA.somePropertyA = sameValue; //somePropertyA is an attribute of ManagedObjectOfClassA
And similar code for class B :
ManagedObjectOfClassA *managedObjectOfClassB = [NSEntityDescription insertNewObjectForEntityForName:#"ManagedObjectOfClassB" inManagedObjectContext:managedObjectContext];
managedObjectOfClassB.somePropertyB = someValue;////somePropertyB is an attribute of ManagedObjectOfClassB
If u look closely, the mistake was in assigning the right entities to the corresponding objects in class B.
So the problem is in code of class B. And the correct code would be :
ManagedObjectOfClassB *managedObjectOfClassB = [NSEntityDescription insertNewObjectForEntityForName:#"ManagedObjectOfClassB" inManagedObjectContext:managedObjectContext];
managedObjectOfClassB.somePropertyB.someValue;
I hope that helps someone.
I am newbie on Xcode, and trying to figure out more about coding in xcode.
So, I am trying to learn more about models (models operation) on objective C.
I am confused in #Class declaration in PhotoViewController.h and .m Files
as you may see below, I already imported Photo.h on appdelegate.m and also PhotoViewController.m files
the objective from my tutorial is PhotoViewController.m files can recognize self.photo.filename
But, why it has to add #Class and #property in PhotoViewController.h files?
isnt #import command is already enough? what does #Class means and why it has to include #property too?
note : I tried to put a comment (//) on #class , but xcode tell me that photo property not found, and when I put added comment (//) on property
PhotoViewController.m file also messed up with unrecognized photo property.
I dont quite understand, the use of #class and #import at the same time, plus declaring #property photo
here is Photo.m
#import "Photo.h"
#implementation Photo
-(id)init
{
self = [super init];
return self;
}
#end
and
Photo.h
#import <Foundation/Foundation.h>
#interface Photo : NSObject
#property (weak, atomic) NSString *title;
#property (strong, nonatomic) NSString *detail;
#property (strong, nonatomic) NSString *filename;
#property (strong, nonatomic) NSString *thumbnail;
#end
Appdelegate.m
#import "AppDelegate.h"
#import "FeedTableViewController.h"
#import "ProfileViewController.h"
#import "FavoritesViewController.h"
#import "Photo.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Photo *photo= [[Photo alloc]init];
photo.title = #"Demo Photo";
photo.detail = #"This is a demo photo";
photo.filename = #"demo.png";
photo.thumbnail = #"demo-thumb.png";
return YES;
}
#end
PhotoViewController.h Files
#import <UIKit/UIKit.h>
#class Photo;
#interface PhotoViewController : UIViewController
#property (weak, nonatomic) NSString *imageFileName;
#property (weak, nonatomic) NSString *imageTitle;
#property (strong, nonatomic) Photo *photo;
#end
PhotoViewController.m Files
#import "PhotoViewController.h"
#import "UIImageView+AFNetworking.h"
#import "Photo.h"
#implementation PhotoViewController
-(void)viewDidLoad {
// self.title = self.imageTitle;
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImageWithURL:[UIImage imageNamed:self.photo.filename]];
imageView.frame = CGRectMake(10,10,300,300);
[self.view addSubview:imageView];
UILabel *imageTitleLabel = [[UILabel alloc] init];
imageTitleLabel.text = self.imageTitle;
imageTitleLabel.frame = CGRectMake(11,320,300,40);
[self.view addSubview:imageTitleLabel];
}
#end
#class Photo defines the existence of class Photo to PhotoViewController.h allowing you to declare photo property.
Photo property is later used in PhotoViewController.m to to access the instance variable photo like this self.photo or [self photo]
You could have put #import "Photo.h" in your PhotoViewController.h but it is cleaner this way :)
#property
It is for replacement of getter method, whenever you want to get the value you have to declare that variable as property, so that need not to write getter method separately,
you should implement #synthesize also in Photo.m, #synthesize will work as setter method.
In my main view controller, I have a UITextField, and I am trying to save the text input into it to a NSString in my Homework model(class).
Homework.h
#import <Foundation/Foundation.h>
#interface Homework : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
#end
Homework.m
#import "Homework.h"
#implementation Homework
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
#end
In my assignmentViewController, I create an object of type Homework and try to set it equal to whatever is entered into the the UITextField when the Save Button is pressed.
Assignment View Controller
#import <UIKit/UIKit.h>
#import "Homework.h"
#interface Assignment : UIViewController {
}
#property(nonatomic) IBOutlet UITextField *ClassNameField;
#property(nonatomic) IBOutlet UILabel *ClassNameLabel;
#property (weak, nonatomic) IBOutlet UIButton *SaveButton;
#property (nonatomic, strong) Homework *homeworkAssignment;
- (IBAction)Save:(UIButton *)sender;
#end
AssignmentViewController.m
- (IBAction)Save:(UIButton *)sender {
self.homeworkAssignment.className = self.ClassNameField.text;
NSLog(#"SaveButtonPressed %#", self.homeworkAssignment.className);
}
The NSLog prints out that className is (null). Can anyone help me figure out what I am doing wrong? This is my first ever iOS app (besides Hello World).
Edit: This is using ARC
Edit: I tried changing
self.homeworkAssignment.className = self.ClassNameField.text; to
self.homeworkAssignment.className = #"TEST";
and the log still shows (Null).
Double check you properly linked ClassNameField outlet and that you're initializing homeworkAssignment. Something like.-
self.homeworkAssignment = [[Homework alloc] init];
By the way, you should consider using camelCase notation for your variable names :)
Well to be honest the first steps are always hard but you should learn it the right way, héhé
First of all synthesize this way:
#synthesize labelAssignmentTitle,labelClassName;
or
#synthesize labelAssignmentTitle;
#synthesize labelClassName;
there is no need to do the following:
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
Now if you initialize the right way from the the start you'll find it a lot easier later!
HomeWork.h
#interface HomeWork : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle;
HomeWork.m
#implementation HomeWork
#synthesize assignmentTitle,className;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle {
self = [super init];
if(self){
assignmentTitle = newAssigmentTitle;
className = newClass;
}
return self;
}
#end
ViewController.m
- (IBAction)saveIt:(id)sender {
HomeWork *newHomeWork = [[HomeWork alloc]initWithClassName:[labelClassName text]andAssignmentTitle:[labelAssignmentTitle text]];
}
Because of this, you directly make a newHomeWork object with the parameters given by your two UITextFields.
Now print it out in your logmessage and see what happends ;)