2

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)

I tried:

let line = readLine()
print(line)

But I get this error:

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?

I'll really appreciate you help

Hamish
  • 78,605
  • 19
  • 187
  • 280
user2924482
  • 8,380
  • 23
  • 89
  • 173

3 Answers3

4
let line = readLine()!

print(line)

//To Array, should work. Wrote it real quick
let array = readLine()!.characters.split(" ").map( { String($0)! } ) 
impression7vx
  • 1,728
  • 1
  • 20
  • 50
  • 2
    Please don't recommend the use of `!` without explaining the hazards and crashes that might result. Run your code and type `^d` on the command line and see what happens. – rmaddy Feb 25 '17 at 01:36
  • Well readLine is suppose to return`String?`, BUT, it needs a default value , so because of readLine() and HackerRanks compiler, it returns `Any` but HackerRank says this cannot happen. So you use `!` to turn it into a string. It works. – impression7vx Feb 25 '17 at 01:38
  • 1
    I have no interest in HackerRank (I have no idea what it is). `readLine` can return `nil`. Force unwrapping a nil causes a crash. That's what I'm pointing out. Suggesting inappropriate force unwrapping to a someone just learning Swift is dangerous. – rmaddy Feb 25 '17 at 01:41
  • 1
    LOL! On hackerrank, it is GUARANTEED (as they provide the input, and you do NOT put it in a loop unless required) as you HAVE to read their input as COMMAND LINE, goo look up before talking smack – impression7vx Feb 25 '17 at 01:42
  • 2
    You're focused on some website. I'm talking more general Swift usage. Not everyone who looks at this question in the future and tries to makes sense of the question and answer will use some old website. People will try it on their computer using their favorite editor and compiler. They will run the code on their computer. My comments address the general issue. I'm simply showing the bigger picture to help a more general user. – rmaddy Feb 25 '17 at 01:45
  • @impression7vx, this works at least is not crashing. In the print I get "5 6 7" .My question is how can cast line as array of Integers ? – user2924482 Feb 25 '17 at 01:46
  • He asked in specific to HackerRank.SO says answer the question and don't go offbase. Was trying to stick to rules – impression7vx Feb 25 '17 at 01:46
  • u can find problem solution here https://stackoverflow.com/questions/38050506/how-to-get-multiple-lines-of-stdin-swift-hackerrank/38211039 – Yestay Muratov Mar 02 '18 at 18:14
0

If you're going to convert a String from readLine to an array of Integers, this works:

let array = readLine()!.split{ $ == " " }.map{ Int(String($0)) }

You cannot:

let array = readLine()!.split{ $ == " " }.map{Int($0)}

Because $0 would be an optional character array type (I think), which Int does not parse.

NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
0

I used below line and worked for me.

 var arr = readLine()!.components(separatedBy: " ").map{(a: String)->(Int) in
    return Int(a)!
}
kumar123
  • 791
  • 7
  • 21