"Expected a type" error pointing to the return type of a method - objective-c

I've attempted to compile, but every time I do, one method throws a strange "expected a type" error. I have a method in the header:
-(ANObject *)generateSomethingForSomethingElse:(NSString *)somethingElse;
The error points at the return type for this method. I've imported ANObject into the header using #import "ANObject.h" and ANObject is compiling fine..
Why is this happening?

This is to do with the order that the source files are compiled in. You are already probably aware that you can't call a method before it is defined (see below pseudocode):
var value = someMethod();
function someMethod()
{
...
}
This would cause a compile-time error because someMethod() has not yet been defined. The same is true of classes. Classes are compiled one after the other by the compiler.
So, if you imagine all the classes being put into a giant file before compilation, you might be able to already see the issue. Let's look at the Ship and BoatYard class:
#interface BoatYard : NSObject
#property (nonatomic, retain) Ship* currentShip;
#end
#interface Ship : NSObject
#property (nonatomic, retain) NSString* name;
#property (nonatomic, assign) float weight;
#end
Once again, because the Ship class has not yet been defined, we can't refer to it yet. Solving this particular problem is pretty simple; change the compilation order and compile. I'm sure you're familliar with this screen in XCode:
But are you aware that you can drag the files up and down in the list? This changes the order that the files will be compiled in. Therefore, just move the Ship class above the BoatYard class, and all is good.
But, what if you don't want to do that, or more importantly, what if there is a circular relationship between the two objects? Let's increase the complexity of that object diagram by adding a reference to the current BoatYard that the Ship is in:
#interface BoatYard : NSObject
#property (nonatomic, retain) Ship* currentShip;
#end
#interface Ship : NSObject
#property (nonatomic, retain) BoatYard* currentBoatYard;
#property (nonatomic, retain) NSString* name;
#property (nonatomic, assign) float weight;
#end
Oh dear, now we have a problem. These two can't be compiled side-by-side. We need a way to inform the compiler that the Ship* class really does exist. And this is why the #class keyword is so handy.
To put it in layman's terms, you're saying, "Trust me man, Ship really does exist, and you'll see it really soon". To put it all together:
#class Ship;
#interface BoatYard : NSObject
#property (nonatomic, retain) Ship* currentShip;
#end
#interface Ship : NSObject
#property (nonatomic, retain) BoatYard* currentBoatYard;
#property (nonatomic, retain) NSString* name;
#property (nonatomic, assign) float weight;
#end
Now the compiler knows as it compiles BoatYard, that a Ship class definition will soon appear. Of course, if it doesn't, the compilation will still succeed.
All the #class keyword does however is inform the compiler that the class will soon come along. It is not a replacement for #import. You still must import the header file, or you will not have access to any of the class internals:
#class Ship
-(void) example
{
Ship* newShip = [[Ship alloc] init];
}
This cannot work, and will fail with an error message saying that Ship is a forward declaration. Once you #import "Ship.h", then you will be able to create the instance of the object.

I found this error hapenning when there is circular dependency on the headers. Check if the .h file where you declare this method is imported in ANObject.h

You basically add
#class ANObject;
before #interface!

So, for some reason I was getting this error while trying to set a method with an enum type in the parameters. Like so:
- (void)foo:(MyEnumVariable)enumVariable;
I had previously used it like this and never had an issue but now I did. I checked for circular dependency and could find none. I also checked for typos multiple times and no dice. What ended up solving my issue was to adding 'enum' before I wanted to access the variable. Like so:
- (void)foo:(enum MyEnumVariable)enumVariable;
{
enum MyEnumVariable anotherEnumVariable;
}

Usually when I see an error like this it's because I have a typo on a previous line, such as an extra or missing parenthesis or something.

It may sound stupid, but wrong shelling or wrong use of uppercase/lowercase letterwrong case this.

