39

I have been working on an app all day which has been working fine until Xcode downloaded libraries or something and then it started having issues. I am just trying to create the getter/setter methods to get a couple of arrays out of my APPDelegate. Like I said it was working fine and then randomly it showed up with this error and now won't build anymore:

property with 'retain(or strong)' attribute must be of object type

Here is the rest of the code:

#import <Foundation/Foundation.h>
#import "Project.h"
#import "TeamMember.h"


@interface Task : NSObject{

NSDate *endDate;
NSDate *startDate;
NSMutableString* notes;
NSMutableString* taskName;

//The error appears first right here over teamMember
TeamMember *teamMember;
Project *project;

}
//The error appears over both of the following lines as well...
@property  (nonatomic, retain)TeamMember *teamMember;
@property  (nonatomic, retain) Project * project;

@property (nonatomic, retain) NSMutableString *notes;
@property (nonatomic, retain) NSMutableString *taskName;
@property (nonatomic, retain) NSDate *startDate;
@property (nonatomic, retain) NSDate *endDate;


@end

Any ideas? This has got me stumped....

Here is Project.h:

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

@interface Project : NSObject{

NSDate *dueDate;
NSDate *startDate;
NSArray *tasksInProject;
NSMutableString* notes;
NSMutableString* description;
NSMutableString* projectName; 

}
@property (nonatomic, retain) NSDate *startDate;
@property (nonatomic, retain) NSDate *dueDate;
@property (nonatomic, retain) NSArray *tasksInProject;
@property (nonatomic, retain) NSMutableString *description;
@property (nonatomic, retain) NSMutableString *projectName;
@end

Here is TeamMember.h

#import <Foundation/Foundation.h>
#import "Task.h"
#import "Project.h"

@interface TeamMember : NSObject{

NSMutableArray *projects;
NSMutableString *name;
NSMutableString *title;
NSMutableString *email;
NSMutableString *phone;
NSMutableString *notes;
}

//@property(nonatomic, retain) NSArray *projects;
@property (nonatomic, retain) NSMutableString *name;
@property (nonatomic, retain) NSMutableString *title;
@property (nonatomic, retain) NSMutableString *email;
@property (nonatomic, retain) NSMutableString *phone;
@property (nonatomic, retain) NSMutableString *notes;
@end
PengOne
  • 48,188
  • 17
  • 130
  • 149
Rob
  • 2,319
  • 4
  • 20
  • 18

7 Answers7

64

It looks like it's caused by recursively including of header files.

Try to add @class Project and @class TeamMember into your Task.h, like this

#import <Foundation/Foundation.h>
#import "Project.h"
#import "TeamMember.h"

@class TeamMember;
@class Project;

@interface Task : NSObject{
    NSDate *endDate;
    NSDate *startDate;
    ...
}
@end
Linghua Zhang
  • 996
  • 7
  • 4
  • 6
    +1 but you should also explain that the `#import`s for Project.h and TeamMember.h should be moved to Task.m, now that the forward declarations are in place - the circular dependency has not been broken yet! – justin Jan 12 '12 at 08:34
  • so just to clarify this, I have a similar project but I dont see myself doing a circular dependency but if i do add class it goes away (i suppose cuz im telling it it IS a class) should i just add the header of this class into the M file and just leave it as a property with the @class on the H file? – Pochi Aug 21 '12 at 00:18
43

You are attempting to retain something that is not an NSObject subclass. Usually this happens when someone tries to retain a float or int.

NSInteger is a scalar and not an object. So you shouldn't retain it, it should be assigned. Changing your property will clear up the warning message. You don't need to do the NSNumber stuff that you added in.

@property (nonatomic, assign) NSInteger integValue;
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
6

The error is that you are attempting to retain something that is not an NSObject subclass. Usually this happens when someone tries to retain a float or int.

You've shown the .h for Project but not for TeamMember. Check the latter for this, and if you don't see it then update your code snippet.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • I looked and they both inherit from Object...I edited the code to include both. I just noticed that it is also saying "unknown type name TeamMember as an error... – Rob Jan 12 '12 at 00:15
1

It happened to me because i have recursively imported some .h files.

I removed those files and my project started working.

Pratik Gujarati
  • 302
  • 2
  • 3
1

Answer is already given But in my case the issue was different.

I resolved it in a different way. In .h file Foundation framework is added by default. So i commented the line and import UIKit framework. Now i am able to run my code.

//#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
Chandan kumar
  • 1,074
  • 12
  • 31
0

Don't Write import statement . Write @class.

Example :- @class SecondViewController.

Thanks

Mujahed Ansari
  • 419
  • 4
  • 13
0

In case we custom a View, to use IBOutlet in ViewController we simply import header of custom view like this in our ViewController

#import "abc.h"

this works for sure

dotrinh PM
  • 903
  • 1
  • 7
  • 19