gpt4 book ai didi

swift - 什么是具体化字符串,而不是 null

转载 作者:行者123 更新时间:2023-11-30 10:50:45 25 4
gpt4 key购买 nike

我正在读这篇文章blog并且无法理解下面具体化字符串的含义。

Both Swift and Kotlin have incorporated the concept of nullable types. The fact that a variable can contain a null becomes part of the type of that variable. A variable of type String cannot contain a null; it can only contain a reified String. On the other hand, a variable of type String? has a nullable type and can contain a null.

The rules of the language insist that when you use a nullable variable, you must first check that variable for null. So if s is a String? then var l = s.length() won’t compile. Instead you have to say var l = s.length() ?: 0 or var l = if (s!=null) s.length() else 0.

最佳答案

具体化的意思是“使(抽象的东西)更加具体或真实”。这里具体化的字符串意味着如果字符串为空,则该字符串输出包含0或任何其他值。

关于swift - 什么是具体化字符串,而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54605581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com