I got this message, when the variable type was misspelled. See below this below
e.g.
-(void)takeSimulatorSafePhotoWithPopoverFrame:(GCRect)popoverFrame {
instead of.....
-(void)takeSimulatorSafePhotoWithPopoverFrame:(CGRect)popoverFrame {

Strangely enough, changing the order of my imports has fixed this in the past... Try moving the import to the bottom after all your other imports.

I solved it by adding #class class_name to the .h file

Related

RLMArray properties in unmanaged RLMObjects in Objective-C

I cannot find a good example code of this anywhere....but the information I find is contradictory and confusing...
#interface DAORealmMetadata : RLMObject
#property (nonatomic, copy) NSString* id;
#end
RLM_ARRAY_TYPE(DAORealmMetadata)
#interface DAORealmBase : RLMObject
#property (nonatomic, copy) NSString* id;
#property (nonatomic, copy) RLMArray<DAORealmMetadata*><DAORealmMetadata>* metadata;
#end
RLM_ARRAY_TYPE(DAORealmBase)
Question:
Am I supposed to add #dynamic metadata in the DAORealmBase implementation...or not?
I've tried it with and without and have the same end result...a crash.
I create the unmanaged object with this code:
DAORealmBase* baseObj = [[DAORealmBase alloc] init];
DAORealmMetadata* metadataObj = [[DAORealmMetadata alloc] init];
[baseObj.metadata addObject:metadataObj];
Question:
Why does the last line cause a crash/exception?
I can only assume that I"m doing something wrong, but I cannot find any specifics as to what I did.
Thanks!
Well, I tracked the problem down, and through some trial and error, determined that the problem was the property attributes on the RLMArray properties.
Changing
#property (nonatomic, copy) RLMArray<DAORealmMetadata*><DAORealmMetadata>* metadata;
to
#property RLMArray<DAORealmMetadata*><DAORealmMetadata>* metadata;
seems to have resolved the problem. I believe specifically the 'copy' attribute.
Now, I know that the Realm docs say that the attributes are ignored and not needed, but the lint checker I'm using wants them there...and since they are "ignored", what's the harm?
Well, they are ignored on normal Realm properties, but on the RLMArray properties they aren't ignored, and problems ensue.
Hopefully this will help someone else in the future and save them some time.

Unknown Type Name in Xcode (even with #class declaration)

I'm having trouble with a simple app setting up a data controller. I get an error on the line #property (strong, nonatomic) BirdsListDataController *dataController; in BirdsListViewController.h. I've tried my best to use a #class declaration of BirdsListDataController, as well as trying to remove any #import statements from the .h files and tried to remove a circular #import which you can find commented out in the top of BirdsListViewController.h. I'm guessing it's something simple.
BirdsListViewController.h
#import <UIKit/UIKit.h>
#class BirdsListDataController;
#interface BirdsListViewController : UITableViewController <UITextFieldDelegate>
{
// NSMutableArray *listOfBirds;
IBOutlet UITextField *addNewBirdTextField;
}
//#property (nonatomic, retain) NSIndexPath *checkedIndexPath;
#property (nonatomic, retain) NSString *textLabelContents;
#property (nonatomic, retain) NSMutableArray *workingArray;
#property (strong, nonatomic) BirdsListDataController *dataController;
#property (strong, nonatomic) IBOutlet UITableView *birdListTableView;
#end
BirdsListViewController.m
#import "BirdsListViewController.h"
#import "BirdsListDataController.h"
#interface BirdsListViewController ()
#end
#implementation BirdsListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
...
BirdsListDataController.h
#import <Foundation/Foundation.h>
#class BirdName;
#interface BirdsListDataController : NSObject
#property (nonatomic, copy) NSMutableArray *listOfBirds;
-(NSUInteger)countOfList;
-(BirdName *)objectInListAtIndex:(NSUInteger)theIndex;
-(void)addBirdNameWithName:(BirdName *)bName;
#end
BirdsListDataController.m
#import "BirdsListDataController.h"
//#import "BirdsListViewController.h"
#import "Bird.h"
#implementation BirdsListDataController
-(id)init
{...
I'm still really new to iOS and Objective C, so hopefully my code isn't too awful to troubleshoot. Thanks for the help.
For people looking for a better answer than comment/uncomment your code, a better solution is to clean your project and to delete your derived data. Once you've fixed your circular references, the keystroke Command+Shift+K will clean your project, or you can go to and select Product->Clean.
To delete your derived data, open Organizer, click on the Projects tab, navigate to your project in the sidebar. You should see "Derived Data" under the project name header. To the right of that should be a button saying delete. If it is enabled, deleting the derived data can also remove hanging errors.
As way of explanation, it seems sometimes that Xcode becomes out of sync with a project, holding on to errors that no longer exists. This is better in more recent version, but still happens occasionally.
I'm not certain what is causing your problem, but a few things:
In the code that you've presented there is no reason not to import BirdListDataController.h in BirdListViewController.h, since there is no reference to BirdListViewControllers in BirdListDataController.h. So try replacing your #class declaration with an #import statement.
In BirdListDataController.h you declare #class BirdName, but in BirdListDataController.m you import Bird.h instead of BirdName.h. It seems like something could be wrong there, although I would have to see the code for BirdName.h and Bird.h to know for sure.
In my case I had duplicate class names in different folders structure, Once I removed the new class and named it differently everything worked again.
So to translate this into a practical solution, as per "shA.t"'s comment:
if you comment/uncomment your code, or clean project as above
answers suggested but still doesnt solve it:
take a step to look back at recent classes changes and double check
all class names are unique even if in different directories, double
check
them all
if duplicate class name found: make a backup of that
code, delete that class (not just reference, but to trash too)
create a new class with unique name and incorporate the backed up
code
For this particular duplicate class name scenario, this will save you the hassle of importing and commenting your #import "class.h"

Difference in setting up a class?

I may not have worded the question right, but I am not sure if what I am asking makes 100% so here goes:-)
In Xcode you can set a #class (name of class) above the #interface in the header file.
Is this the same as changing the the UIViewController in the name of the class? See code below:
So is this the same -
#class CoreDataClass;
#interface FlipsideViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
}
//This file declares the UITableView
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) CoreDataClass *cdc;
As this:
#interface FlipsideViewController : CoreDataClass <UITableViewDataSource, UITableViewDelegate>
{
}
//This file declares the UITableView
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#end
??
If this is not the same, how is it different and what are advantages to the different implementation?
The Difference is only really asked if they are similar:-)
#class is not used to create a class, but to forward declare another one. See this question for a good explanation.
They are not the same at all. The first case is a 'forward declaration' - you are telling the compiler that the class CoreDataClass exists, so that you can refer to it in your header file without actually importing the files that define it.
The second case, you are declaring that FlipsideViewController is a subclass of CoreDataClass, and inherits all its methods and instance variables.
They're not even related. The difference is that the superclass ("parent" class) of your view controller will be different (and this can lead to nice unrecognized selector errors...). Forward-declaring a class using the #class keyword is just a convenient way of referring to a class when one doesn't want to import a whole framework header hierarch just in order to refer to one class. I. e., if you don't need to know anyting about a class except that it exists, you can use this keyword. Be careful, however, if you maks heavy use of the class - in those cases, the class forward-declaration is not considered a good solution.
In first case when you use #class it's inform XCode that you will be using CoreDataClass somewhere and you will #import header for example in .m file, in second case you're inherit from CoreDataClass (you will get access to all public and protected properties)

