财产拒绝综合

发布于 2024-08-12 23:01:56 字数 1003 浏览 3 评论 0原文

我已经在这里浏览了截屏视频 学习如何编写基于表格的 iPhone 应用程序,到目前为止进展得相当顺利。目前我已经完成了第三集的一半,并且开始遇到障碍。

为了删除表顶层的临时硬编码,本教程为所有条目及其数据创建一个 NSMutableDicitonary,然后使用forKeys 语句获取一个仅包含要在表格单元格中显示的词条的数组。

我遇到的问题是数组的变量拒绝合成。

有问题的变量在 AppDelegate.h 文件中定义,其余属性如下:

@property (readonly) NSArray *recipes;

然后在 AppDelegate.m 文件中综合并实现,如下所示:

@synthesize recipes;

- (NSArray *)recipes {
    return [data allKeys];
}

I询问了截屏视频的作者,他对 AppDelegate.h 提出了以下建议:

@class Foo :NSObject {
    NSArray *_recipes;
}

@property(nonatomic, retain)NSArray *recipes;

@end

对于 AppDelegate.m

@implementation Foo

@synthesize recipes = _recipes;

@end

我尝试了这种方法,但它产生的错误比以前有过。是什么让这个变量定义与任何其他 @property 不同,我怎样才能让它表现出来?

I've been going through the screencasts here to learn how to write a table-based iPhone application, and it's been going pretty smoothly so far. Presently I'm halfway through the third episode, and it's starting to hit a snag.

In order to remove temporary hard-coding for the top layer of the table, the tutorial creates an NSMutableDicitonary for all the entries and their data, and then creates an NSArray using the forKeys statement to get an array with just the headwords to display in the table cells.

The problem I'm having is that the variable for the array refuses to synthesize.

The offending variable is defined in the AppDelegate.h file with the rest of the properties as follows:

@property (readonly) NSArray *recipes;

It is then synthesized and implemented in the AppDelegate.m file as follows:

@synthesize recipes;

- (NSArray *)recipes {
    return [data allKeys];
}

I inquired with the author of the screencast, and he suggested the following for AppDelegate.h:

@class Foo :NSObject {
    NSArray *_recipes;
}

@property(nonatomic, retain)NSArray *recipes;

@end

And this for AppDelegate.m:

@implementation Foo

@synthesize recipes = _recipes;

@end

I tried this method, but it created more errors than there were before. What makes this variable definition different from any other @property, and how can I make it behave?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

提笔落墨 2024-08-19 23:01:56

@property “仅仅”是方法声明的简写。 @dynamic 意味着您将在运行时动态提供实现,这是一种非典型的高级使用模式。

因此:

@property (readonly) NSArray *recipes;

在标头中,Is 的简写形式为:

- (NSArray *) recipes;

@synthesize Recipes; 将合成 @property 声明隐含的方法。不多也不少。既然有人提到过,@synthesize Recipes=_recipes;将合成方法,但使用实例变量_recipes进行存储(而不是recipes) 。

如果您实现自己的 getter(因为这是只读的,因此只有一个 getter),其形式为:

- (NSArray *) recipes {
    return ....;
}

那么就不需要 @synthesize@dynamic

@property is "merely" a shorthand for method declarations. @dynamic means that you'll dynamically provide the implementations at runtime, an advanced use pattern that is atypical.

Thus:

@property (readonly) NSArray *recipes;

Is, in the header, shorthand for:

- (NSArray *) recipes;

@synthesize recipes; will synthesize the methods implied by the @property declaration. No more, no less. Since someone mentioned it, @synthesize recipes=_recipes; will synthesize the methods, but use the instance variable _recipes for storage (and not recipes).

If you implement your own getter (since this is readonly, there is only a getter), of the form:

- (NSArray *) recipes {
    return ....;
}

Then there is no need for either @synthesize or @dynamic.

您的好友蓝忘机已上羡 2024-08-19 23:01:56

@synthesize 生成一个简单的方法来访问该属性。由于您的访问器方法比较复杂,因此无法使用 @synthesize 生成。

- (NSArray *)recipes {
    return [data allKeys];
}

@synthesize generates a simple method to access the property. Since your accessor method is more complex, it can't be generated with @synthesize.

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