草庐IT

ios - navigationController 在 Swift XCTest 案例中为 nil

在测试ViewController时,如何在测试用例中模拟/stubnavigationController?我收到运行时异常,因为在运行测试时它在我的UIViewController中为nil。您不能直接设置它,因为它是只读的。我正在使用Swift2.2和XCTest。 最佳答案 如上面的评论所述,这可以通过实例化UINavigationController并将您尝试测试的ViewController添加到其View数组中来实现。代码:letnavigationController=UINavigationController()

ios - 为什么 UICollectionView 的 didDeselectItemAt 会抛出 unexpected found nil 的错误并使应用程序 Swift 崩溃?

这个问题在这里已经有了答案:HowcanIfixcrashwhentaptoselectrowafterscrollingthetableview?(2个答案)关闭5年前。我使用了CollectionView来显示图像集合。它正在工作,但是当我使用函数didDeselectItemAt时如果我单击某些图像,它会崩溃。发生的错误:fatalerror:unexpectedlyfoundnilwhileunwrappinganOptionalvalue代码设置:numberOfItemsInSection:funccollectionView(_collectionView:UIColle

Swift,可选的包装器。 "?" "!"我明白它是如何工作的。但为什么它比 != nil 检查更好

我理解“!”或者”?”作品。但我不太确定与!=nil检查相比有什么额外好处。移动到“!?”的额外好处是什么?我觉得这只是苹果添加的东西,但与iOS的现状相比,看不到额外的好处。我在这里错过了什么吗?提前致谢。 最佳答案 检查nil和要求一个可选的被解包之间的区别可能等于你的代码崩溃与否之间的区别。如果使用得当,可选项既可以提高安全性,也可以让您的代码更具可读性。假设你有一个数组,你想取出其中的第一个值。你可以这样做:if!arr.isEmpty{useValue(arr[0])}当然,很容易忘记isEmpty部分,如果您忘记了,您的

swift - 为什么 nil 合并运算符 ??返回零?

考虑以下涉及nil合并运算符??的示例:letmysteryInc=["Fred":"Jones","Scooby-Doo":nil]letlastname=mysteryInc["Scooby-Doo"]??"nolastname"print(lastname==nil)//true如最后一个print语句所示,nil合并运算符的结果是nil。如果nil合并运算符应该解包Optional,为什么它返回nil? 最佳答案 要查看这里发生了什么,请将字典查找分配给一个常量:letname=mysteryInc["Scooby-Doo"

Swift 可选类型 : how . None == nil 有效

我想了解它是如何工作的:1>funcreturnNone()->String?{return.None}2>returnNone()==nil$R0:Bool=true3>returnNone()==.None$R1:Bool=true为什么.None等于nil。我在枚举定义中没有看到任何关于它的内容:publicenumOptional:_Reflectable,NilLiteralConvertible{caseNonecaseSome(Wrapped)///Constructa`nil`instance.publicinit()///Constructanon-`nil`inst

ios - 核心数据查询值 "is not nil"未返回值等于 0 的对象

我尝试返回属性rawValue不为nil的对象(Measure)。我的谓词是:myPredicate=NSPredicate(format:"rawValue!=%@","nil")还有我的要求letfetchRequest=NSFetchRequest(entityName:"Measure")fetchRequest.predicate=predicatedo{letfetchResults=trymanagedObjectContext?.executeFetchRequest(fetchRequest)as?[Measure]iffetchResults!.count>0{me

swift - tableView.cellForRowAtIndexPath(indexPath) 返回 nil

我有一个循环遍历我的TableView的验证函数,问题是它在某些时候返回nil单元格。forvarsection=0;section0{letcell=tableView.cellForRowAtIndexPath(indexPath)as!MyCell//cellisnilwhenself.tableView.numberOfRowsInSection(section)return3forrow1and2//...Otherstuff}}}我不太确定我在这里做错了什么,我尝试仔细检查indexPath行和部分,它们都很好,numberOfRowsInSection()返回3但第1行和

ios - self.view?.presentScene : unexpectedly found nil while unwrapping an Optional value (Swift)

我正在尝试切换场景,但我的应用程序崩溃并出现此错误:crashcrashfatalerror:unexpectedlyfoundnilwhileunwrappinganOptionalvalue(lldb)这是我切换场景的代码:funcswitchscenes(){ifdisplay>=2{Player.removeFromParent()PlayerRight.removeFromParent()PlayerLeft.removeFromParent()fireHair.removeFromParent()fireHairRight.removeFromParent()fireHai

ios - Swift 中的 UIView 子类 : IBOutlet is found nil while unwrapping

我正在尝试通过子类化UIView在Swift中创建自定义View,并且我有一个名为MyViewPanel.xib的View板,其类已分配给我的自定义View。实现如下:importUIKit@IBDesignableclassMyCustomView:UIView{@IBOutletweakvartitle:UILabel!varquestion:Question{didSet{print("didsetquestion,titleis:\(question.title)")}}overrideinit(frame:CGRect){super.init(frame:frame)}req

ios - 为什么,将 nil 作为参数从 Objc C 发送到 swift 类初始值设定项,用新对象替换 nil 参数

我创建了这个Swift类:@objcpublicclassTester:NSObject{privatevarname:Stringprivatevaruser:Usersinit(string:String,user:Users){print(user.empId)print(user.name)self.user=userself.name=stringsuper.init()}}我这样从ObjC调用初始化程序:-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDiction