Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift选择零支票_Ios_Swift - Fatal编程技术网

Ios Swift选择零支票

Ios Swift选择零支票,ios,swift,Ios,Swift,因此,我无法在Swift中为以下语法找到1行,这让我发疯: if lastProfile == nil || lastProfile.id == profile.id { useProfile(profile) lastProfile = profile } 现在看,我可以把它链起来,但我最终还是会有2-3个如果。我可以把它打包,但后来我又得到了2-3个如果。。。有没有可能一下子就做到这一点 编辑: 我的同事找到了另一种选择(尽管我们同意): 如果将lastPr

因此,我无法在Swift中为以下语法找到1行,这让我发疯:

if lastProfile == nil || lastProfile.id == profile.id {
        useProfile(profile)
        lastProfile = profile
}
现在看,我可以把它链起来,但我最终还是会有2-3个如果。我可以把它打包,但后来我又得到了2-3个如果。。。有没有可能一下子就做到这一点

编辑:

我的同事找到了另一种选择(尽管我们同意):


如果将
lastProfile
声明为
let lastProfile:Profile?=,是否有比上述更好的方法,例如,id声明为可选,您也可以将其用作:

if let lastProfileId = lastProfile?.id { 
    // lastProfile is not nil and id is not nil
}
else {
    // lastProfile is nil or lastProfile.id is nil
}

它被称为可选链接,您可以在swift上了解它。

解决方案只需
即可:

if lastProfile == nil || lastProfile?.id == profile.id {
    print("true")
    lastProfile = profile
}

当lastProfile为nil或lastProfile与profile具有相同id时,此选项将打印“true”。否则,它将不打印任何内容。

I必须为稠密。第一个示例有什么问题,第二个如何更好?巧妙的一行程序使代码更难阅读/理解。接下来,您或其他人将必须查看此代码并理解此代码的意图。我已经发布了一个答案,但我读的问题越多,我就越不理解问题的实际含义……:)如果lastProfile==nil | | lastProfile!。id==profile.id
,因为如果
lastProfile
nil
“找出一条直线”似乎是修改某些内容的错误原因,则短路将阻止强制展开。为什么不将功能包装到一个函数中,而不是
func shouldSetProfile(profile){…}
如果是nil,我会输入block吗?如果没有。。。那么不,这不是更好。对不起,我误解了你的问题,上面的答案是很好的JBOIS是好的。谢谢你试图帮助这个白痴:)(顺便说一句,我没有投反对票。)我也没有。你的回答让我想到Swift中的“where”有什么好处。这门语言仍然有一些奇迹。该死。由于几个月前的一个错误,我无法使用==nil,所以我甚至没有再往下看。。。
if let recentProfile = latestProfile where recentProfile.voieId != profile.voiceId {
    useProfile(profile)
    lastProfile = profile
}
if lastProfile == nil || lastProfile?.id == profile.id {
    print("true")
    lastProfile = profile
}