Objective-C / iOS 的术语问题

发布于 2024-10-10 14:42:38 字数 1532 浏览 1 评论 0原文

抱歉,如果这听起来有点菜鸟问题,我对 ObjC 和 iOS 还比较陌生,但对软件开发并不陌生,但是,在阅读有关各种主题的文档/观看视频时,我听到了某些我不熟悉的短语当然,有人可以花点时间对以下术语进行非常简短的描述 - 或者,为我指出一个很好的参考资料。

我将在下面列出我所坚持的术语以及我认为我认为它是什么/意味着什么,以及任何帮助纠正我以便我可以更好地遵循苹果文档流程的帮助将不胜感激。

1) Singleton

可能类似于 PHP 中的完全静态类,没有实例方法

2) 模型、视图、控制器 (MVC) 组织 - 特别是“模型”组件

我知道广泛的定义是保持事物分离,我认为这等同于to 是你的视图将是你构建的以编程方式或在界面生成器中输出到屏幕的内容,控制器将是处理来自视图的消息(点击、点击等)并将信息推送到视图中的代码,尽管在许多情况下形成视图和控制器的.xib 和.h/.m 对属于同一个系列(即MyViewController.h/.m/.xib)。我猜测的模型是用于从源读取和写入数据的单独处理程序,例如,您向其发送消息的类,该类可以从 SQLite 数据库或 XML 提要加载和返回数据并处理它。

3)当谈论对象类型时,* 的正确位置在哪里?

我知道 * 表示指针,但我见过像下面这样写的东西,看起来它们的意思是相同的:

NSString *myVar;
NSString* myVar;
NSString * myVar;

我理解 * 位于消息头中的类型之后,例如:

- (void)myMessageHandler:(NSString *)str;

4)何时使用 NSInteger 与普通 int ?

这可能会让我看起来像个白痴,但我必须问它,因为我不确定何时使用它,而且我看到许多不同的混合示例。

5)可重用实体标识符

当取消队列/创建通过重用队列工作的对象时,我看到每次都使用相同的标识符(例如,对于 UITableViewCell,@“cellIdentifier”)。然而,我看到的是,单元格每次都会使用相关的行数据进行重置,而不是简单地存储在队列中,这种混乱来自 HTML 中的表格,在其中处理每一行,您要么遍历 DOM,要么添加一个 ID 标签每一行都是唯一的。

我认为这是一个内存管理问题,如果您使用默认类,您不会真正注意到发生了什么,但如果您使用不同的自定义类,我认为您的想法是您仅为每个父类实例化每种类型之一实例(例如 UITableView),以便即使您每次重置属性,所需的总内存也是最少的,并且实例化时间从循环中取出,因为您只执行一次?

这个问题的第二部分是,如果我有一个应用程序,其中多个视图控制器相互推送,是否建议使用与特定视图相关的唯一标识符,因此如果我使用自定义子类,标识符将不会'不会导致重叠并且可能导致内存泄漏。

例如,如果我在两个 UITableView 中使用“cellIdenfier”作为我的可重用身份名称,但当我返回顶视图时,第二个使用 UITableViewCell 的自定义类,则 UITableView 会在选择注释时推送一个地图,该地图会推送 UITableView ,系统不会在出队期间返回错误的类型并导致稍微奇怪的输出吗?

Sorry if this sounds like a bit of a noobie question, i'm still relatively new to ObjC and iOS but not new to software development, however, when reading documents / watching videos on various topics, I hear certain phrases that i'm not sure about, could someone take a moment to give a very brief description of the following terms - or, point me to a good reference for them.

I'll list below the terms i'm stuck with and what I think i've figured it is/means and any help in correcting me so I can follow the flow of Apple's documentation better would be heavily appreciated.

1) Singleton

Possibly similar to a wholly static class in PHP, has no instance methods

2) Model, View, Controller (MVC) organisation - specifically the 'model' component

I know the broad definition is keeping things seperate, what I think this equates to is your view would be what you build to output to the screen either programatically or in interface builder, the controller would be the code that handles messages (clicks, taps etc) from the view and pushes information into the view although in many cases the .xib and .h/.m pair which form view and controller are part of the same family (ie, MyViewController.h/.m/.xib). The model in my guesswork is where seperate handlers for reading and writing data to/from a source, eg, a class which you send messages to which can load and return data from an SQLite database or from an XML feed and process it.

3) Where is the right place for the * when talking about object types?

I know the * means a pointer but i've seen things written like the below, looking like they mean the same thing:

NSString *myVar;
NSString* myVar;
NSString * myVar;

I understand the * goes after the type in a message header eg:

- (void)myMessageHandler:(NSString *)str;

4) When to use NSInteger versus just plain int?

This is the one that might make me look an idiot but I have to ask it as i'm not sure when to use which and I see many different examples where it's mixed.

5) Reusable entity identifiers

When de-queing/creating objects that work via the re-use queue, I see the same identical identifier used each time (eg for a UITableViewCell, @"cellIdentifier"). However, what I see is that the cell is reset with the relevant row data each time rather than simply stored on the queue, this confusion comes from tables in HTML where to address each row, you either traverse the DOM or add an ID tag to each row which is unique.

I think this is a memory management thing whereby you wouldn't really notice what's going on if you're using the default class but if you were using different custom classes, I assume the idea is you instantiate one of each type only per parent class instance (eg UITableView) so that even though you reset the properties each time, the total memory required is minimal and the instantiation time is taken out of the loop as you only do it once?

A part 2 to this question would be if I have an app which has multiple view controllers pushed onto each other, would it be advisable to use a unique identifier which relates to a specific view so if I was using custom subclasses, the identifiers wouldn't cause an overlap and probably a memory leak.

