草庐IT

optional_width

全部标签

swift - 无法使用 AutoreleasingUnsafePointer 解包 Optional.None

在使用Applescript命令运行NSAppleScript.executeAndReturnError时应该会失败并返回错误,我收到Can'tunwrapOptional.None错误forerrorDict。varerrorDict:NSDictionary?=nilvarscriptObject:NSAppleScript=NSAppleScript(source:command)varexecute:NSAppleEventDescriptor=scriptObject.executeAndReturnError(&errorDict)我知道错误是由于解包了一个nil可选变量

ios - Swift Array 正在从我的数组中打印单词 "optional"不想强制解包

我的数组:varsavedDataArray:Array?=NSUserDefaults.standardUserDefaults().objectForKey("savedDataArray")as?Array控制台打印Optional(["75500.0","2.19351e+08"])我不想要“可选”这个词或大括号强制展开varsavedDataArrayArray!停止出现“可选”一词,但我的数组可能是nil所以我不想这样做(而且牙套仍然存在)这与以下答案不同,因为它们都建议使用力展开Printingvalueofaoptionalvariableincludestheword

ios - swift 中 bounds.size.width 和 bounds.width 之间的区别?

swift中bounds.size.width和bounds.width有什么区别?他们会返回同样的东西吗?谢谢! 最佳答案 bounds是UIView的CGRect结构属性,其中包含2个嵌套结构;CGPoint和CGSize。它们代TableView的原点(x和y),以及分别以点指定的高度和宽度的View大小。如果您有一个100x100的UIView,那么:bounds.width将返回100,bounds.size.width也将返回100。基本上它们会即使您的CGRect具有负宽度和高度值,也会返回相同的CGFloat值。

objective-c - fatal error : unexpectedly found nil while unwrapping an Optional value, 快速获取 json 错误

获取json错误当我尝试返回json时,出现错误fatalerror:unwrappedlyfoundnilwhileunwrappinganOptionalvalue:funcgetJson(str:NSString)->AnyObject{varproxiesURL=NSURL(string:str)varproxiesDataJson=NSData.dataWithContentsOfURL(proxiesURL,options:NSDataReadingOptions.DataReadingUncached,error:nil)varjson:AnyObject!if(prox

ios - Xcode 自动布局 : keep minimum between height and width of square

我为UIView使用了固定的纵横比,所以它是正方形的。然后我将固定的尾随和前导空间设置为屏幕边框,它们是相同的。并保持垂直居中位置。所以结果我在屏幕中央得到了一个正方形,前后有一些相等的空间,如果屏幕更宽,正方形就更大。这在纵向方向上非常有效。但是当我使用横向时,基于前导和尾随空间的正方形大小变得大于设备的高度,所以它不适合。这是一种基于约束并选择最小的二维的方法吗?这是一种让iPhone和iPad的屏幕边框间距不同的方法吗? 最佳答案 为顶部添加一个不等式约束,使间距大于或等于您的最小值。这实际上会与您的其他约束产生冲突。您可以通

ios - CLLocationManager fatal error : unexpectedly found nil while unwrapping an Optional value Swift

我有一个问题,因为当我尝试获取位置用户并在我的.plist中获得权限时,我的应用程序崩溃了,这是我的代码。importUIKitimportMapKitimportCoreLocationclassmapClass:UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{varlocation4=CLLocation()varlm=CLLocationManager()@IBOutletvarpropMapa:MKMapView!lm.delegate=selflm.desiredAccuracy=kCLLocation

ios - 条件绑定(bind)的初始化程序必须具有 Optional 类型,而不是 'NSManagedObjectContext

我收到此错误消息:“条件绑定(bind)的初始化程序必须具有可选类型,而不是‘NSManagedObjectContext’。我不确定如何修复此错误。我认为错误在于“iflet”。ifletmanagedObjectContext=(UIApplication.sharedApplication().delegateas!AppDelegate).managedObjectContext{restaurant=NSEntityDescription.insertNewObjectForEntityForName("Restaurant",inManagedObjectContext:m

ios - 设置可选文本时,UIlabel 文本不显示 "optional"字?

我一直在使用可选的很多。我已经将一个字符串变量声明为可选的varstr:String?现在我在这个变量中设置一些值作为str="hello"。现在如果我打印这个可选的而不展开然后它打印为Optional("hello")但是如果我将标签上的文本设置为self.label.text=str那么它只会将值显示为hello请解释为什么它不显示标签上的文字Optional("hello") 最佳答案 UILabel的text属性是可选的。UILabel足够智能,可以检查text属性的值是否设置为nil或非nil值。如果它不是nil,那么它会

Swift 在可空性可用之前使用 Implicitly Unwrapped Optional

在Apple关于可空性的博客中,他们提到了这一点:"...inSwiftthere’sastrongdistinctionbetweenoptionalandnon-optionalreferences,e.g.NSViewvs.NSView?,whileObjective-CrepresentsbothsofthesetwotypesasNSView*.BecausetheSwiftcompilercan’tbesurewhetheraparticularNSView*isoptionalornot,thetypeisbroughtintoSwiftasanimplicitlyunw

ios - Swift 通用变量 fatal error : unexpectedly found nil while unwrapping an Optional value

我正在尝试实现一个简单的通用方法:funcfindMax(numbers_array:[T])->(min_tuple:T,max_tuple:T){varmax_number:T?varmin_number:T?foriinnumbers_array{if(max_numberi){min_number=i}}return(min_number!,max_number!)}我正在尝试访问这样的方法:letresult=findMax([3.4,5,-7,-7.8])但是每当我运行这段代码时,我都会收到以下错误:fatalerror:unexpectedlyfoundnilwhileu