NSDictionary,这是为什么

发布于 2024-10-01 20:41:35 字数 607 浏览 1 评论 0原文

我正在学习这个行业,目前专注于 NSDictionary。我目前正在做《Beginning iPhone Development Exploring the iPhone SDK》(Dave Mark / jeff LaMarche)一书中的示例。

我正在研究的示例位于第 7 章(第 166 页)“选项卡栏和选择器”中。

我想问他们为什么这样使用词典。

这是场景:

在 .h 文件中:

NSDictionary *stateZips;

.m 文件(viewDidLoad)它们有以下代码:

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.stateZips = 字典;
[词典发布];

我的问题是,是否有任何特定的 Objective-C 原因导致他们将字典复制到 stateZips,而不使用 stateZips 从 plist 进行初始化以避免额外的代码? ...或者这只是作家自己的做事方式?

I am learning this trade and is currently focusing on NSDictionary. I am currently doing examples from the book "Beginning iPhone Development Exploring the iPhone SDK" (Dave Mark / jeff LaMarche).

The example i am working on is in chapter 7 (page 166), Tab Bars and Pickers.

I would like to ask why they use Dictionaries the way they do.

Here is the scenario:

in the .h file:

NSDictionary *stateZips;

The the .m file (viewDidLoad) they have the following code:

NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.stateZips = dictionary;
[dictionary release];

My question is if there is any specific Objective-C reason why they copy the dictionary to stateZips and not use stateZips to init from the plist to avoid the extra code? ...or if this is just the writers own way of doing things?

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

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

发布评论

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

评论(2

偏爱自由 2024-10-08 20:41:35

.m 文件是否也有 stateZips 的 @synthesize 语句?我猜测是这样,因为分配的风格。

如果存在带有保留的属性声明和 stateZips 的 @synthesize 语句,并且您执行此操作:

self.stateZips = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

您将出现内存泄漏。在上面的语句中创建的字典在该语句之后的保留计数将为 2。一项保留用于分配,一项保留由合成设置器执行。

这是使用包含保留语句的 setter 的副作用,但是如果您不使用带有保留语句的 setter,您可能会遇到更大的内存管理问题...;-)

为了最好地理解这些事情,您应该研究使用@property 的分配和保留以及合成的 setter 和 getter 背后的代码。

Does the .m file also have a @synthesize statement for stateZips? I am guessing it has, because of the style of the allocation.

If there is a property declaration with retain, and a @synthesize statement for stateZips and you do this:

self.stateZips = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

You will have a memory leak. The dictionary created in the statement above will have a retain count of 2 after the statement. One retain for the alloc, and one retain performed by the synthesized setter.

This is a side affect of using setters that contain retain statements, but if you do not use setters with retain statements you will probably have a lot bigger memory management problems... ;-)

To best understand these things you should study the use of @property's assign and retain and the code behind the synthesized setters and getters.

━╋う一瞬間旳綻放 2024-10-08 20:41:35

因为我认为 stateZips 是一个保留的属性。
因此,当他们 alloc/initWithContentsOfFile 字典时,他们需要释放它,因为 self.stateZips 已经保留了它。

because I think stateZips is a property which retain.

So when they alloc/initWithContentsOfFile dictionary they need to release it since self.stateZips already retains it.

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