草庐IT

Duck-typing

全部标签

ios - 将文件转换为 Swift 3 : unable to infer closure type in the current context

我正在转换我的应用程序中的一些库代码,但我不知道如何将该文件从Swift2.3转换为Swift3importUIKitstructConstraint{varidentifier:String?varattribute:NSLayoutAttribute=.centerXvarsecondAttribute:NSLayoutAttribute=.notAnAttributevarconstant:CGFloat=0varmultiplier:CGFloat=1varrelation:NSLayoutRelation=.equal}funcattributes(attrs:NSLayou

Swift 泛型 : Non-nominal type does not support explicit initialization

所以我试图理解通用协议(protocol)和类:protocolListPresenterTypewhereView.PDO.SW==Dispatcher.SW{associatedtypeDispatcher:ListDispatcherTypeassociatedtypeView:ListViewTypeinit(dispatcher:Dispatcher,state:@escaping(_state:AppState)->(ListState))funcattachView(_view:View)...}我从另一个通用类启动它:classAbstractListViewContr

swift - 整数类型 : Cannot invoke 'init' with an argument of type 'T'

我有一个返回范围内整数的函数:funcrandomNumber(min:T,max:T)->T{letn=max-min+1letu=UInt32(n)//Error:Cannotinvoke'init'withanargumentoftype'T'letr=arc4random_uniform(u)returnr+min}我不明白为什么这不起作用,因为UInt32最上面的协议(protocol)是UnsignedIntegerType,它符合IntegerType。我必须将n变成UInt32因为arc4random_uniform()需要一个UInt32作为参数为什么这行不通?

swift - 使用未声明的类型错误 : How to compare object with class type?

如何比较object与AnyClass泛型:我想比较一个对象和类的类型,类名应该作为参数传递。funccheckGeneric(className:AnyClass){letobject=UIViewController()if(objectisclassName){//Useofundeclaredtype`className`print(className)}}checkGeneric(className:UIViewController.self) 最佳答案 您可以使用type(of:)获取object的类型并将其与AnyCl

arrays - Swift 数组标识 "Type does not conform to protocol ' AnyObject'"错误

我创建了Playground并编写了这段代码:vara=[1,2,3]varb=aa===bPlayground给我错误Type'[Int]'doesnotconformtoprotocol'AnyObject'。我做错了什么?我正在使用XCode6GMSeed。更新此代码摘自“TheSwiftProgrammingLanguage”一书,其中指出:“Checkwhethertwoarraysorsubarrayssharethesamestorageandelementsbycomparingthemwiththeidentityoperators(===and!==).”在“类和结

ios - XCode 6.2 - CLAuthorizationStatus.Type 没有名为 'Authorized' 的成员

升级到XCode6.2后,我的工作代码突然开始抛出此错误错误:CLAuthorizationStatus.Type没有名为“Authorized”的成员错误就在这一行CLAuthorizationStatus.Authorized:"应用已获得使用定位服务的授权。",CLAuthorizationStatus.AuthorizedWhenInUse:"您已授权仅当应用对您可见时才使用您的位置。"] 最佳答案 看起来他们在CLAuthorizationStatus中用AuthorizedAlways替换了Authorized。参见he

swift - Xcode 6.3 和 Swift 1.2 中的新警告 : Cast from '[SKNode]' to unrelated type 'String' always fails

更新到xCode6.3/Swift1.2后,我遇到了一个我无法解决的错误:“从‘[SKNode]’转换为不相关的类型‘String’总是失败”。下面是我的GameScene代码,它从解析p-list开始。importSpriteKitclassGameScene:SKScene,SKPhysicsContactDelegate,NSXMLParserDelegate{varcurrentTMXFile:String?overridefuncdidMoveToView(view:SKView){/*Parsep-list*/letpath=NSBundle.mainBundle().pa

ios - 准备 segue : Cannot convert value of type 'UIViewController' to specified type 'SecondViewController'

我知道我肯定在这里遗漏了一些明显的东西,但我似乎无法通过它的实际类型来引用目标ViewController。我按照以下步骤从头开始创建了一个新项目来对此进行测试:创建一个新的单View应用程序项目添加一个新的ViewController创建一个新类,子类化UIViewController,并将其命名为SecondViewController将这个新类设置为第二个ViewController的自定义类(现在它的标题是第二个ViewController)向第一个ViewController添加一个按钮,然后按住Ctrl键单击它到第二个ViewController,然后从ActionSegu

xcode - swift 错误 : Cannot find an initializer for type 'Double' that accepts an argument list of type '(String)'

我正在尝试从标记为“temperatureTextField”的文本字段中获取数据并将其分配给“t”,这是一个Double。理想情况下,用户应该向temperatureTextField添加一个数字值。这是我的方法:@IBOutletweakvartemperatureTextField:UITextField!@IBActionfuncconvert(sender:AnyObject){lett=Double(temperatureTextField.text!)lettempM=TemperatureModel(temp:t!)temperatureTextField.text=S

ios - 核心数据错误 : Unsupported expression type (11, SIZE)

当我尝试创建以下NSPredicate时发生错误:letpredicate=NSPredicate(format:"size=nil")错误如下:Unsupportedexpressiontype(11,SIZE) 最佳答案 这是因为size是CoreData中的保留字。这可以像这样修复:letpredicate=NSPredicate(format:"#size=nil") 关于ios-核心数据错误:Unsupportedexpressiontype(11,SIZE),我们在Stack