草庐IT

Getters-Setter

全部标签

ios - 使用来自另一个 View Controller 的 setter getter

我已经用它的类(.Swift)创建了一个CustomView(.Xib)。为了达到它的View(小部件)属性,我定义了几个getter和setter,现在我需要调用它们进行修改,但是当我从类中创建对象时,我无法修改属性或使用get{}方法。这是我自定义的View类代码:CustomClass.Swift@IBOutletweakvarbarLbl:UILabel!@IBDesignableclassCustomControl:UIView{@IBInspectablevarlblSetGetName:String!{set{barLbl.text=newValue}get{return

swift - Setter 在使用协议(protocol)转换访问计算变量期间调用结构

我为遵守协议(protocol)的结构创建了一个全局变量。变量有一个setter观察器,它在设置变量时完成任务。由于结构本身遵守协议(protocol),因此我希望变量的类型是协议(protocol)类型。protocolTestProtocol{varsomeInt:Int{getset}varcomputedVar:Int{get}funcfuncCall()->Int}structTestStruct:TestProtocol{varsomeInt:Int=1varcomputedVar:Int{return0}funcfuncCall()->Int{return2}}varte

swift - 错误 : Variable with getter/setter cannot have an initial value

如何修复此错误?Variablewithgetter/settercannothaveaninitialvalue这是我的代码:functableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath)->UITableViewCell{varcell:UITableViewCell=tableview.dequeueReusableCellWithIdentifier("cell")asUITableViewCell{//Errorcell.textLabel?.text=self.items[ind

ios - Swift 中缺少属性 setter 和 getter

我正在尝试转换thisObjective-C类到Swift。以下是我到目前为止所做的。importUIKitpublicclassCustomColoredAccessory:UIControl{varaccessoryColor=UIColor()varhighlightedColor=UIColor()requiredpublicinit(coderaDecoder:NSCoder!){super.init(coder:aDecoder)}overrideinit(frame:CGRect){super.init(frame:frame)backgroundColor=UIColo

ios - 元组属性的自定义 setter (Swift)

我的类中有一个计算属性,它是一个可选的元组:varcontents:(Int,Bool)?我想编写自定义setter,但不知道如何单独引用元组组件。有谁知道?我尝试了以下但它不起作用:varcontents:(IntcolorNumber,Boolselected)?{//getter&setterhere...} 最佳答案 使用let分解元组variVal:Int?varbVal:Bool?varcontents:(Int,Bool)?{get{ifiVal!=nil&&bVal!=nil{return(iVal!,bVal!)}

swift - 有没有办法只弃用变量的 setter ?

我目前在类openvardeviceIdentifier:String?中有一个变量,我想弃用setter只将它变成openprivate(set)vardeviceIdentifier:String?有什么办法吗?我尝试将这两行都放入,但这显然是Xcode不喜欢的冲突。@available(*,deprecated)openvardeviceIdentifier:String?openprivate(set)vardeviceIdentifier:String?如果我将它更改为计算变量,编译器会很高兴,但我想避免这种情况。这是唯一的方法吗,它会按预期工作吗?privatevar_de

swift - a 如何在结构中使用 setter 的值进行操作?

我有这段带有getter和setter的swift结构代码。newValue的值是如何进入这个setter的?我不知道它是如何定义的。structProgram{varallResults:[String]=[]varitems:Int{get{returnallResults.count}set{letresult=String(newValue*100)//what'sthatnewValue,howdiditgetthere?allResults.append(result)}} 最佳答案 它被称为ShorthandSette

ios - 你如何使用 perform(#selector(setter :)) to set the value of a property using swift 4?

我正在尝试运行这段代码,但它产生了意想不到的结果。classTest:NSObject{@objcvarproperty:Int=0}vart=Test()t.perform(#selector(setter:Test.property),with:100)print(t.property)正在打印的值是一些垃圾号码-5764607523034233277。如何使用perform方法设置属性的值? 最佳答案 performSelector:withObject:方法需要一个对象参数,因此Swift将基本类型100转换为对象引用。se

ios - 无法注入(inject)属性,找不到 setter 选择器

使用Typhoon和Swift,我正在设置我的项目,但我遇到了这个问题。我有一个TPLAddInteractor这样的类classTPLAddInteractor:NSObject,TPLAddInteractorInput{varoutput:TPLAddInteractorOutput?vardataManager:TPLDataManagerInterface?}我的程序集是这样的classTPLAddAssembly:TyphoonAssembly{varapplicationAssembly:TPLApplicationAssembly?dynamicfuncaddInter

swift - "Use of unresolved identifier"对隐式 setter 上的选择器使用新的 Swift 2.2 语法

将我的代码迁移到Swift2.2,我有一个属性varactiveTextField:UITextfield?并且我使用的选择器是"setActiveTextField:"。此方法在我的swift代码中不明确存在。使用新语法,#selector(setActiveTextField)不起作用:Useofunresolvedidentifier我知道我可以使用Selector("setActiveTextField:")但我会失去新的快速选择器的好处。那么,这样做的新方法是什么? 最佳答案 这里的问题是您使用的是属性,而不是方法。这有两