使用Setter和Getter方法与直接操作的好处 [英] Benefits of using Setter and Getter methods vs. direct manipulation

查看:170
本文介绍了使用Setter和Getter方法与直接操作的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是一个初学者问题,但我想知道什么好处是使用setter和getter方法,而不是直接操作它们。我在obj-c,我想知道是否在内存/ cpu使用方面有任何好处。

Sorry if this a beginner question but I was wondering what the benefits are to using setter and getter methods rather than directly manipulating them directly. I'm in obj-c and I'm wondering if there is any benefits in terms of memory/cpu usage.

例如,我裁剪图像之前我上传它,我裁剪它后,采取/挑。我把所有的代码放在 didFinishPickingMediaWithInfo 方法中。所以它看起来像下面:

For instance, I'm cropping an image before I upload it and I crop it after it is taken/picked. I put all my code in the didFinishPickingMediaWithInfo method. So it would look like the following:

-(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 = imageFile;

    //Dismiss image picker
    [imagePickerController dismissViewControllerAnimated:YES completion:nil];

}

所以让我们说我做同样的事情,用于填充 selectedImage imageView和用于裁剪图像的方法,如下所示:

So let's say I do the same thing but have a method for populating the selectedImage imageView and a method for cropping the image so it would look like the following:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    //Create image
    [self setImage:[info objectForKey:UIImagePickerControllerOriginalImage]];

    //Create local image
    UIImage * localImage = [self returnImage];

    //Unhide imageView and populate
    selectedImage.hidden = false;
    [self populateImageView:localImage];

    //Create original image for reservation
    originalImage = localImage;

    //Crop image
    localImage = [self getImageFromContext:localImage withImageView:selectedImage];

    //Update imageView with croppedImage
    [self populateImageView:localImage];

    //Dismiss image picker
    [imagePickerController dismissViewControllerAnimated:YES completion:nil];

}

//Crop image method
-(UIImage *)getImageFromContext:(UIImage *)image withImageView:(UIImageView *)imageView{

    UIGraphicsBeginImageContext(imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextRotateCTM(context, 2*M_PI);

    [imageView.layer renderInContext:context];
    image =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

    image = nil;
    context = nil;

}

-(void)populateImageView:(UIImage *)image{

    selectedImage.image = image;

}

- (void)setImage:(UIImage *)image{

    imageFile = image;

}

-(UIImage *)returnImage{

    return imageFile;

}

除了可读性和整洁性,还有其他好处码?

So are there any other benefits other than readability and neatness of code? Is there anyway to make this more efficient?

推荐答案

您有一个 Big Nerd Ranch制作的伟大基准

通常我使用属性作为最佳实践。这是有用的,因为您有:

Usually I use properties as a best practice. This is useful because you have:


  • 您的资源将被访问的预期地点(getter)


这通常有助于调试(可以覆盖setter或set一个断点,以检查谁正在改变属性,当它正在改变),你可以做一些延迟实例化。

This usually helps in debugging (you can override the setter or set a breakpoint there to check who is changing the property and when it is changing) and you can do some lazy instantiation.

通常我做延迟实例化数组或用程序创建视图。例如:

Usually I do lazy instantiation with arrays or with programmatically created views. For instance:

@property(nonatomic, strong) UIView *myView;

-(UIView*) myView {
   if(!_myView) {
       //I usually prefer a function over macros
       _myView = [[UIView alloc] initWithFrame: [self myViewFrame]];
       _myView.backgroundColor = [UIColor redColor];
   }

   return _myView;
}

另一个重要的因素以 Jacky Boy 是属性,你有一个免费的KVO ready结构。

Another important factor bolded by Jacky Boy is that with properties you have a free KVO ready structure.

这篇关于使用Setter和Getter方法与直接操作的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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