ios - substring of the first 4 characters from a textField in Swift 4 -


i'm trying create substring of first 4 characters entered in textfield in swift 4 on ios app.

since change swift 4 i'm struggling basic string parsing.

so based on apple documentation i'm assuming need use substring.index function , understand second parameter (offsetby) number of characters create substring with. i'm unsure how tell swift start @ beginning of string.

this code far:

  let postcode = textfieldpostcode.text    let newpostcode = postcode?.index(startatthebeginingofthestring, offsetby: 4) 

i hope explanation makes sense, happy answer questions on this.

thanks,

in swift 4 can use

let string = "hello world" let first4 = string.prefix(4) // hell 

the type of result new type substring behaves similar string. if first4 supposed leave current scope – example return value of function – it's recommended create string explicitly:

let first4 = string(string.prefix(4)) // hell 

see se 0163 string revision 1


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -