简体   繁体   English

检查 NSSize 是否为零 - 比较“NSSize”类型的非可选值?

[英]Check if NSSize is nil - Comparing non-optional value of type 'NSSize'?

I have a NSSize variable我有一个 NSSize 变量

var originalselectedimagesize:NSSize

if(originalselectedimagesize == nil)
{
}

I'm trying to check if NSSize is set?我正在尝试检查是否设置了 NSSize? But i keep getting the following warning.How can i check if the value of NSSize is changed?但我不断收到以下警告。如何检查 NSSize 的值是否已更改?

h(aka 'CGSize') to 'nil' always returns false

Because from the declaration, NSSize is not an optional.因为从声明来看,NSSize 不是可选的。 So checking a non-optional value for nil always return false.因此,检查 nil 的非可选值总是返回 false。

In case if you have the following, then you won't get the warning.如果您有以下情况,那么您将不会收到警告。

var originalselectedimagesize:NSSize?

if(originalselectedimagesize == nil)
{
}

You can declare originalselectedimagesize as NSSize?您可以将originalselectedimagesize声明为NSSize? ( Optional type of NSSize ), and set it to nil . NSSizeOptional类型),并将其设置为nil Then, you can check if it has value like this:然后,您可以检查它是否具有如下值:

var originalselectedimagesize: NSSize? = nil

// what ever...

// check for value
if let size = originalselectedimagesize {

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将“JSON”类型的非可选值与“nil”进行比较总是返回 true。 有什么建议可以解决这个问题吗? - Comparing non-optional value of type 'JSON' to 'nil' always returns true. Any suggestion to fix this? 将 'Bool' 类型的非可选值与 'nil' 进行比较总是返回 true - Comparing non-optional value of type 'Bool' to 'nil' always returns true 将非可选的 Any 与 nil 进行比较总是错误的? - Comparing non-optional Any to nil is always false? 在Swift中检查非可选值是否为nil的最佳方法 - Best way to check non-optional values for nil in Swift 致命错误:解开可选值时意外发现nil(无法强制解开非可选类型'String'的值) - fatal error: unexpectedly found nil while unwrapping an Optional value (cannot force unwrap value of non-optional type 'String') Swift:测试非可选值是否为零/未设置 - Swift: test if a non-optional value is nil / not set 无法强制展开非可选类型的值,但打印“可选” - Cannot force unwrap value of non-optional type but prints with "optional" 不能对“Auth”类型的非可选值使用可选链 - Cannot use optional chaining on non-optional value of type 'Auth' 不能对类型为“[Int]”的非可选值使用可选链 - Cannot use optional chaining on non-optional value of type '[Int]' 不能对“ViewController”类型的非可选值使用可选链接 - Cannot use optional chaining on non-optional value of type 'ViewController'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM