开发者

How to synthesize members from another class

开发者 https://www.devze.com 2023-01-14 17:07 出处:网络
I have created a certain class to represent an object. My program has several views and view controllers. I want the different view controllers to work with this object. However, when I try to synthes

I have created a certain class to represent an object. My program has several views and view controllers. I want the different view controllers to work with this object. However, when I try to synthesize the class members so that they can be used in the view controller, I get an error saying that there is no declaration of the property found in the interface, even after I have included the .h file. How can I synthesize member开发者_StackOverflows from another class in the various view controllers that will be working with them?


Without seeing your code or the specific compiler errors, it's hard to say what might be going on.

If you're just setting up a simple model object, then you should be able to write something like this in Model.h:

@interface Model : NSObject {}

@property (nonatomic, retain) NSString *name;

@end

and in your Model.m file:

@implementation Model

@synthesize name;

@end

All you need to do at that point is #import "Model.h" into your other source files and then you can use the name property like so:

Model *m = [[Model alloc] init];
m.name = @"Bob";
...

It sounds like the compiler is complaining about the lack of the @property declaration in your case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号