2

First, input data using keyboard:

    var fh = NSFileHandle.fileHandleWithStandardInput()

    println("Please input first number")
    let num1 = fh.availableData
    println("Please input second number")
    let num2 = fh.availableData

After accept the keyboard input, I want to calculate

    num1+num2

and print out. But I don't know how to convert num1, num2 to Integer.

이국화
  • 809
  • 1
  • 9
  • 8

5 Answers5

2

It depends which swift you are using, you can see how to do it for 1.x and 2.0: Get integer value from string in swift
In 2.0 you can initialize an int with a string:

var stringNumber = "1234"
var numberFromString = Int(stringNumber)
Community
  • 1
  • 1
azahar
  • 48
  • 7
0

To print it out do it like print("My result: \(num1 + num2)")

regetskcob
  • 1,172
  • 1
  • 13
  • 35
0

The type that NSFileHandle gives you for num1 and num2 is NSData.

You can't do math on NSData, you have to first convert the data to numbers.

To transform this data into integers, we're doing two steps: first convert the data to an NSString, then convert the string to an Integer. After that, we can sum the two values.

We're using the integerValue method of NSString:

let fh = NSFileHandle.fileHandleWithStandardInput()
print("Please input first number")
let num1 = fh.availableData
print("Please input second number")
let num2 = fh.availableData
if let  numString1 = NSString(data: num1, encoding: NSUTF8StringEncoding),
        numString2 = NSString(data: num2, encoding: NSUTF8StringEncoding) {
    let val1 = numString1.integerValue
    let val2 = numString2.integerValue
    print("\(val1) + \(val2) = \(val1 + val2)")
}

Result:

Please input first number
33
Please input second number
42
33 + 42 = 75

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • I tried to arrange your code, but I got an error, would you please tell me the reason? let numString1 = NSString(data: num1, encoding: NSUTF8StringEncoding) let numString2 = NSString(data: num2, encoding: NSUTF8StringEncoding) if(numString1 != nil)&&(numString2 != nil){ let val1 = numString1.integerValue let val2 = numString2.integerValue print("\(val1) + \(val2) = \(val1 + val2)") } – 이국화 Apr 25 '16 at 03:33
  • My example works. Your modifications make it not work anymore. Observe the differences and you'll know the reasons. – Eric Aya Apr 25 '16 at 08:10
0

This is what i did in latest version of Swift:

`

class Input2 {
    func inputString() -> String {
        let keyboard = FileHandle.standardInput
        let inputData = keyboard.availableData
        return String(data: inputData, encoding: .utf8)!
    }

    func inputInt() -> Int {
        let keyboard = FileHandle.standardInput
        let inputData = keyboard.availableData
        let str = String(data: inputData, encoding: .utf8)!
        let i = Int(str.replacingOccurrences(of: "\n", with: ""))!
        return i
    }
}

var inp = Input2()
print("Please enter your name")
var name:String = inp.inputString()
print("line: \(name)")
//var i = Int(name.replacingOccurrences(of: "\n", with: ""))!
print("Enter a number:")
var i = inp.inputInt()
print(i)

`

ketav
  • 1
-1

You can print or store the sum by :

var fh = NSFileHandle.fileHandleWithStandardInput()
print("Please input first number")
let data1 = fh.availableData
let stringvalue1 = NSString(data: data1, encoding:NSUTF8StringEncoding) as!    String // getting as string value
let num1 = Int(stringvalue1.stringByReplacingOccurrencesOfString("\n",    withString: ""))! // converting string to Int
print("Please input second number")
let data2 = fh.availableData
let stringvalue2 = NSString(data: data2, encoding:NSUTF8StringEncoding) as! String
let num2 = Int(stringvalue2.stringByReplacingOccurrencesOfString("\n", withString: ""))!
let sum = num1 + num2
print("sum is \(sum)")
user3815344
  • 243
  • 1
  • 21
  • 1
    @MartinR sorry. I just thought it may work. I didn't compiled before. Now I compiled and changed my answer with working code. Thanks you. – user3815344 Apr 22 '16 at 09:51