草庐IT

getter-setter

全部标签

ios - 使用 Objective-C 属性 getter 实现 Swift 协议(protocol)

我想为Swift中所有可突出显示的View创建一个共同的祖先。我希望已经存在的实现highlighted属性的UIKit类开箱即用,所以在阅读thisanswer之后并检查theObjective-CgetterisdefinedasisHighlighted我将协议(protocol)定义更改为:@objcprotocolHighlightable{varhighlighted:Bool{@objc(isHighlighted)getset}}所以UILabel和UIControl的协议(protocol)实现就这么简单:extensionUILabel:Highlightable{

objective-c - 在 Swift 中命名 Obj-C 属性 getter 和 setter

我正在寻找访问的通用解决方案:Obj-C来自Swift的命名属性getter和命名属性setter符合具有readonly属性的Objective-C@protocol类似于CreatinganObjective-CequivalentGetterandSetterinSwift,已关闭,但未提供令人满意的答案。Objective-C到Swift属性示例:我有一个Objective-C协议(protocol),它定义了两个有问题的属性,一个带有自定义getterisEnabled,另一个带有私有(private)setterexists。@protocolSomeProtocol@pr

xcode - 带有 Objective-C 选择器 'setChecked' 的方法 'setChecked:' 与具有相同 Objective-C 选择器的 'checked' 的 setter 冲突

我创建了继承自UIButton的自定义类。在那个类中我创建了一个定义为的函数:funcsetChecked(checked:Bool){self.checked=checkedifchecked{buttonImageView.image=UIImage(named:"radioSelected.png")}else{buttonImageView.image=UIImage(named:"radioUnselected.png")}}在我将我的xCode更新到6.1.3之前它工作正常。现在我不断在函数定义行收到错误消息:Method'setChecked'withObjective-

ios - Swift - 带有 Objective-C 选择器 '*()' 的方法 '*' 与具有相同 Objective-C 选择器的父类(super class) '*' 的 'NSObject' 的 getter 冲突

自从将我的xcode更新到6.3.1后,我收到了这条错误消息。/Users/MNurdin/Documents/iOS/xxxxx/Models/Message.swift:46:10:Method'hash()'withObjective-Cselector'hash'conflictswithgetterfor'hash'fromsuperclass'NSObject'withthesameObjective-Cselector我的代码varhash_:UIntfunchash()->UInt{returnUInt(hash_);} 最佳答案

Swift - 在 getter 上返回自身属性并在 setter 上设置自身

无论如何要在getter中返回自身属性并在setter中设置自身?因为我只想在getter和setter中打印日志,仅此而已。例如我尝试做这样的事情:private(set)varinternetConnectionAvailable:Bool{get{logger.debug("verifyinginternetconnection")//herereturnitselfproperty}set{logger.debug("changinginternetconnection")//heresetitself}}如果我在getter上返回self.internetConnection

ios - 覆盖 UIImageView 的图像 getter/setter 方法

我将UIImageView子类化,以便每次设置图像属性时都会出现动画。以下是成功的:importUIKitclassAnimatedImageView:UIImageView{varimg:UIImage!{get{returnself.image}set{self.image=newValueUIView.animateWithDuration(0.5,delay:0.4,usingSpringWithDamping:0.2,initialSpringVelocity:5.0,options:.CurveEaseIn,animations:{_inself.transform=CGA

swift - 在 Swift 中,为什么分配给静态变量也会调用它的 getter

我知道在Swift中,静态变量是隐式惰性的:https://stackoverflow.com/a/34667272/1672161但我不清楚为什么会这样:protocolHatType{}classHat:HatType{init(){print("realhat")}}classMockHat:HatType{init(){print("mockhat")}}structHatInjector{staticvarhat:HatType=Hat()}HatInjector.hat=MockHat()//Output://realhat//mockhat我看到的是,对静态变量的赋值在某

ios - Swift 中的惰性属性等同于 Objective C 中的惰性 Init getter

Swift中的lazy属性是否等同于在ObjectiveC中使用延迟加载模式覆盖getter? 最佳答案 来自文档:Alazystoredpropertyisapropertywhoseinitialvalueisnotcalculateduntilthefirsttimeitisused.Youindicatealazystoredpropertybywritingthelazyattributebeforeitsdeclaration.所以,大多数情况下,是的。Youmustalwaysdeclarealazypropertya

arrays - nil 在 swift 中检查 getter 方法内部?

我的ViewController中有一个数组。只有当它为nil时才应该分配它,否则它应该返回现有值。等效于ObjectiveC:-(NSArray*)states{if(!_states){_states=//readfilesfromjsonandassignedtoarray}return_states;}我必须swift实现这一目标。我尝试使用存储的属性但无法实现这一点什么是实现这一目标的最佳方式 最佳答案 可以是这样的:classWhatever{private(set)var_states:[AnyObject]?vars

java - POJO的getters和setters有必要吗

这个问题在这里已经有了答案:Aregettersandsetterspoordesign?Contradictoryadviceseen[duplicate](16个答案)关闭8年前。我一直在阅读干净的代码书,其中指出该类不应公开其数据的内部状态,而应该只公开行为。如果一个非常简单和愚蠢的javabean暴露了getter和setter的内部状态,是否不值得只删除它们并使私有(private)成员公开?或者只是把类当作一个数据结构?