top of page

SWIFT FOR VCE SOFTWARE DEVELOPMENT

4. Validation

One of the key components of this course is going to be validating user input.




You will need this for SAT’s and SACS. Whilst it is only specifically taught in U4O1, it really is something we need to look at now. we will start with the easiest type, and that is the keyboard that we allow our users to use to input data.


1. UI Design

Choose the right keyboard for the data you want the user to input. We can make sure that users only use numbers when on a number field and so on.


2. Existence Check

Existence checks make sure that there is actually data in the variable. In Swift we have had the ability to use optionals, but programmatically we can check to see if data actually exists.

let strToValidate = "Hello"

let fltToValidate = 110

//String Validation

//Isn't empty

if (strToValidate == ""){

print("String Is Empty")

}


3. Type Check

Type checking is used to ensure that the data entered is the correct data type. In Swift, everything that is put into the UI by the user is a string and we parse it into the correct data type.


4. Range Check

Range checking is to make sure that data inputted falls within an expected range of values. It could mean that it i between 1 and 10, or it could be letters only. Here are some examples

Don’t allow numbers

//Check if a string contains these Characters || Is case sensitive use .lower()

var chsIllegalCharacters = CharacterSet (charactersIn: "1234567890") // Defines the characters not to be in the string

if (strToValidate.rangeOfCharacter(from: chsIllegalCharacters) != nil){ // Checks if any of the forbiden characters are in the string

print("No Numbers")

}


Don't allow Letters

//Check if a string doesn't contain these characters

var chsCompolseryCharacters = CharacterSet (charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZ") // Defines the characters that need to be in the string

if (strToValidate.rangeOfCharacter(from: chsCompolseryCharacters) == nil) { // Checks if at least one of the required characters are being used

print("Need a capital Letter")

}


Only include these characters in string

//Check if a string uses letters other than these || Similar to the first method but you enter the valid characters instead of the forbidden ones.

var chsOnlyAllowedCharacters = CharacterSet (charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuzwxyz") // Defines the characters that need to be in the string

chsOnlyAllowedCharacters.invert() //Inverts the character set. It now contains the forbidden characters

if (strToValidate.rangeOfCharacter(from: chsOnlyAllowedCharacters) != nil){ // Checks if any of the forbiden characters are in the string

print("Forbidden Character Found")

}


Practical

Add existence checking to your Gloria Chino’s App. It would be nice if it could be done with an alert.


There is a large number of input validation that can be used within Swift as there is in any programming language. This should give you enough to get started. If you need more specific ones then I suggest you use your google skills.




Comments


bottom of page