- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在自学 Swift 编程,但遇到了一条我不知道如何摆脱的错误消息。错误消息如下:
warning: expression implicitly coerced from 'String?' to Any
据我了解,此警告消息是由于我在代码中调用的 print() 函数需要“Any”类型的参数,而是传递“String”类型的参数,但我不确定如何抑制或防止此错误或消息。我的程序是一个简单的 Magic 8 Ball 命令行程序:
import Foundation
// Greet user, and then prompt he or she to ask his or her question
print("Welcome to Magic 8 Ball!")
print("What is your question? ")
var question = readLine()!
// Initialize a dictionary named 'answer', this array will contain all 20 of the
// standard Magic 8 Ball responses.
let answer = ["zero": "It is certain.",
"one": "It is decidedly so.",
"two": "Without a doubt.",
"three": "Yes, definitely.",
"four": "You may rely on it.",
"five": "As I see it, yes.",
"six": "Most likely.",
"seven": "Outlook good.",
"eight": "Yes.",
"nine": "Signs point to yes.",
"ten": "Reply hazy, try again.",
"eleven": "Ask again later.",
"twelve": "Better not tell you now.",
"thirteen": "Cannot predict now.",
"fourteen": "Concentrate and ask again.",
"fifteen": "Don't count on it.",
"sixteen": "My reply is no.",
"seventeen": "My sources say no.",
"eightteen": "Outlook not so good.",
"nineteen": "Very doubtful."]
// Generate a random number between 0 and 19.
// We will use this number to choose chick answer to show the user
var randomUInt32:UInt32 = arc4random_uniform(20)
// Convert UInt32 datatype to Int
var randomInt: Int = Int(randomUInt32)
switch (randomInt) {
case 0:
print(answer["zero"]) // tell fortune
case 1:
print(answer["one"]) // tell fortune
case 2:
print(answer["two"]) // tell fortune
case 3:
print(answer["three"]) // tell fortune
case 4:
print(answer["four"]) // tell fortune
case 5:
print(answer["five"]) // tell fortune
case 6:
print(answer["six"]) // tell fortune
case 7:
print(answer["seven"]) // tell fortune
case 8:
print(answer["eight"]) // tell fortune
case 9:
print(answer["nine"]) // tell fortune
case 10:
print(answer["ten"]) // tell fortune
case 11:
print(answer["eleven"]) // tell fortune
case 12:
print(answer["twelve"]) // tell fortune
case 13:
print(answer["thirteen"]) // tell fortune
case 14:
print(answer["fourteen"]) // tell fortune
case 15:
print(answer["fifteen"]) // tell fortune
case 16:
print(answer["sixteen"]) // tell fortune
case 17:
print(answer["seventeen"]) // tell fortune
case 18:
print(answer["eightteen"]) // tell fortune
case 19:
print(answer["nineteen"]) // tell fortune
default:
print("ERROR: PC LOAD LETTER") // tell fortune
}
程序输出:
Optional("Yes, definitely.")
注意:我知道字典对于这个特定的程序来说并不是一个很好的选择,但我正在阅读一本关于 Swift 的书,所以我只是在处理所有不同的数据类型和数据结构。通读这本书。
最佳答案
当您调用 answer["one"]
之类的电话时,您正在调用 Dictionary.subscript(_:)
。这会返回一个可选值(在本例中为 String?
,又名 Optional<String>
),因为据编译器所知,给定键 ( "one"
) 可能没有值。
print
,正式名称为 print(_:separator:terminator:)
取任意数量的Any
论据。当您通过String?
时(字典下标的值),您将其隐式转换为 Any
,这掩盖了该值确实是可选的这一事实。该警告建议您明确这一点,如果模糊可选性确实是您想要的(例如 print(array["one"] as Any)
)。通过像这样明确地表达出来,你会说“是的,我知道这个强制转换为 Any
掩盖了可选性,而这就是我想要的。”
您更大的问题是此代码需要主要代码审查:
// Greet user, and then prompt he or she to ask his or her question
print("Welcome to Magic 8 Ball!")
清晰地打印出问候语。print("What is your question?")
显然是在问问题。print(answer["four"]) // tell fortune
print("ERROR: PC LOAD LETTER") // tell fortune
"ERROR: PC LOAD LETTER"
这是一个奇怪的算命。array
实际上是一个Dictionary<String, String>
(又名 [String: String]
)。Array<String>
(又名 [String]
)!以下是我建议的实现方式:
import Foundation
let magic8BallAnswers = [
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes, definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful.",
]
extension Array {
func randomElement() -> Element? {
if self.isEmpty { return nil }
return self[Int(arc4random_uniform(UInt32(self.count)))]
}
}
print("Welcome to Magic 8 Ball!")
print("What is your question?")
var question = readLine()!
// Forceunwrap is justified here, because we know the array is not empty.
let answer = magic8BallAnswers.randomElement()!
print("Magic 8 Ball says: \(answer)")
关于快速警告 : expression implicitly coerced from 'String?' to Any,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50867469/
这是我的查询: INSERT INTO location_province(name, country) SELECT child.name ,location_country.id
尽管我看到 C++ 教程中广泛使用术语 implicit casting 来表示这样一个事实,即当您将某种类型分配给另一种类型时,类型的转换将自动(隐式)完成,但我经常听说应该叫implicit co
所有表格都在 utf_unicode_ci 中。 我这样做是为了检查 SELECT table_schema, table_name, column_name, character_set_name,
def MyFun(result: ListBuffer[(String, DateTime, List[(String, Int)])]): String = { val json = (r
我刚刚在 Postgres 中创建了一个表,并收到一条通知消息,我不完全理解隐式索引和序列。如有任何澄清,我们将不胜感激。 my_database=# CREATE TABLE sites my_da
我正在关注 Fernando Villalobos 的 React.js - A guide for Rails developers AirPair 教程。 这里的目标是使用 Rails 和 Rea
当我选择一个选项时,我有通过多选列表在 dbase 中搜索的代码我有这个错误: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (ut
我正在尝试使用 execl 调用来执行 kernel-space-driver (driver.c) 中的二进制文件此时(从第 850 行开始): if (!retval) {
我正在尝试在内核 3.13 上编译内核模块,但出现此错误: error: implicit declaration of function 'create_proc_read_entry' [-Wer
我检查了数据库表,发现它在 latin1_swedish_ci 中,所以我将其更改为 utf8_general_ci 然后我将排序规则从 latin1_swedish_ci 更改到所有字段的 utf8
尝试通过 MySQL 中的存储过程进行选择时出现以下错误 Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_ge
我收到了这个错误; Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT)
我需要您帮助确定为什么会出现此错误 Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT
我收到了这个错误; Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT)
MySql 上的错误信息: Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) fo
在我的删除服务器上执行 MySQL 中的存储过程时,如下所示: CREATE OR REPLACE PROCEDURE `SetNextPage`( IN `inRefNo` varchar(
我正在尝试为 Kali linux 2.0 安装我的 Alfa AWUS036ACH 适配器 我已经修复了之前的错误,但现在我被困在这里了。这是错误我正在接收。 os_dep/linux/rtw_an
我们正在使用以下存储过程,并且所有提到的表都使用“Collation = utf8_general_ci”,但我们仍然收到此错误: Error Code: 1267. Illegal mix of
我想让我的 User 表的 password 列在 mysql 中区分大小写。 表的说明如下: /*Table: mst_user*/ FIELD TYPE
我对这一切都很陌生,正在尝试在内核版本为 3.10.0-957.el7.x86_64 的虚拟机上编译程序。但我收到此错误: /home/../../../isr_demux.c: In functio
我是一名优秀的程序员,十分优秀!