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.'
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.
精彩评论