Tag: 编码风格

_iVar与iVar_进行variables命名

我曾经使用前缀下划线作为实例variables的命名,以区别于局部variables。 我碰巧看到“Google Objective-C风格指南” ,发现它build议使用尾部下划线(请参考这里 ),但没有详细解释原因。 我知道这是一个编码风格的问题,但我不知道使用尾部下划线有什么好处吗?

在通用应用程序上分离iPhone和iPad类有什么优势吗?

我有一个通用(为iPhone和iPad)应用程序。 在文件夹结构中分离iPad和iPhone类有什么优势吗? 这里是我的意思的一个例子: – MyApp – Resources – Classes – iPad – SomeUniqueClassOnIPad.h – SomeUniqueClassOnIPad.m – iPhone – SomeUniqueClassOnIPhone.h – SomeUniqueClassOnIPhone.m – SomeUniversalClass.h – SomeUniversalClass.m 这在Objective-C项目中很普遍吗?

使用Setter和Getter方法与直接操作的好处

对不起,如果这是一个初学者的问题,但我想知道什么好处是使用setter和getter方法,而不是直接直接操纵它们。 我在obj-c,我想知道在内存/ CPU使用方面是否有任何好处。 例如,我在上传图像之前裁剪一张图像,然后在拍摄/拾取后裁切。 我把所有的代码放在didFinishPickingMediaWithInfo方法中。 所以它看起来像下面这样: -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ //Create image imageFile = [info objectForKey:UIImagePickerControllerOriginalImage]; //Unhide imageView and populate selectedImage.hidden = false; selectedImage.image = imageFile; //Create original image for reservation originalImage = imageFile; //Crop image UIGraphicsBeginImageContext(selectedImage.frame.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextRotateCTM(context, 2*M_PI); [selectedImage.layer renderInContext:context]; imageFile = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); //Update imageView with croppedImage selectedImage.image = […]

把委托方法放到一个类别中

我到现在为止开发了一些应用程序。 现在我正在写一个新的,在这个项目中我想保持代码非常干净,所以很容易find方法。 我想从UIViewControllers开始,它的视图有一个UITableView作为子视图。 我希望有一个名为DetailViewController的文件,用于直接属于它的所有function。 另一个名为DetailViewController+Protocols文件应该包含上面类的一个类和UITableView的所有这些委托方法。 有没有可能做这样的事情? 我想保持我的代码干净,并将其分割成多个文件。 编辑 DetailViewController.h @interface DetailViewController : UIViewController … Some Properties … Some Methods @end DetailViewController.m #import "DetailViewController.h" @implementation DetailViewController … Some Synthesizes … Some Methods @end DetailViewController + Protocols.h @interface DetailViewController (Protocols) <UITableViewDelegate, UITableViewDataSource> @end DetailViewController + Protocols.m @implementation DetailViewController (Protocols) – (NSINteger)numberOfSections { return …; } – (NSInteger)numberOfRowsInSection:(NSInteger)section […]

Objective-C ++的缺点

我在Objective-C ++中为iOS编写一个大型项目。 我主要使用Objective-C用于UI和其他Apple API,而C ++用于内部audio处理和其他信息处理。 我想知道自由混合Objective-C和C ++的缺点。 当然,混合两个对象模型有其固有的局限性和潜在的混乱和混乱。 我更好奇如何使用Objective-C ++会影响编译过程,可能遇到的语法错误,可读性问题以及如何避免这些问题,等等。我有兴趣了解您的Objective-C ++经验。一直像你和提示你可能有接近这一点。

用给定的分布生成随机数

看看这个问题: 随机数被选中的概率是多less? 最好的答案build议使用一个switch语句,完成这项工作。 但是,如果我有很多情况要考虑的话,代码看起来很不雅观; 我有一个巨大的开关语句,在每种情况下,代码非常相似,一遍又一遍地重复。 有一个更好,更清洁的方法来select一个随机数字时,你有大量的概率考虑一定的概率? (像〜30)