您在编写 Objective-C 和 Cocoa 时使用的最佳实践是什么? [英] What are best practices that you use when writing Objective-C and Cocoa?

查看:28
本文介绍了您在编写 Objective-C 和 Cocoa 时使用的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 HIG(这很方便!),但是您在编写 Objective-C 时使用了哪些编程实践,更具体地说,在使用 Cocoa(或 CocoaTouch)时.

I know about the HIG (which is quite handy!), but what programming practices do you use when writing Objective-C, and more specifically when using Cocoa (or CocoaTouch).

推荐答案

我已经开始做一些我认为不标准的事情:

There are a few things I have started to do that I do not think are standard:

1) 随着属性的出现,我不再使用_"作为私有"类变量的前缀.毕竟,如果一个变量可以被其他类访问,不应该有它的属性吗?我一直不喜欢_"前缀让代码更丑,现在我可以省略它了.

1) With the advent of properties, I no longer use "_" to prefix "private" class variables. After all, if a variable can be accessed by other classes shouldn't there be a property for it? I always disliked the "_" prefix for making code uglier, and now I can leave it out.

2) 说到私有的东西,我更喜欢将私有方法定义放在 .m 文件中的类扩展名中,如下所示:

2) Speaking of private things, I prefer to place private method definitions within the .m file in a class extension like so:

#import "MyClass.h"

@interface MyClass ()
- (void) someMethod;
- (void) someOtherMethod;
@end

@implementation MyClass

为什么将 .h 文件与外人不应该关心的东西混在一起?空 () 适用于 .m 文件中的私有类别,如果您没有实现声明的方法,则会发出编译警告.

Why clutter up the .h file with things outsiders should not care about? The empty () works for private categories in the .m file, and issues compile warnings if you do not implement the methods declared.

3) 我已经将 dealloc 放在 .m 文件的顶部,就在 @synthesize 指令的下方.你在课堂上想考虑的事情列表的顶部不应该是你 dealloc 的内容吗?在 iPhone 这样的环境中尤其如此.

3) I have taken to putting dealloc at the top of the .m file, just below the @synthesize directives. Shouldn't what you dealloc be at the top of the list of things you want to think about in a class? That is especially true in an environment like the iPhone.

3.5) 在表格单元格中,使每个元素(包括单元格本身)不透明以提高性能.这意味着在所有内容中设置适当的背景颜色.

3.5) In table cells, make every element (including the cell itself) opaque for performance. That means setting the appropriate background color in everything.

3.6) 当使用 NSURLConnection 时,作为一项规则,您可能希望实现委托方法:

3.6) When using an NSURLConnection, as a rule you may well want to implement the delegate method:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
      return nil;
}

我发现大多数 Web 调用都非常单一,这比您希望缓存响应的规则更例外,尤其是对于 Web 服务调用.实现如图所示的方法会禁用响应缓存.

I find most web calls are very singular and it's more the exception than the rule you'll be wanting responses cached, especially for web service calls. Implementing the method as shown disables caching of responses.

同样令人感兴趣的是,约瑟夫·马蒂洛 (Joseph Mattiello) 提供了一些很好的 iPhone 特定技巧(在 iPhone 邮件列表中收到).还有更多,但这些是我认为最有用的(请注意,现在对原始内容进行了少量编辑,以包含回复中提供的详细信息):

Also of interest, are some good iPhone specific tips from Joseph Mattiello (received in an iPhone mailing list). There are more, but these were the most generally useful I thought (note that a few bits have now been slightly edited from the original to include details offered in responses):

4) 仅在必要时才使用双精度,例如在使用 CoreLocation 时.确保以 'f' 结束常量,以便 gcc 将它们存储为浮点数.

4) Only use double precision if you have to, such as when working with CoreLocation. Make sure you end your constants in 'f' to make gcc store them as floats.

float val = someFloat * 2.2f;

someFloat 实际上可能是双精度时,这非常重要,您不需要混合模式数学,因为您在存储中失去了 'val' 的精度.虽然 iPhone 上的硬件支持浮点数,但与单精度相比,执行双精度算术可能仍需要更多时间.参考资料:

This is mostly important when someFloat may actually be a double, you don't need the mixed-mode math, since you're losing precision in 'val' on storage. While floating-point numbers are supported in hardware on iPhones, it may still take more time to do double-precision arithmetic as opposed to single precision. References:

在较旧的手机上,计算应该以相同的速度运行,但寄存器中的单精度组件可以多于双精度组件,因此对于许多计算,单精度最终会更快.

On the older phones supposedly calculations operate at the same speed but you can have more single precision components in registers than doubles, so for many calculations single precision will end up being faster.

5) 将您的属性设置为 nonatomic.默认情况下,它们是 atomic 并且在合成时,将创建信号量代码以防止多线程问题.99% 的人可能不需要担心这个,当设置为 nonatomic 时,代码不会那么臃肿并且内存效率更高.

5) Set your properties as nonatomic. They're atomic by default and upon synthesis, semaphore code will be created to prevent multi-threading problems. 99% of you probably don't need to worry about this and the code is much less bloated and more memory-efficient when set to nonatomic.

6) SQLite 可以是一种非常非常快的缓存大型数据集的方法.例如,地图应用程序可以将其切片缓存到 SQLite 文件中.最昂贵的部分是磁盘 I/O.通过在大块之间发送 BEGIN;COMMIT; 来避免许多小的写入.我们使用一个 2 秒的计时器,例如在每次新提交时重置.当它到期时,我们发送 COMMIT;,这会导致您的所有写入都集中在一个大块中.SQLite 将事务数据存储到磁盘,执行此开始/结束包装可避免创建多个事务文件,将所有事务分组到一个文件中.

6) SQLite can be a very, very fast way to cache large data sets. A map application for instance can cache its tiles into SQLite files. The most expensive part is disk I/O. Avoid many small writes by sending BEGIN; and COMMIT; between large blocks. We use a 2 second timer for instance that resets on each new submit. When it expires, we send COMMIT; , which causes all your writes to go in one large chunk. SQLite stores transaction data to disk and doing this Begin/End wrapping avoids creation of many transaction files, grouping all of the transactions into one file.

此外,如果 SQL 在您的主线程上,它会阻塞您的 GUI.如果您有一个很长的查询,最好将您的查询存储为静态对象,并在单独的线程上运行您的 SQL.确保在 @synchronize() {} 块中包装任何修改数据库的查询字符串.对于简短的查询,为了更方便,只需将内容留在主线程中即可.

Also, SQL will block your GUI if it's on your main thread. If you have a very long query, It's a good idea to store your queries as static objects, and run your SQL on a separate thread. Make sure to wrap anything that modifies the database for query strings in @synchronize() {} blocks. For short queries just leave things on the main thread for easier convenience.

这里有更多SQLite优化技巧,虽然文档看起来已经过时了,但很多点可能还是不错的;

More SQLite optimization tips are here, though the document appears out of date many of the points are probably still good;

http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html

这篇关于您在编写 Objective-C 和 Cocoa 时使用的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