XCode: Unrecognized selector sent to instance

I am getting the following error:
"-[Order items]: unrecognized selector sent to instance 0x6b5f240"
I do have a class called Order, which looks like this:
Order.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#class OrderItem;
#interface Order : NSManagedObject {
#private
}
#property (nonatomic, retain) NSNumber * orderID;
#property (nonatomic, retain) NSDate * date;
#property (nonatomic, retain) NSNumber * orderCode;
#property (nonatomic, retain) NSSet* items;
#end
Order.m
#import "Order.h"
#import "OrderItem.h"
#implementation Order
#dynamic orderID;
#dynamic date;
#dynamic orderCode;
#dynamic items;
...
It doesn't extend any sort of class which has an "items" method, if I'm reading that correctly?
Is there any other reason I would be getting such an error. To add to the madness, this project is copied directly from a previous project, with some minor edits. I've done text comparisons on every single class in both projects and there are no differences other than the cosmetic changes I've made.
#dynamic items tells the compiler that you will be providing the methods for items.
Since this was working in a previous project, it must have had the following method somewhere in the .m file:
- (NSSet *)items {
// Appropriate code
}
If you do not want to provide your own custom getter like this, then change #dynamic items to #synthesize items and the compiler will generate one for you.
For more details, see the Declared Properties section of The Objective-C Programming Language provided by Apple here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html
EDIT
While everything above still applies to a normal object (and may still apply here), I just noticed that this is a subclass of NSManagedObject.
In your old data model there was probably a relationship called items and therefore the appropriate methods were provided by NSManagedObject and #dynamic was appropriate to prevent compiler warnings.
If in your new data model there is no relationship named items, then the methods will not be generated and it will cause the problem that you are getting here.

variable accessing

I have a variable x in one class.And I want to access the updated x value in some other class.
There is so much of confusion.Can I use property?.Please help me.
Thanks in advance
Do you mean that you want to be told when the value changes? Have a look at Key Value Observing
To simply access an iVar in one class from another, a property is exactly what you want.
The syntax is :
in your .h
#interface myclass : NSObject {
UIWindow *window;
}
#property (nonatomic, retain) UIWindow *window;
#end
in your .m
#implementation myclass
#synthesize window;
...
#end
The #synthesize directive instructs the compiler to produce a lot of boilerplate code (as directed by the (nonatomic, retain) specifiers. In this case to handle thread safety and memory management.
Also note that in Objective-C 2.0 the iVar declaration UIWindow *window; is not required.
If you want to be notified in your second class when an iVar is updated, you need to look at key value observing. Unless you are writing a framework or some very dynamic code, that is probably overkill.
Maybe this tutorial will help you out..
If this is not what you mean, please rephrase the question, because i don't understand it..
Edit: Or a shared Instance can be used
you could access it by #import Classname, and then just use the getter that is created with the property. but first initialize the class you have imported..
#import "ClassY.h"
#implementation ClassX
ClassY * classY;
NSString * name;
...
name = [classY name];
...
#end