草庐IT

Type_Ary

全部标签

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

ios - swift 2 : Cannot subscript a value of type '[GKScore]?'

让highscore=leaderboardRequest.scores[0]为!GKScore结果为无法下标“[GKScore]”类型的值?Swift2中正确的语法是什么 最佳答案 leaderboardRequest.scores未解包,使用下标前解包。ifletscores=leaderboardRequest.scores{iflethighscore=scores.firstas?GKScore{//safelyuse`highscore`here}}“展开”是Swift中的一个关键概念,它与Optionals一起出现。[

ios - swift 3.1 : Cannot convert value of type '(_) -> ()' error/Problems with closures

我升级到Swift3.1,我收到了一些新错误,这些错误似乎是3.1语法问题,因为在迁移之前它们不是问题。它们大多与闭包有关,如本例所示:letalert=UIAlertController(title:"Success",message:"Thanksforparticipatinginourraffle!",preferredStyle:UIAlertControllerStyle.alert)alert.addAction(UIAlertAction(title:"OK",style:.default,handler:{performSegue(withIdentifier:"to