草庐IT

about_nil

全部标签

Swift:带有可选链接和 nil-coalescing 的编译时错误

我对Swift中的可选链接和nil-coalescing有一个奇怪的问题。谁能解释为什么以下代码无法编译:classA{varval:Tvarx:A?vary:A?init(t:T){val=t}functest()->[T]{return(self.x?.test()??[])+[val]+(self.y?.test()??[])}}但是写的时候functest()->[T]{return(self.x?.test()??[])+([val]+(self.y?.test()??[]))}是吗?错误说:Cannotconvertvalueoftype'[T]?'toexpectedar

ios - 检查 UICollectionViewCell 子类中的变量是否为 nil

我有这个UICollectionViewcellfunccollectionView(_collectionView:UICollectionView,cellForItemAtindexPath:IndexPath)->UICollectionViewCell{letcell=collectionView.dequeueReusableCell(withReuseIdentifier:cellId,for:indexPath)as!PostCellifletCurrentPost=posts[indexPath.row]as?Post{if(CurrentPost.PostImage

ios - 如何修复返回 nil 的 TableView numberOfRowsInSection?

我正在尝试使用OpenWeatherMapAPI检索5天的预报,我不确定为什么,但每次调用我的weatherCount()方法时它都返回nil。在View模型中,我使用print语句来验证行数应为40。我尝试使用guard语句并强制展开,这只会导致程序崩溃。我尝试实现回调方法,但我认为我没有正确实现它们。天气View模型importFoundationclassWeatherViewModel{varweatherInfo:WeatherData?weakvardelegate:WeatherDelegate?funcgetWeatherData(){letweather="https

ios - 当我在 block 中使用 self 时,我得到一个 nil。但是 self 没有释放

我使用继承BaseViewController的viewController。在BaseViewController的方法“viewDidLoad”中调用函数“monitorNetworkStatus()”。privatefuncmonitorNetworkStatus(){ReachabilityManager.shared.startMonitoring{[weakself](status,presentingVC)inprint(self?.description)}}ReachabilityManager是单例的。startMonitoring函数是这样的funcstartMo

properties - Swift - 延迟加载一个可以在以后设为 nil 的属性

我正在寻找一种方法来延迟加载我的变量,但我希望能够稍后将其设为nil,然后在获取时重新创建它。例如,在出现内存警告的情况下,我想清除所有未使用的内容,然后在需要时重新创建它。以下是我在Objective-C中的做法以及我目前在swift中的解释。我不确定它是否保留了用于保持当前导航的变量。Obj-C实现@property(strong,nonatomic,readwrite)UINavigationController*navController;...-(UINavigationController*)navController{if(!_navController){UIStory

ios - 从缓冲区创建的 NSData 创建 UIImage 返回 nil?

我试图通过抓取CGImage来使UIImage变暗,获取每个像素并从中减去0xa,然后将每个像素保存到一个新的缓冲区。但是当我尝试将该缓冲区作为图像加载回时,[创建CGImage]函数返回nil。这意味着我的代码中一定有错误(我不会感到惊讶)。我希望它与缓冲区格式不正确或其他原因有关。熟悉CoreGraphics的人可以帮我找出错误吗?varprovider=CGImageGetDataProvider(imageArray[imageNumber]?.CGImage)//GetdataproviderforimageinanarrayatindexNo.imageNumberletd

ios - NSJSONSerialization.JSONObjectWithData 返回 nil

[{"_id":"557f27522afb79ce0112e6ab","endereco":{"cep":"asdasd","numero":"asdasd"},"categories":[],"name":"teste","hashtag":"teste"}]没有错误返回nil:varjson=NSJSONSerialization.JSONObjectWithData(data,options:NSJSONReadingOptions.AllowFragments,error:&erro)as?NSDictionary 最佳答案

swift - Nil不能赋值给类型()->()?

因此,这是一个类中的函数,可让您进行双击和单击手势。它在Swift2.3中运行良好,但在转换为Swift3后会抛出一些错误。我无法理解/弄清楚我的生活。我评论了它们发生的地方。//UIShortTapGestureRecognizer.swift////importFoundationimportUIKitfuncdelayHelper(_time:TimeInterval,task:@escaping()->())->DelayTask?{funcdispatch_later(_block:@escaping()->()){DispatchQueue.main.asyncAfter(

ios - CMSampleBufferGetImageBuffer(sampleBuffer) 返回 nil

我使用此代码从相机捕获视频,但CMSampleBufferGetImageBuffer(sampleBuffer)始终返回nil。问题是什么?。这是代码,我修改了此源代码以适应Swift4https://github.com/FlexMonkey/CoreImageHelpers/blob/master/CoreImageHelpers/coreImageHelpers/CameraCaptureHelper.swiftimportAVFoundationimportCoreMediaimportCoreImageimportUIKitclassCameraCaptureHelper:

swift - 如何让 Nil Coalescing 运算符处理闭包?

letnilClosure:(()->Void)?=nilletnotNillable:AnyObject=nilClosure??1为什么这不起作用?任何展示如何让NilCoalescing运算符使用闭包的示例都值得赞赏。我得到的错误是:Binaryoperator'??'cannotbeappliedtooperandsoftype'(()->Void)?and'Int"' 最佳答案 来自Apple的文档:Thenilcoalescingoperator(a??b)unwrapsanoptionalaifitcontainsav