草庐IT

properties

全部标签

ios - 存储的属性需要一个初始值或者应该是@NSManaged

我正在尝试转换Apple'ssamplecode用于为核心数据创建自定义部分标识符,以便将TableView中的部分从Objective-C正确排序/标记到Swift中。他们为声明为@dynamic的非transient属性实现了一个setter,因此他们可以在时间更改时使标识符无效。我以为我会在@NSManaged属性上使用didSet,但是Xcode抛出一个错误:Storedpropertyrequiresaninitialvalueorshouldbe@NSManaged.如何适本地转换以获得所需的行为?@property(nonatomic)NSDate*primitiveTi

Swift - 保持两个属性同步

在Swift中,我有两个相关的结构属性,我想保持同步。我目前正在与属性(property)观察员一起做这件事,但我不得不添加一个额外的标志来防止他们互相玩无限的乒乓球游戏。是否有更优雅和/或更透明的方式来实现这一目标?一个简化的例子:importFoundationstructAngle{varblockPropertyObservers=falsevardegrees:Double{willSet(degrees){print("willsetdegreesto\(degrees)")if!blockPropertyObservers{blockPropertyObservers=t

ios - "Cannot assign to property: "任何 "is immutable"错误

tiledViewsStack包含UIImageViews。我试图给数组中的每个UIImageView一个新的中心坐标。不知道为什么我在for循环的最后一行收到此错误...vartiledViewsStack=[AnyObject]()varrandLocInt=Int()varrandLoc=CGPoint()forvaranyintiledViewsStack{randLocInt=Int((arc4random()*10)%9)//0,---8randLoc=allCenters[randLocInt].CGPointValue()any.center=randLoc}

swift - 不符合协议(protocol)的属性(property)去哪儿了?

Teacher&TeamMate是两个协议(protocol)。Coach类符合这些协议(protocol)。protocolTeacher{varfirstName:String{get}varlastName:String{get}vartitle:String{get}}protocolTeamMate{varfirstName:String{get}funcrole()}classCoach:Teacher,TeamMate{varfirstName:StringvarlastName:Stringvartitle:Stringfuncrole(){print("coachth

xcode - 如何将 GIDSignInButton 类与 Google 身份验证结合使用?

Google有一个名为GIDSignInButton的类提供“使用Google登录”按钮。要自定义按钮,您必须修改包含的属性。你如何在Swift中正确地做到这一点?按钮变量:@IBOutletweakvargoogleSignInButton:GIDSignInButton!此处列出属性:developers.google.com 最佳答案 我找到了;在viewDidLoad()函数中使用这些来修改按钮:googleSignInButton.colorScheme=GIDSignInButtonColorScheme.Darkgoo

ios - Swift 属性观察不会覆盖

我编写了带有属性覆盖的类继承。我发现存储属性观察不像其他重写那样工作。这些类具有用于比较的存储属性和计算属性。classParent{varstoredProp:Int!{didSet{print("ParentstoredpropertydidSet")}}varcalcProp:Int{print("Parentcalculatedpropertyget")return100}}classChild:Parent{overridevarstoredProp:Int!{didSet{print("ChildstoredpropertydidSet")}}overridevarcalc

swift - 我们可以将 willSet 和 didSet 与 getter 和 setter 一起使用吗?

我正在阅读有关swift属性的willset和didset我开始知道我可以将它们与具有如下初始值的变量一起使用:varproperty="name"{willSet{print("propertyisabouttochanged")}didSet{ifproperty==oldValue{print("valuesaresame")}else{print("valuechanged")}}}property="anothername"我可以像下面这样使用willget和didset吗:varproperty2:String{willSet{print("valueisabouttoch

ios - TWTRTwitter sessionStore 现在返回 TWTRAuthSession : so how does one access the userName property now?

使用Swift中的TwitterKit3.3.0sharedInstance().sessionStore.session()现在返回一个TWTRAuthSession而不是TWTRSession,因为前。事情发生了变化,这很好,但是thedocumentation尚未更新以反射(reflect)这一点,因此我不再知道如何访问以前的TWTRSession对象提供的userName属性。 最佳答案 你可以用perform(_:)来做到这一点希望下面的内容能帮助那些面临这个问题的人。letusername=session.perform

swift - 提取 Swift 类常量属性的初始化代码

classFoo{letresult:CGFloatinit(){result=calculate()}privatefunccalculate()->CGFloat{//domanycomplexcalculationwithmanycodesreturn0}}毫无疑问,错误发生了。'self'usedinmethodcall'calculate'beforeallstoredpropertiesareinitialized`我知道有几种方法可以解决这个问题。var而不是let。例如var结果懒惰。例如惰性结果:CGFloat={return0}将calculate()设为class

swift - 如果全局属性总是惰性计算,而局部属性从不惰性计算,那么惰性修饰符会修改什么?

TheSwiftProgrammingLanguage(Swift2.1)第248页的注释解释了以下内容:Globalconstantsandvariablesarealwayscomputedlazily,inasimilarmannertoLazyStoredProperties.Unlikelazystoredproperties,globalconstantsandvariablesdonotneedtobemarkedwiththelazymodifier.Localconstantsandvariablesarenevercomputedlazily.摘自:AppleInc