A
A
Andrew2018-03-21 19:37:39
Swift
Andrew, 2018-03-21 19:37:39

Why is String not being converted to Int?

Greetings!
There is a task: to create a class that will store huge integers (up to 40 elements) using an array.
In the class, I have 2 properties: an array and a variable of type Boolean, which shows a negative number or not.

var GeneralMassive = [Int](repeating: 0, count: 40) //Создаем массив 40 целых чисел, забитый нулями.
var isMinus : Bool = false // Опциональная переменная

Next, I write an init that takes a string, then strips off the last character from it and converts it from a String to an Int and adds it to the end of the GeneralMassive[39] array.
public init (integer: String) {
        var inStr : String 
        var lastStr : String
        var lastInt : Int
        
        inStr = integer //Приравниваем входящую строку к переменной inStr
        
        lastStr = String(inStr.removeLast()) //Получаем последний символ этой строки (тип Character) и конвертируем его в String
        lastInt = Int!(lastStr)   //Пытаемся конвертировать String в Int
//Добавление в массив

    }

By itself, this should all be in a cycle. The code is given as an example.
In the last line I tried to put "?" and "!" - sense 0.
The error "Cannot convert value of type 'String' to expected argument type 'Int'" constantly crashes.
I write all this in the Playground as a separate file.
What am I doing wrong? Why can't I convert from one type to another?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2018-03-21
@racshaser

Problem solved.
It:
Must be
lastInt = Int(lastStr) !

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question