Ios 零合并运算符的左侧'';具有非可选类型';字符串';,所以右边永远不用

Ios 零合并运算符的左侧'';具有非可选类型';字符串';,所以右边永远不用,ios,swift,optional,forced-unwrapping,Ios,Swift,Optional,Forced Unwrapping,我有下面的代码,我正试图使用它初始化一个变量并对其执行一些操作 let formattedPointsValue: String? self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name) ?? .none 然而,我得到了警告 nil合并运算符“??”的左侧具有非可选类型“字符串”,因此

我有下面的代码,我正试图使用它初始化一个变量并对其执行一些操作

let formattedPointsValue: String?
self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name) ?? .none
然而,我得到了警告

nil合并运算符“??”的左侧具有非可选类型“字符串”,因此永远不会使用右侧

当我删除
时。无
我的项目运行正常,没有问题,但是当我运行单元测试时,我得到一个错误

致命错误:在展开可选值时意外发现nil

我发现解决这个问题的唯一方法就是使用这个代码

if let unformattedValue = model.pointUnitsEarned {
    self.formattedPointsValue = unformattedValue.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name)
} else {
    self.formattedPointsValue = nil
}
我想了解为什么这样的事情会起作用:

let legend: String?
self.legend = model.pointsCategory ?? .none
但这失败了:

let formattedPointsValue: String?
self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+" "+"model.name".localized(in: .name) ?? .none

.name属性不是可选的,这就是出现错误的原因。name属性在型号中是可选的,只有在左侧的值可以是nil时才有用

Swift告诉您,它永远不能为
nil
,因此将永远不会使用右侧的值。您也可以删除
:String?

model.pointsCategory
的值是可选的,因此可能是
nil
,这就是为什么它适用于此,并且不会给您任何错误或警告


nil合并操作符的要点是,如果一个值不存在,就可以返回到默认值,如果总是存在一个值,就没有必要使用它,所以这就是为什么会收到警告

我认为您对
??
操作符有点困惑

你认为这是可行的,因为
legend
是可选的,不是吗

let legend: String?
self.legend = model.pointsCategory ?? .none
这不是原因上述操作有效的实际原因是因为
model.pointsCategory
是可选的。它与
=
左侧的内容无关。这都是关于
左边的操作数的。上面是这样说的:

如果
model.pointsCategory
不为零,则将
self.legend
设置为
model.pointsCategory
。如果为nil,则将
self.legend
设置为
.none

在这种情况下:

self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+
    " "+"model.name".localized(in: .name) ?? .none
由于
“model.name”.localized(in:.name)
不是可选的,因此它不会编译。我怀疑你打算在这里做的可能是:

if self.formattedPointsValue == nil {
    self.formattedPointsValue = .none
} else {
   self.formattedPointsValue = model.pointUnitsEarned.stringValueWithWhiteSpaceThousandSeperator()+
        " "+"model.name".localized(in: .name)
}

model.pointUnitsAnned.StringValueWithWhiteSpaceTournandSeparator()+“”+“model.name”。本地化(in:.name)==>应返回可选字符串的值

例如: 下面的代码将得到与您收到的相同的错误

let nickName: String = "k"
let fullName: String? = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"
但是下面的代码可以正常工作

let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"

因此结论是,合并运算符“??”将从右向左替换或使用默认值。不是从左到右。

name属性是来自objective c类的属性。在这个项目中,我与OC和swift一起工作。因此,我无法将属性更改为optional当我尝试添加括号时,仍然会出现以下错误:nil合并运算符“??”的左侧具有非可选类型“String”,因此,如果您具有声明为非null的Objective-C属性,但同时可以返回nil,则永远不会使用右侧,那么你就有麻烦了。那么基本上这段代码永远不会工作?让formattedPointsValue:String?self.formattedPointsValue=model.pointUnitsEarned.stringvaluewithwhitespacethousandseparator()+“”+“model.name”。本地化(in:.name)。none@Erent那么你明白
现在实际做什么了吗?@Sweeper为什么这个代码有效:-var demooptional:String?demooptional=“AB”??“B”@Amey它没有。它产生了一个警告。Ohk没有看到Thanx