自定义类的 getter 和 setter

发布于 2024-09-24 02:08:37 字数 1011 浏览 4 评论 0原文

如果您合成一个自定义类,是否会为其创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2024-10-01 02:08:37

仅当您将所需的实例变量声明为属性,然后 synthesize propname; 时,才会创建 getter 和 setter。现在,getter 和 setter 中使用什么样的代码取决于您定义的属性属性(nonatomic/atomicassign保留复制

编辑OP的修订问题:是的,将为DetailViewController的myObject实例变量创建一个getter/setterclass

DetailViewController.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 the DetailViewController class

DetailViewController.myObject = [theArray objectAtIndex:indexPath.row];

You are attempting to set a class variable that isn't defined. DetailViewController is of type Class, not DetailViewController. Perform the same operation on an instance of DetailViewController and you should be all set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文