Swift:HackerRank readLine一个Int数组 [英] Swift: HackerRank readLine an array of Int
本文介绍了Swift:HackerRank readLine一个Int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在玩HackerRank.我需要从行到整数数组读取的问题之一:
I'm playing with HackerRank. One of the problems I need to read from line to arrays of Integers:
A=(1,2,3)
B=(1,4,7)
我尝试过:
let line = readLine()
print(line)
但是我得到这个错误:
solution.swift:2:7: warning: expression implicitly coerced from 'String?' to Any
print(line)
^~~~
solution.swift:2:7: note: provide a default value to avoid this warning
print(line)
^~~~
?? <#default value#>
solution.swift:2:7: note: force-unwrap the value to avoid this warning
print(line)
^~~~
!
solution.swift:2:7: note: explicitly cast to Any with 'as Any' to silence this warning
print(line)
^~~~
as Any
你们中的任何人都知道我应该如何读取行并获取数组吗?
Any of you knows how I suppose to read line and get the array?
非常感谢您的帮助
推荐答案
let line = readLine()!
print(line)
//To Array, should work. Wrote it real quick
let array = readLine()!.characters.split(" ").map( { String($0)! } )
这篇关于Swift:HackerRank readLine一个Int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文