草庐IT

objective-c - 在 swift 中从 objective-c header 访问 getter 属性

在objective-cheader中,我们有一个如下属性:@property(nonatomic,assign,getter=isMyThingEnabled)BOOLenableMyThing;在swift中,任何像这样访问此属性的尝试都会生成一个错误,指出找不到该属性:myClassInstance.isMyThingEnabled那么如何在swift中访问属性isMyThingEnabled? 最佳答案 即使您在中为getter方法定义了自定义名称Objective-C,来自Swift的访问仍然使用属性名称,所以这有效:le

ios - RealmSwift 嵌套对象 getter 不返回数据

我的Realm对象在尝试访问该属性时返回0时遇到一些问题。当我这样做时:print(calculation)print(calculation.homePrice)它给了我:PaymentsCalculation{id=90941762-E648-4377-ABA3-4BDA88880768;homePrice=200000;downPayment=40000;interestRate=0.04;homeInsurance=900;HOADues=0;loanType=30-yearfixed;hidePMI=0;}0.0为什么会这样?计算是来自User类的嵌套对象:importRea

swift - Swift 字典的通用项目 getter

假设我有一个Dictionary为此,我希望为实现StringLiteralConvertible的类型提供方便的setter/getter。.letitems=["Key1":"Value1","Key2":"Value2"]funcget(key:String,_defaultValue:T)->T{ifletvalue=items[key]{returnT(stringLiteral:value)//BUG}returndefaultValue}但是在有BUG注释的那一行放什么呢? 最佳答案 该行实际上是正确的,但上面的行应该

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

swift - 警告 : Attempting to access 'myProperty' within its own getter vs SwiftFormat

我有这样的代码:@IBInspectablevarmyProperty:String?{set(newValue){//logicforsetter}get{returnmyProperty}}上面的代码产生了一个警告:Attemptingtoaccess'myProperty'withinitsowngetter.Access'self'explicitlytosilencethiswarning.所以我按照建议修改代码:@IBInspectablevarmyProperty:String?{set(newValue){//logicforsetter}get{returnself.

caching - Swift getter 代码重复

我正在尝试在swift中使用变量,这些变量是关键的应用程序范围的用户设置,因此它们必须在每次更改后保存到磁盘。这些变量很少,我对应用程序启动后从磁盘进行的第一次读取感到满意。我有类似这样的代码:var_myEnumMember:MyEnum?varmyEnumMember:MyEnum{get{ifletvalue=_myEnumMember{//inmemoryreturnvalue}varc:Cache=Cache()varstoredValue:MyEnum?=c.get("SomeStorageKey");ifletvalue=storedValue{//existsondis

ios - Swift:当我们对 Int 进行扩展时,属性 getter 中的 "self"应该是什么?

这似乎是一个错误并在Xcode7中用Swift(2b3)解决了为了方便观察,我们把代码贴到playground上,结果会在评论中指出。extensionInt{varsq:Int{mutatingget{self//5self=self*self//25returnself//25Here25ismade}}}varn:Int=5//5n.sq//25n//5Whyisn't25?我的问题是,如果属性sq的getter中的self指的是整数本身,为什么n在之后保留5>n.sq做了?让我们看看如果我们只添加一个setter会发生什么:extensionInt{varsq:Int{muta

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