eg, UITableView pushes a map which pushes a UITableView when an annotation is selected, if I used "cellIdenfier" as my re-usable identity name in both UITableView's but the second one used a custom class for UITableViewCell, when I return to the top view, would the system not potentially return me the wrong type during the de-queue and cause slightly strange output?

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

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

发布评论

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

评论(2

汐鸠 2024-10-17 14:42:38

尽管您已经接受了答案,但由于它不完整,我将对其进行补充。

就我而言,卡尔的单身定义是关于金钱的。

模型不仅仅是存储数据的对象,它也是业务逻辑。

至于 * 去哪里,是的,这是一个风格问题,但说它是 C 中类型的一部分(Objective-C 是 C 的超集)是非常错误的。这可以通过以下内容轻松证明:

int* foo, bar;

它没有声明两个 int* 类型的变量,而是声明一个指向 int (foo) 和 int (bar) 的指针。因此,从技术上讲,您应该将 * 与变量名称放在一起。但是,我使用与 Karl 相同的约定,并通过从不在逗号分隔列表中声明变量来避免上述问题。

何时使用 NSInteger 而不是仅使用普通 int?

只要 Cocoa API 文档规定您应该使用它,您就应该始终使用它。与普通 int 相比,使用它绝对没有任何优势。如果您需要保证 int 类型的特定宽度,您应该使用 stdint.h 中的 C99 标准宽度(int32_t、int64_t 等)。如果您不需要保证一定的宽度,您也可以使用内置类型,除非 Cocoa API 为您提供 NS(U)Integer。将 NS(U)Integer 转换为(无符号)int 在某些架构(例如 x86_64)上存在截断风险。

可重用的实体标识符

UITableView 需要为屏幕上当前可见的每个单元格提供一个实例。实例化一个单元可能会很昂贵(例如,您可能需要访问磁盘来加载 NIB),因此表视图不会在单元离开视图后将其丢弃,而是将它们放入丢弃单元的队列中。当您需要一个新单元时,从队列中取出未使用的单元比从头开始实例化单元要快得多。

需要重用标识符,因为您的表视图中可能包含不同类型的单元格。例如,一行可能有一个文本字段,另一行可能有一个日期选择器。因此,如果表视图请求文本行的单元格,则为其提供带有日期选择器的单元格是没有用的。

Even though you have accepted an answer, since it is not complete, I'll add to it.

Karl's singleton definition is on the money as far as I'm concerned.

The model is not just the objects storing the data, it's the business logic as well.

As for where the * goes, yes it is a matter of style, but it is subtly incorrect to say it is part of the type in C (of which Objective-C is a superset). This is easily demonstrated with the following:

int* foo, bar;

It does not declare two variables of type int* but a pointer to an int (foo) and an int (bar). So technically you should put the * with the variable name. However, I use the same convention as Karl and avoid the above issue by never declaring variables in comma separated lists.

When to use NSInteger versus just plain int?

You should always use it any time the Cocoa API documents say you should. There is otherwise absolutely no advantage to using it as opposed to a plain int. If you need to guarantee a specific width for an int type you should use the C99 standard ones from stdint.h (int32_t, int64_t etc). If you don't need to guarantee a certain width you might as well use the built in types, except where a Cocoa API gives you an NS(U)Integer. Casting an NS(U)Integer to an (unsigned) int runs the risk of truncating it on some architectures (x86_64 for instance).

Reusable entity identifiers

A UITableView needs one instance for every cell that is currently visible on the screen. Instantiating a cell might be expensive (you might need to go to disk to load a NIB, for instance), so instead of throwing away the cells once they go out of view, the table view will put them in the queue of discarded cells. It's much faster to then pull an unused cell off that queue when you need a new one than it is to instantiate one from scratch.

The reuse identifier is needed because your table view may have cells of different types in it. e.g. one row might have a text field and another row might have a date picker. So if the table view requests a cell for the text row, it would be no good giving it a cell with a date picker.

野鹿林 2024-10-17 14:42:38

正如 middaparka 所说,你可能应该分开问题,但我可以回答 1-3。

单例是一种设计模式,意味着整个程序中只有一个类的实例。它通常使用静态 getInstance 方法来实现,该方法要么返回现有实例(存储为静态变量),要么创建一个新实例(如果不存在)。任何关于设计模式的好的参考都应该解决这个问题。

对于 MVC,您的模型想法本质上是正确的。模型是存储数据的地方。再次,请查看设计模式参考以获取详细信息。

至于 * 去哪里,这很大程度上是风格问题,因为编译器会接受任何方法。我个人更喜欢将它放在类型旁边,因为从技术上讲它是类型的一部分,但我工作过的大多数地方都将它放在标识符旁边。我认为将其放在标识符旁边的理由是 * 的其他用途之一是取消引用指针,在这种情况下,将其与变量分组是有意义的,并且他们希望使其在视觉上保持一致。我只在用作乘法运算符时看到 * 周围的空格。

As middaparka said, you should probably split the questions, but I can answer 1-3.

A singleton is a design pattern meaning there is only one instance of a class in the entire program. It is usually implemented with a static getInstance method that either returns the existing instance (stored as a static variable) or creates a new one if it doesn't exist. Any good reference on design patterns should address it.

For MVC, your idea of a model is essentially correct. The model is where the data is stored. Again, look at a design pattern reference for details.

As for where the * goes, that's largely a matter of style as the compiler will accept any method. I personally prefer to put it next to the type, as it technically is part of the type, but most places I've worked put it next to the identifier. I believe the rationale for putting it next to the identifier is that one of the other uses for * is to dereference a pointer, in which case it makes sense to group it with the variable, and they want to make it visually consistent. I've only seen the spaces around a * when used as a multiplication operator.

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