• Saltar a la navegación principal
  • Saltar al contenido principal

CARLOSZR.COM

Mi blog personal, CarlosZR

  • Blog
  • Contacto
  • Curso de SwiftUI por CarlosZR

Swift and Beach / 08/11/2021

Swift String to Int

Swift String to Int

In this case we have an integer within a string. Well, it can be converted to an integer simply by using the integer initializer. Let’s look at the example code:

// Using integer's initializer.

let string1 = "328"
let int1 = Int(string1)

Strings can contain anything other than a number, anything, for example «Palm» instead of «328»: the Int initializer then returns an optional integer. So we better put a value. In this case we can then put this example code:

// Using integer's initializer.

let string1 = "328"
let int1 = Int(string1) ?? 0
swft string to int
Swift String to Int

You may see other ways to do this, but check that they work for you because there is deprecated code.

Here you can see what the opposite step would be like, going from Int to String.

I hope this tutorial has helped you. Do not forget to subscribe to the YouTube channel to continue learning Swift. We continue …

Publicado en: Swift and Beach

Interacciones con los lectores

Deja una respuesta Cancelar la respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Carlos ZR

  • Sígueme en Twitter
  • Política de Cookies
  • Política de Privacidad
  • Aviso Legal

Utilizamos cookies para ofrecerte la mejor experiencia en nuestra web.

Puedes aprender más sobre qué cookies utilizamos o desactivarlas en los ajustes.

CARLOSZR.COM
Powered by  GDPR Cookie Compliance
Resumen de privacidad

Esta web utiliza cookies para que podamos ofrecerte la mejor experiencia de usuario posible. La información de las cookies se almacena en tu navegador y realiza funciones tales como reconocerte cuando vuelves a nuestra web o ayudar a nuestro equipo a comprender qué secciones de la web encuentras más interesantes y útiles.

Cookies estrictamente necesarias

Las cookies estrictamente necesarias tiene que activarse siempre para que podamos guardar tus preferencias de ajustes de cookies.

Si desactivas esta cookie no podremos guardar tus preferencias. Esto significa que cada vez que visites esta web tendrás que activar o desactivar las cookies de nuevo.