草庐IT

question_type

全部标签

swift 2 : ! , ? -"Value of optional type ".. ."not unwrapped"

Valueofoptionaltype"..."notunwrapped;didyoumeantouse'!'or'?'?谁能解释一下这条错误消息的确切含义?我什么时候应该使用“?”什么时候“!”? 最佳答案 obj?.fn()如果对象不为null,则调用fn成员函数,否则不执行任何操作。另一方面,obj!.fn()断言obj不为空,并调用fn。如果对象为null,则会出现异常。所以这是自信的不同:您要么询问,要么简单地声明可空对象的可空属性。 关于swift2:!,?-"Valueof

ios - Swift 编译错误 "Unknown type name"和 "Expected a type"

我遇到了一个问题,我的Swift代码编译正常,但生成的-Swift.h文件中有错误...这是我正在做的一个例子:classMyScene:CCLayer{varctrl:CCControlSlider?}这在swift代码中编译得很好,我也可以看到ctrl对象上的方法也很好。我的桥头中有以下内容:#import"cocos2d.h"#import"CCControlSlider.h"这在使用其他正常工作的库的其他类中工作得很好。另请注意,我可以在我的objective-c类中使用这个CCControlSlider类,也没有任何问题。这是生成的-Swift.h文件中发生的事情:SWIFT

ios - Swift 编译错误 "Unknown type name"和 "Expected a type"

我遇到了一个问题,我的Swift代码编译正常,但生成的-Swift.h文件中有错误...这是我正在做的一个例子:classMyScene:CCLayer{varctrl:CCControlSlider?}这在swift代码中编译得很好,我也可以看到ctrl对象上的方法也很好。我的桥头中有以下内容:#import"cocos2d.h"#import"CCControlSlider.h"这在使用其他正常工作的库的其他类中工作得很好。另请注意,我可以在我的objective-c类中使用这个CCControlSlider类,也没有任何问题。这是生成的-Swift.h文件中发生的事情:SWIFT

ios - 错误 : Initializers may only be declared within a type

这是我的代码extensionUIImage{convenienceinit(color:UIColor,size:CGSize=CGSizeMake(1,1)){letrect=CGRectMake(0,0,size.width,size.height)UIGraphicsBeginImageContext(rect.size)letcontext=UIGraphicsGetCurrentContext()CGContextSetFillColorWithColor(context,color.CGColor)CGContextFillRect(context,rect)letima

ios - 错误 : Initializers may only be declared within a type

这是我的代码extensionUIImage{convenienceinit(color:UIColor,size:CGSize=CGSizeMake(1,1)){letrect=CGRectMake(0,0,size.width,size.height)UIGraphicsBeginImageContext(rect.size)letcontext=UIGraphicsGetCurrentContext()CGContextSetFillColorWithColor(context,color.CGColor)CGContextFillRect(context,rect)letima

swift 4 : Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()' or Argument passed to call that takes no arguments

使用XCode9,Beta3。Swift4。statsView.createButton("Buttonname"){[weakself]Voidin//stuffstuffself?.doSomething()}我遇到了数百个这样的错误,我该如何修复它们?错误:Cannotconvertvalueoftype'(_)->()'toexpectedargumenttype'()->()'Argumentpassedtocallthattakesnoarguments 最佳答案 我们似乎不再在Swift4中使用Voidin了。我是如何

swift 4 : Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()' or Argument passed to call that takes no arguments

使用XCode9,Beta3。Swift4。statsView.createButton("Buttonname"){[weakself]Voidin//stuffstuffself?.doSomething()}我遇到了数百个这样的错误,我该如何修复它们?错误:Cannotconvertvalueoftype'(_)->()'toexpectedargumenttype'()->()'Argumentpassedtocallthattakesnoarguments 最佳答案 我们似乎不再在Swift4中使用Voidin了。我是如何

ios - executeFetchRequest 抛出 fatal error : NSArray element failed to match the Swift Array Element type

我正在使用CoreData测试swift并创建了以下代码:importUIKitimportCoreDataclassContact:NSManagedObject{@NSManagedvarname:String@NSManagedvaremail:Stringclassfuncexecute(){letappDel:AppDelegate=(UIApplication.sharedApplication().delegateasAppDelegate)letcontext:NSManagedObjectContext=appDel.managedObjectContext!lete

ios - executeFetchRequest 抛出 fatal error : NSArray element failed to match the Swift Array Element type

我正在使用CoreData测试swift并创建了以下代码:importUIKitimportCoreDataclassContact:NSManagedObject{@NSManagedvarname:String@NSManagedvaremail:Stringclassfuncexecute(){letappDel:AppDelegate=(UIApplication.sharedApplication().delegateasAppDelegate)letcontext:NSManagedObjectContext=appDel.managedObjectContext!lete

ios - Swift 3.0 迁移错误 : Type 'Element' constrained to non-protocol type 'IndexPath'

我正在尝试使用xCode将我的代码库迁移到swift3.0。有几个问题我无法理解。问题:类型“Element”被限制为非协议(protocol)类型“IndexPath”在错误导航面板的左侧,它仅显示错误下方。我无法理解是哪一行代码或代码分支导致了错误。谁能帮我解决这个问题。UPDATE经过一番努力后,我陷入了这些问题。UPDATE谢谢大家的帮助。现在我只面临以下问题。很少有人要求发布源代码,但Xcode没有在页面上显示任何类型的警告或错误。泛型很少privateextensionArraywhereElement:IndexPath{funcindexOf(_indexPath:In