Tag: getter setter

使用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 = […]

如何从asynchronous属性的getter返回?

我已经覆盖了一个请求在线服务获取结果的getter。 如何强制getter只返回来自同步块的结果? @interface MyClass () @property (nonatomic, strong) NSMutableDictionary* myDictionary; @end @implementation MyClass -(NSMutableDictionary*) myDictionary { dispatch_async(queue, ^{ /* perform online request */ dispatch_sync(dispatch_get_main_queue(), ^{ // I need to obtain lock until this line gets executed and only then return }); }); } @end 用相当不错的谷歌search至less3小时,我遇到了dispatch_group_async , dispatch_semaphore和__block 。 我不知道我是否用错了,但没有达到目的。 更新1: myDictionary是一个asynchronous属性。 我想看看是否可以通过getter本身来实现。

当我重写Modern Objective-C中的setter和getters时,错误地访问生成的ivars

我现在知道新的Objective-C编译器可以让你不再需要综合你的属性。 我有一个文件,它有两个类。 我的.h为一个简单的助手类看起来像这样: @interface ViewFrameModel : NSObject @property (nonatomic, strong) UIView *view; @property (nonatomic, assign) CGRect frame; – (id)initWithView:(UIView *)view frame:(CGRect)frame; @end 在同一个.h文件中,对于我的其他类(class 2),我有: @property (nonatomic, strong) ViewFrameModel *viewFrameModel; 在2.mclass,我可以这样做: – (void)setViewFrameModel:(ViewFrameModel *)viewFrameModel { _viewFrameModel = viewFrameModel; [self pushViewFrameModel:viewFrameModel]; } 这工作正常,没有来自编译器的投诉,但是,当我添加这个: – (ViewFrameModel *)viewFrameModel { return _viewFrameModel; } 我得到两个投诉,第一个方法setViewFrameModel : “使用未声明的标识符_viewFrameModel,你的意思是viewFrameModel” 而另一个返回_viewFrameModel : “使用未声明的标识符_viewFrameModel,你的意思是viewFrameModel”“引用局部variablesviewFrameModel”在封闭的上下文中声明“ 为什么我在添加时遇到这些错误? […]

Swift协议只能设置?

为什么我可以做到这一点没有任何错误: var testDto = ModelDto(modelId: 1) testDto.objectId = 2 而我定义这个: protocol DataTransferObject { var objectType: DtoType { get } var parentObjectId: Int { get set } var objectId: Int { get } var objectName: String { get set } } struct ModelDto: DataTransferObject { var objectType: DtoType var parentObjectId: Int var objectId: Int var objectName: […]