草庐IT

swift - 当子类中有覆盖函数时如何转换为父类(super class)

创建了父类(superclass)Car和子类Jaguar。子类中的函数info()->Void覆盖了父类(superclass)的函数。已创建类型为Jaguar的名为theAuto的实例。问题:似乎我无法将theAuto转换为Car类型,请查看代码片段及其注释classCar{funcinfo(){print("You'vegotacar")}}classJaguar:Car{overridefuncinfo(){print("You'vegotaJaguar")}}lettheAuto=Jaguar()theAuto.info()//-->You'vegotaJaguarletau

ios - 子类化 UIButton 并覆盖触摸事件 - 不起作用

我需要一个项目中所有按钮的比例Spring动画。所以我子类化了UIButton并重写了触摸事件函数。importUIKitclassUIAnimatedButton:UIButton{overridefunctouchesBegan(touches:Set,withEventevent:UIEvent){UIView.animateWithDuration(0.1,animations:{()->Voidinself.transform=CGAffineTransformMakeScale(0.8,0.8)})super.touchesBegan(touches,withEvent:e

由 xib (nib) 文件初始化的 iOS Swift 子类化 ViewController

我有一个基类MyViewController:UIViewController由MyViewController.xib和一些socket初始化。我只在MyViewController.xib中将FileOwner类设置为MyViewController,在MyViewController.swift中没有任何初始化方法(全部继承自UIViewController),并且下一行按预期工作:letvc=MyViewController()View属性已设置,socket已设置。我希望继承MyViewController:SecondViewController:MyViewControl

ios - 报告 UIView 子类所需大小的正确方法是什么?

我正在对UIView进行子类化,但无法找到请求/报告其所需大小的正确方法。我希望systemLayoutSizeFittingSize可以解决这个问题,但它从未被调用过。布局很简单——我固定了我的View的顶部和前导,并将尾部和底部限制为小于或等于顶级View。在所有调整大小的函数中,只有intrinsicContentSize被调用,但那个函数不是很有用。importFoundationimportUIKitclassStatsView:UIView{overridefuncdrawRect(rect:CGRect){letcontext=UIGraphicsGetCurrentCo

ios - 在两个嵌套子类中调用 super.viewDidLoad() 时,我遇到了 Swift 的无限循环

我正在开发一个用Swift编写的iOS应用程序。我有一个UITabBarController的子类,然后是一个嵌套的子类:classHWTabBarController:UITabBarController{overridefuncviewDidLoad(){super.viewDidLoad()...}}classMainTabBarController:HWTabBarController{overridefuncviewDidLoad(){super.viewDidLoad()...}}这在iOS模拟器中运行良好,甚至当我在iPhone上调试应用程序时也是如此。但是当我存档应用程

inheritance - 是否可以将 Swift 泛型类函数返回类型限制为同一个类或子类?

我正在Swift中扩展一个基类(我无法控制的基类)。我想提供一个类函数来创建一个类型为子类的实例。需要通用函数。但是,像下面这样的实现不会返回预期的子类类型。classCalculator{funcshowKind(){println("regular")}}classScientificCalculator:Calculator{letmodel:String="HP-15C"overridefuncshowKind(){println("scientific")}}extensionCalculator{classfunccreate()->T{letinstance=T()ret

ios - Swift 中的 parse.com - 是否可以将检索到的 PFObject 转换为子类?

我已经创建了一个PFObject的子类,基本上遵循parse.comdocs上的说明,并将对象固定在本地。解析文档似乎没有涉及检索PFObject子类,我想知道-是否可以将检索到的对象转换为PFObject子类。如果是,怎么办?(我知道如果不可能,可能有必要根据PFObject的检索属性重新实例化子类。)letquery=PFQuery(className:Armor.parseClassName())query.fromLocalDatastore()query.findObjectsInBackgroundWithBlock({(objects:[AnyObject]?,error

ios - Swift 子类继承初始值设定项吗?

这个问题在这里已经有了答案:Classdoesnotimplementitssuperclass'srequiredmembers(4个答案)关闭6年前。我有以下代码:classParent{varfoo:Intinit(someNum:Int){self.foo=someNum}}classChild:Parent{}varparent=Parent(someNum:999)println(parent.foo)//prints"999"varchild=Child(someNum:3872)println(child.foo)//prints"3872"在Apple的2014年WW

swift - 为什么我不能将 UICollectionViewCell 向下转换为它的子类?

我正在制作一个CollectionView,并制作了我自己的自定义collectionCell。我已经在身份检查器中指定了,据我所知,我做的一切都是正确的。代码是importUIKitclassSubjectCollectionViewCell:UICollectionViewCell{@IBOutletweakvarimageView:UIImageView!@IBOutletweakvarlabel:UILabel!}在我的collectionViewController中overridefunccollectionView(collectionView:UICollectionV

ios - 防止子类覆盖 Swift 中的继承函数

有没有办法防止子类覆盖Swift中继承的函数? 最佳答案 看看final关键字。根据文档,Youcanpreventamethod,property,orsubscriptfrombeingoverriddenbymarkingitasfinal.Dothisbywritingthefinalmodifierbeforethemethod,property,orsubscript’sintroducerkeyword(suchasfinalvar,finalfunc,finalclassfunc,andfinalsubscript)