自定义类的 getter 和 setter
如果您合成一个自定义类,是否会为其创建 getter 和 setter?
这是我创建的自定义类。
// MyClass.h
#import <Foundation/Foundation.h>
@interface MyClass : NSObject <NSCoding> {
NSString *string1;
NSString *string2;
}
@property (nonatomic, retain) NSString *string1;
@property (nonatomic, retain) NSString *string2;
@end
在这里,我将该类的一个对象声明为属性
// DetailViewController.h
#import <UIKit/UIKit.h>
#import "MyClass.h"
@interface DetailViewController : UIViewController {
MyClass *myObject;
}
@property(nonatomic, retain) MyClass *myObject;
@end
。在这里,我合成了该对象。
#import "DetailViewController.h"
#import "MyClass.h"
@implementation DetailViewController
@synthesize myObject;
那么它有 getter 和 setter 吗?
当我尝试在 RootViewController.m 中运行此代码时,
DetailViewController.myObject = [theArray objectAtIndex:indexPath.row];
出现错误“访问未知的 'setMyObject:' 类方法。无法设置对象 - 只读属性或找不到设置器。”
If you synthesize a custom class, do getters and setters get created for it?
This is the custom class I created.
// MyClass.h
#import <Foundation/Foundation.h>
@interface MyClass : NSObject <NSCoding> {
NSString *string1;
NSString *string2;
}
@property (nonatomic, retain) NSString *string1;
@property (nonatomic, retain) NSString *string2;
@end
Here I declare an object of that class as a property
// DetailViewController.h
#import <UIKit/UIKit.h>
#import "MyClass.h"
@interface DetailViewController : UIViewController {
MyClass *myObject;
}
@property(nonatomic, retain) MyClass *myObject;
@end
Here I synthesize the object.
#import "DetailViewController.h"
#import "MyClass.h"
@implementation DetailViewController
@synthesize myObject;
So does it have getters and setters?
When I try to run this code inside RootViewController.m
DetailViewController.myObject = [theArray objectAtIndex:indexPath.row];
I get an error saying "Accessing unkown 'setMyObject:' class method. Object cannot be set - either readonly property or no setter found.'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您将所需的实例变量声明为属性,然后
synthesize propname;
时,才会创建 getter 和 setter。现在,getter 和 setter 中使用什么样的代码取决于您定义的属性属性(nonatomic
/atomic
、assign
、保留
,复制
)编辑OP的修订问题:是的,将为
DetailViewController的
classmyObject
实例变量创建一个getter/setterDetailViewController.myObject = [theArray objectAtIndex:indexPath.row];
您正在尝试设置未定义的类变量。
DetailViewController
的类型为Class
,而不是DetailViewController
。对DetailViewController
的实例执行相同的操作,您应该已准备就绪。Only if you declare the desired instance variables as properties, then
synthesize propname;
, will getters and setters be created. Now, what kind of code goes into the getters and setters depends on what property attributes you define (nonatomic
/atomic
,assign
,retain
,copy
)EDIT to OP's revised question: Yes a getter/setter will be created for the
myObject
instance variable of theDetailViewController
classDetailViewController.myObject = [theArray objectAtIndex:indexPath.row];
You are attempting to set a class variable that isn't defined.
DetailViewController
is of typeClass
, notDetailViewController
. Perform the same operation on an instance ofDetailViewController
and you should be all set.