Converting String to Int with Swift

前端 未结 30 1275
小鲜肉
小鲜肉 2020-11-22 06:59

The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the value

相关标签:
30条回答
  • 2020-11-22 07:06

    I recently got the same issue. Below solution is work for me:

            let strValue = "123"
            let result = (strValue as NSString).integerValue
    
    0 讨论(0)
  • 2020-11-22 07:06

    My solution is to have a general extension for string to int conversion.

    extension String {
    
     // default: it is a number suitable for your project if the string is not an integer
    
        func toInt(default: Int) -> Int {
            if let result = Int(self) {
                return result
            }
            else {
                return default  
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 07:07
    //  To convert user input (i.e string) to int for calculation.I did this , and it works.
    
    
        let num:Int? = Int(firstTextField.text!);
    
        let sum:Int = num!-2
    
        print(sum);
    
    0 讨论(0)
  • 2020-11-22 07:07

    As of swift 3, I have to force my #%@! string & int with a "!" otherwise it just doesn't work.

    For example:

    let prefs = UserDefaults.standard
    var counter: String!
    counter = prefs.string(forKey:"counter")
    print("counter: \(counter!)")
    
    
    var counterInt = Int(counter!)
    counterInt = counterInt! + 1
    print("counterInt: \(counterInt!)")
    
    OUTPUT:
    counter: 1
    counterInt: 2
    
    0 讨论(0)
  • 2020-11-22 07:09

    In Swift 4:

    extension String {            
        var numberValue:NSNumber? {
            let formatter = NumberFormatter()
            formatter.numberStyle = .decimal
            return formatter.number(from: self)
        }
    }
    let someFloat = "12".numberValue
    
    0 讨论(0)
  • 2020-11-22 07:09

    Swift 3.0

    Try this, you don't need to check for any condition I have done everything just use this function. Send anything string, number, float, double ,etc,. you get a number as a value or 0 if it is unable to convert your value

    Function:

    func getNumber(number: Any?) -> NSNumber {
        guard let statusNumber:NSNumber = number as? NSNumber else
        {
            guard let statString:String = number as? String else
            {
                return 0
            }
            if let myInteger = Int(statString)
            {
                return NSNumber(value:myInteger)
            }
            else{
                return 0
            }
        }
        return statusNumber
    }
    

    Usage: Add the above function in code and to convert use let myNumber = getNumber(number: myString) if the myString has a number or string it returns the number else it returns 0

    Example 1:

    let number:String = "9834"
    print("printing number \(getNumber(number: number))")
    

    Output: printing number 9834

    Example 2:

    let number:Double = 9834
    print("printing number \(getNumber(number: number))")
    

    Output: printing number 9834

    Example 3:

    let number = 9834
    print("printing number \(getNumber(number: number))")
    

    Output: printing number 9834

    0 讨论(0)
提交回复
热议问题