Posts

Finding free space in UIView

When there is a need to find free space in the UIView to add any subview, we can do this using CAShapeLayer and UIBezierPath. Create a CAShapeLayer for your main view where you will be adding sub views. Let's call it as main shape layer. This will be helpful to check the new view estimated is within the main view. Create a UIBezierPath instance. Whenever a valid new sub view is found, add the edges to this path. Create a random point within the main view. Create a CGRect based on random point as center of your sub view. Let’s call it as estimated view frame. Check the estimated view frame is completely visible in your main view. Else go to step 3. Check your 4 edges of your estimated view frame with path object. If any one of the edge is inside the path, go to step 3. If 4 edges are not inside the path, the estimated view frame is the new view’s frame. Create a new subview and add it to your main view. Add the edges of new view to path. https://github.com/mcabashe...

Swift - Functions

Functions Let's discuss about Functions. To declare a function, use func keyword. Add the parameters and their data type inside a parentheses. The return type is separated by the -> symbol. A general function syntax would be, func ( : ) -> { } func welcome (guest : String, meridian : String) -> String { return "Hello \(guest), \(meridian)." } welcome (name : "Alice", meridian : "Good Morning") Swift supports a great feature when it comes to returning values from functions. You can combine multiple parameters and return them as Tuples . func daysOfWeek(week : [String]) -> (firstDay : String, secondDay : String) { let fristDay : String = week[0] let secondDay : String = week[1] return (firstDay, secondDay) } Here, firstDay and secondDay are two String type parameters that are grouped and returned together. Lets look how the values that are returned and handled in swif...

Swift - Strings

String To declare a string, use let or var keyword. var greeting = "Hello"   // Mutable string let name = “Alice” // Immutable string let keyword creates an immutable string. var creates a mutable string. To modify a string later in code, use var keyword. let will throw error. Swift provides a list of common methods extending String class. These methods will perform various operations with strings. Let’s have a look at few of those commonly used methods. To check the variable is String type let stringObject = "String here" if stringObject is String { print ("Yes") // Yes } To check if two strings are equal let greet1 = "Hello guest" let greet2 = "Hello world" if greet1 == greet2 { print ("same greeting") } else { print ("different greetings") } To capitalise all the words var greeting = "hello guest" print (greeting.capitalizedString)...

Swift - Classes

Classes This is Part 4 of our Swift tutorial series. To create a class in swift, use class keyword. The properties and methods are declared in the same way as constant and variable. class { init () { } } Let's create a class with functions. class mathsClass { func add (a:Int, b:Int) -> Int { return a+b } } let mathsObject = mathsClass() print (mathsObject.add(a:1, b:1)) // 2 Swift classes supports inheritance . We can create a parent class and inherit its properties in the child class. class Student { func studentName () -> String { return "Alice" } } class ReportCard : Student { } let studentCard = ReportCard() print (studentCard.studentName()) // Alice The above example had the student name in function. Let's create a property and store the value. class Student { var name : String init (studentName : String) { name = studentName } } let student = Student(...

Swift - Control Flows

Control Flow This is Part 3 of our Swift tutorial series. In this article, we are going to discuss about if and switch to make conditional execution and for, for-in, while and repeat-while to make loops. IF An If condition must contain a Boolean expression. let inviteAllowed = true if inviteAllowed { print("All are welcome") } Swift allows developers to use let command work with If statement. let guestName : String? = "Host" if let guest = guestName { print("Welcome,  \(guest)") } If guestName is set to nil, the if condition would be skipped IF-ELSE This is an extended version of If statement. When the condition is success, executes a set of command. When the condition fails, execute another set of command. let inviteAllowed = true if inviteAllowed { print("All are welcome") } else { print("Invite Only") } SWITCH Switch support all kind of data and ...