草庐IT

override_function

全部标签

function - Swift 类函数

所以我想知道类函数和“普通实例函数”。我想知道类函数的目的是什么。只是为了可以使用某些功能而无需先将类分配给变量,还是它有其他目的?classDog{funcbark()->String{return"WoefWoef!"}classfuncbark_class()->String{return"WoefWoef!"}}vardog=Dog()dog.bark()//WoefWoef!Dog.bark()//ThrowsanderrorDog.bark_class()//WoefWoef!>Apparentlyisdoens'tneedaninitiatedobject

function - Swift 方法中的多个未命名参数

据我所知,Swift函数默认有未命名的参数,但Swift方法没有(第一个参数除外)。例如,给定以下定义:funcfoo(a:Int,b:Int)->Int{returna+b}classMyClass{funcbar(a:Int,b:Int)->Int{returna+b}}我必须使用未命名的参数调用foo:foo(10,20)//finefoo(a:10,20)//errorfoo(10,b:20)//errorfoo(a:10,b:20)//error我必须调用bar,第一个参数未命名,第二个参数命名为:MyClass().bar(10,b:20)//fineMyClass().b

function - 大括号的 Swift 行业标准

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭7年前。Improvethisquestion在Swift中,我正在尝试弄清楚我是否应该这样做if(true){//stuff}else{//otherstuff}或者if(true){//stuff}else{//otherstuff}我知道从技术上讲这没有什么区别,但我想知道行业标准是什么,以及为什么标准是……标准。

Swift: Overriding Self-requirement 是允许的,但会导致运行时错误。为什么?

我刚开始学习Swift(v.2.x),因为我很好奇新功能是如何发挥作用的,尤其是带有self要求的协议(protocol)。下面的示例将编译得很好,但会导致任意运行时影响的发生://TheprotocolwithSelfrequirementprotocolNarcissistic{funcgetFriend()->Self}//BaseclassthatadoptstheprotocolclassMario:Narcissistic{funcgetFriend()->Self{print("Mario.getFriend()")returnself;}}//Intermediatec

ios - swift - "unexpected non-void return value in void function"

我已经开始编写一个用户类,它有一个方法来计算用户与对象的距离。它看起来像这样:classUser{varsearchRadius=Int()varfavorites:[String]=[]varphoto=String()varcurrentLocation=CLLocation()funccalculateDistance(location:CLLocation){letdistance=self.currentLocation.distanceFromLocation(location)*0.000621371returndistance//Errorreturnsonthisli

ios - "Method does not override any method from its superclass" swift 3

这个问题在这里已经有了答案:Swift2to3MigrationforprepareForSegue[duplicate](1个回答)关闭6年前。这段代码是swift2overridefuncprepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?){letplayerViewController=segue.destinationas!PlayerViewControllerplayerViewController.videoID=channelsDataArray[selectedVideoIndex]["videoID"]

swift 2 : Try/Catch a non-throwing function

我正在重构一个Obj-c类,其中在removeObserver:周围有一个@try/@catchblock。在swift中做同样的事情会触发警告,因为removeObserver可能会失败(当没有观察者时)但它不会抛出任何错误。知道如何实现相同的行为吗?编辑:我的代码:try{self.removeObserver(self,forKeyPath:"LineDisplayChanged")} 最佳答案 您调用的funcremoveObserver(_anObserver:NSObject,forKeyPathkeyPath:Stri

swift - Cocoa pod安装报错: "The target overrides the SWIFT_VERSION build setting "

我正在尝试将Cocoalumberjack安装为pod,但出现此错误:TheProjectName[Release]targetoverridestheSWIFT_VERSIONbuildsettingdefinedin`Pods/TargetSupportFiles/Pods-ProjectName/Pods-ProjectName.release.xcconfig'.ThiscanleadtoproblemswiththeCocoaPodsinstallationpodxcconfig文件包含以下内容:SWIFT_VERSION=3.0但是我的项目已经在使用swift3.所以我不明

ios - : override public func observeValueForKeyPath 错误

我正在使用适用于Xcode7Beta2的代码。刚刚获得Beta3,现在我遇到了这个看起来像是Xcode错误的问题?对于我的一门公开课...overridepublicfuncobserveValueForKeyPath...Methoddoesnotoverrideanymethodfromitssuperclass所以我去掉了“override”:publicfuncobserveValueForKeyPathMethodconflictswithmethodfromsuperclassNSObjectwiththesameObjective-Cselector哈!我被第一个错误骗了

cocoa - Swift 协议(protocol) : method does not override any method from its superclass

由于Xcode6仍然有很多Swift的错误,我不确定是一个错误还是我遗漏了什么。我的类采用协议(protocol)NSLayoutManagerDelegate。但似乎不可能覆盖我需要的方法。我按照文档描述的那样做:overridefunclayoutManager(_aLayoutManager:NSLayoutManager!,didCompleteLayoutForTextContaineraTextContainer:NSTextContainer!,atEndflag:Bool){}但我在这里遇到错误:方法没有覆盖其父类(superclass)中的任何方法。我该怎么办?