Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve this error? "property with 'retain(or strong)' attribute must be of object type"

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
like image 897
Rob Avatar asked Jan 11 '12 23:01

Rob


4 Answers

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
like image 92
Linghua Zhang Avatar answered Oct 20 '22 04:10

Linghua Zhang


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;
like image 26
Rajneesh071 Avatar answered Oct 20 '22 03:10

Rajneesh071


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.

like image 6
PengOne Avatar answered Oct 20 '22 05:10

PengOne


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

I removed those files and my project started working.

like image 1
Pratik Gujarati Avatar answered Oct 20 '22 05:10

Pratik Gujarati