Category Archives: Swift

Special characters that can be included in a Swift String ?

By | January 5, 2018

Special characters can be included in string literals using the following escape sequences: The following example shows how to use a few string literals: Swift 4 Escape sequence Meaning \0 Null Character \\ \character \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab \’ Single Quote \” Double Quote \000 Octal… Read More »

How to Count characters in a String in Swift 4?

By | December 20, 2017

To retrieve a count of the characters in a string, call the global countElements function and pass in a string as the function’s sole parameter: [java] let sampleString = "Koala, Snail, Penguin, Dromedary" print("sampleString has \(countElements(sampleString)) characters") [/java] prints “sampleString has 32 characters” Note : Different Unicode characters and different representations of the same Unicode character can require… Read More »

Hex String to UIColor in iOS Swift

By | December 10, 2017

Dear Friends, In Today’s post I will show you how to convert a simple hex color string to UIColor in Swift 3. Code [java] static func hexStringToUIColor (hex:String) -> UIColor { var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() if (cString.hasPrefix("#")) { cString.remove(at: cString.startIndex) } if ((cString.characters.count) != 6) { return UIColor.gray } var rgbValue:UInt32 = 0 Scanner(string: cString).scanHexInt32(&rgbValue) return UIColor(… Read More »

Simple Alert with two buttons in iOS Swift with callback

By | December 1, 2017

Dear Friends, Today I will show you how to create a simple alert with two buttons and how to write a callback for each button. The code is simple. Its written for Swift 3. [java] func showErrorAlert(VC vc : UIViewController, OKTitle okTitle : String, CancelTitle cancelTitle : String, Message message : String, Title title : String) -> Void… Read More »

Convert Hex String to UIColor in Swit 3

By | October 15, 2017

Here is a simple ulility function to convert a hex color string to UIColor in iOS in Swift 3. [java] static func hexStringToUIColor (hex:String) -> UIColor { var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() if (cString.hasPrefix("#")) { cString.remove(at: cString.startIndex) } if ((cString.characters.count) != 6) { return UIColor.gray } var rgbValue:UInt32 = 0 Scanner(string: cString).scanHexInt32(&rgbValue) return UIColor( red: CGFloat((rgbValue & 0xFF0000)… Read More »

Class has not initialiser error in Swift – Solved.

By | August 14, 2017

Apart from other languages, when you write a class in Swift, It should have a initialiser. To solve this we have to write init() function in that particular class. Just like below. For example, look at the below class. Here we have 5 variables. The above error usually comes when you don’t initialize all variables in the class… Read More »

Custom TimerView in iOS – Swift

By | April 10, 2017

Hello everyone, Here in this tutorial I will show you how you can create a Custom View for Timer without any layout in Swift. Custom Timer Class Our Custom timer class will look like this [java] var timerBorderWidth : Int = 2; var timerWidth : Float = 30; var timeRadius : Float = 40; var timerVal : Int… Read More »

Core Image Tutorial in Swift

By | March 30, 2017

It allows developers to filter images in their apps. Advantages Core Image supplies 90+ filters. Powerful image filtering capabilities Core Image includes APIs for face detection Automatic image enhancements Custom effects through “chained” filters. You can get all kinds of effects, such as modifying the vibrance, hue, or exposure. It uses either the CPU or GPU to process… Read More »

How to record Audio using AudioRecorder in iOS Swift

By | February 10, 2017

Well, its really easy for recording audio in an iOS application. We will decalre two variables in our View Controller [java] var recordingSession: AVAudioSession! var audioRecorder: AVAudioRecorder! [/java] Request Permisson from the User [java] recordingSession = AVAudioSession.sharedInstance() do { try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord) try recordingSession.setActive(true) recordingSession.requestRecordPermission() { [unowned self] allowed in DispatchQueue.main.async { if allowed { self.loadRecordingUI() } else {… Read More »

Error Handling in Swift

By | October 1, 2016

When calling a method that may cause a failure, you normally pass it with an NSError object (as a pointer). If there is an error, the object will be set with the corresponding error. You then check if the error object is nil or not and respond to the error accordingly. That’s how you handle errors in Swift… Read More »

Custom TableView in iOS Swift

By | August 6, 2016

In this post we will see how we can create custom tableview cells in Swift. Let’s Start… Create a new project and name it the way you like. For creating Custom TableView we have to do below things 1. Drag a tableview and Drag a UITableView Cell into it. 2. For our example – Drag and image and… Read More »

What are optionals in Swift ( ? && ! ).

By | June 30, 2016

Swift introduces Optionals type, which handles the absence of a value. Optionals say if there are set a value, then take that value, it can be of any type AND else it is nil. Basically It has two possible values, None and Some(T), where T is an associated value of the correct data type available in Swift. Example… Read More »

Using NSOperationQueue in iOS to do Serial/Concurrent Operations.

By | May 7, 2016

NSOperationQueue is used to do scheduled operations in iOS. You can customize NSOperationQueue to do Concurrent/Serial operations. You can set NSOperationQueue maxConcurrentOperationCount to tell it to do how many operations to execute at a time. Lets see this with an example [Swift Version] [c] import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let downloadQueue =… Read More »

Contacts Demo in Swift 2 – Showing Contacts Picker, Load Contacts in Custom TableView, Add New Contact, Update Contact, Delete Contact, Search Contact

By | February 23, 2016

In Today’s post I will show you How to do below things in Swift 2… 1. Show Contact Picker using System In-Built UI & Pick a Contact 2. Load all Contacts in a TableView 3. Search a Contact 4. Add a New Contact 5. Update a Contact 6. Delete a Contact Before using the contacts we need to… Read More »