ios 如何解决这个错误?“带有‘retain(or strong)’属性的属性必须是对象类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8828398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:15:02  来源:igfitidea点击:

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

iphoneobjective-ciosios4

提问by Rob

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:

我整天都在开发一个应用程序,它一直运行良好,直到 Xcode 下载库或其他东西,然后它开始出现问题。我只是想创建 getter/setter 方法来从我的 APPDelegate 中获取几个数组。就像我说它工作正常,然后随机出现此错误,现在不再构建:

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:

这是 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

这是 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

回答by Linghua Zhang

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

尝试将@class Project 和@class TeamMember 添加到您的 Task.h 中,如下所示

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

@class TeamMember;
@class Project;

@interface Task : NSObject{
    NSDate *endDate;
    NSDate *startDate;
    ...
}
@end

回答by Rajneesh071

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

您正在尝试保留不是 NSObject 子类的内容。通常当有人试图保留浮点数或整数时会发生这种情况。

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.

NSInteger 是一个标量而不是一个对象。所以你不应该保留它,它应该被分配。更改您的属性将清除警告消息。您不需要执行您添加的 NSNumber 内容。

@property (nonatomic, assign) NSInteger integValue;

回答by PengOne

The error is that you are attempting to retain something that is not an NSObjectsubclass. Usually this happens when someone tries to retaina floator int.

错误是您试图保留不是NSObject子类的东西。通常当有人尝试retainafloat或时会发生这种情况int

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

您已显示 .h forProject但未显示for TeamMember。为此检查后者,如果您没有看到它,请更新您的代码片段。

回答by Pratik Gujarati

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

它发生在我身上,因为我递归地导入了一些 .h 文件。

I removed those files and my project started working.

我删除了这些文件,我的项目开始工作。

回答by Chandan

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

答案已经给出但在我的情况下,问题是不同的。

I resolved it in a different way. In .hfile Foundation frameworkis added by default. So i commentedthe line and import UIKit framework. Now i am able to run my code.

我以不同的方式解决了它。默认添加在.h文件Foundation framework中。所以我commented线和进口UIKit framework。现在我可以运行我的代码了。

//#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

回答by Mujahed Ansari

Don't Write import statement . Write @class.

不要写导入语句。写@class。

Example :- @class SecondViewController.

示例:-@class SecondViewController。

Thanks

谢谢