草庐IT

swift - 用类型别名注释整个函数声明

我的目标是使用typealias作为“附加”到函数声明的单字提醒。说,typealiasVoidToVoid=()->()我可以在声明预期的闭包类型时使用类型别名,因此letf:VoidToVoid={return}assert(fisVoidToVoid)但是,这似乎不是最常用的函数声明方式。我希望有一种方法可以告诉读者像下面的f′声明的函数应该是VoidToVoid类型,并使用该名称。(但是没有读者不必推断、思考或等待编译器告诉他或她有关类型的信息。更糟糕的是,编译器无法知道类型别名我想要f′并且可能会输出说明裸类型而不是别名的消息。)funcf′():VoidToVoid{//t

json - 无法将类型 'AnyObject?' 的值转换为预期的参数类型 '[AnyObject]!

我正在使用swift库"Sync"fromHyperoslo将JSON用户转换为核心数据对象。letuser=JSON.valueForKey("user")Sync.changes(user,inEntityNamed:"User",dataStack:DataManager.manager,completion:{(response)->Voidinprint("USER\(response)")})但是当使用我的JSON对象设置此方法的第一个参数时,出现预编译错误:Cannotconvertvalueoftype'AnyObject?'toexpectedargumenttype

swift - 如何在 Swift 中检查/谓词函数类型?

例如:funcf(x:Int)->Int{returnx}funch(f:@escaping(Int)->Any){if(fis(Int)->Int){print(f(1))}else{print("invalid")}}h(f:f)我希望它打印出1但它实际上打印出invalid。 最佳答案 有一个使用泛型的解决方法:funcintF(x:Int)->Int{returnx}funcstringF(x:Int)->String{return"\(x)"}funch(f:(Int)->T){if(T.self==Int.self){p

Swift 无法转换匹配泛型约束的类型的值

给定以下类型protocolAProtocol{}protocolAnotherProtocol{}classAClass:AProtocol,AnotherProtocol{}我有点困惑为什么会抛出以下错误:“错误:无法将类型‘AClass’的值转换为指定类型‘T’”classgenericwhereT:AProtocol,T:AnotherProtocol{letprop:T=AClass()}但以下工作正常:classgenericwhereT:AProtocol,T:AnotherProtocol{letprop:Tinit(withPropprop:T){self.prop=

arrays - iOS swift : How to find unique members of arrays of different types based on specific attributes

目标:我有两个不同的类,以及两个包含每个类成员的数组。使用Swift2.0,我想根据每个类的特定属性找到一个数组与另一个数组相比的唯一成员。示例:classA{varname:Stringinit(name:String){self.name=name}}classB{vartitle:Stringinit(title:String){self.title=title}}letaArray=[A(name:"1"),A(name:"2"),A(name:"3"),A(name:"4")]letbArray=[B(title:"1"),B(title:"2"),B(title:"5")]

string - Swift String 常量的类型是否与 String 文字不同?

在Swift2.1中,下面的代码片段会产生错误。varstr="Hello,playground!"//SuccessCaseif"!"==str.characters.last{print("Toneitdownplease")}//FailCaseletbang="!"ifbang==str.characters.last{//thislinewon'tcompileprint("Toneitdownplease")}编译器错误说:Binaryoperator'=='cannotbeappliedtooperandsoftype'String'and'_Element?'那么在这种

Swift AnyObject 转换

我创建了一些testcode显示我遇到的问题。这在Playground上编译得很好,但是,当我尝试将它放入项目中时,Xcode给出以下警告:Treatingaforceddowncastto'String'asoptionalwillneverproduce'nil'关于line30.我得到了两个解决问题的建议:使用“作为?”执行条件向下转换为“String”,这完全没有意义。然而,它编译时没有警告/错误,这看起来很奇怪,因为它正在为非可选类型的String分配一个可选值。Usetheconditionalformofthetypecastoperator(as?)whenyouare

types - 括号内的 Swift 类型

我今天在玩Swift,一些奇怪的类型开始出现:letflip=Int(random()%2)//orarc4random(),orrand();whateveryouprefer如果我在Xcode6(Beta2)中输入flip,自动完成会弹出,并显示flip的类型是(Int)而不是Int。这很容易改变:letflip:Int=Int(random()%2)letflop=random()%2现在flip和flop的类型是Int而不是(Int)进一步研究,这些“括号类型”是可以预测的,您可以通过在变量赋值的任何部分添加额外的括号来实现它们。letg=(5)//becomestype(In

types - arc4random() 的余数运算符和数组的计数导致 "could not find an overload"错误

这段代码varrandomNumber:Int=arc4random()%nameArray.count给我错误“找不到接受所提供参数的‘%’的重载”我仍在努力适应语法并阅读文档,但似乎无法弄清楚这一点。谁能帮忙? 最佳答案 您必须将arc4random()的返回值(CInt)转换为Int:varrandomNumber:Int=Int(arc4random())%nameArray.count 关于types-arc4random()的余数运算符和数组的计数导致"couldnotfin

ios - 无法将类型的值转换为预期的参数类型 NSURLSessionDelegate

我知道它应该很简单,但我是Swift的新手,看不出这里有什么问题:publicclassAPI:NSObject,NSURLSessionDelegate{publicclassfuncsendHttpRequestNew(urlPath:String,params:AnyObject,method:String)->Int{vardelegate=selflettxtUserName="admin"lettxtPassword="test1234"letPasswordString="\(txtUserName):\(txtPassword)"letPasswordData=Pass