Skip to main content

IOS Q&A -The Basics#01

IOS Interview Questions and Answers

The Basics#01

1. What are the Key Features of the Swift programming language?
  • Modern: Swift is a result of the latest research on programming languages, combined with decades of experience building Apple platforms. Also, Swift is continuously evolving.
  • Safe: Swift is a type-safe language which means, If we pass a String to a variable of type Int by mistake, then Swift throws a compile-time error. Also, it’s a static programming language i.e. any variable or a constant should be declared or inferred to a specific type at the compile time.
  • Fast: Swift was built to be fast using the incredibly high-performance LLVM compiler technology. There are claims that Swift is 2.6x faster than Objective-C and 8.4x faster than Python.
  • Expressive: Swift is called Syntactic sugar because it makes the language “Sweeter” for developer use. Swift offers a simplified syntax and grammar that is easy to read and write.
  • Open source: In 2015, Swift was open-sourced to encourage community-driven development of the language itself.

 2. What are types in swift?

In Swift, there are two kinds of types: Named types and Compound types.

A Named type is a type that can be given a particular name when it’s defined. Named types includes classes, structures, enumerations, protocols, arrays, dictionaries, optional values 
and user-defined named types etc,.

A Compound type is a type without a name, defined in the Swift language itself. There are two compound types: function types and tuple types.

3. How Swift ensures safety?
  • Swift eliminates entire classes of unsafe code.
  • Variables are always initialized before use.
  • Integers are checked for overflow.
  • Array indices are checked for out-of-bounds errors.
  • By default Swift objects can never be nil. Optionals ensure that nil values are handled explicitly.
  • Error handling allows controlled recovery from unexpected failures.
  • Swift heavily leverages value types, especially for commonly used types like Arrays and Dictionaries. Value types are quite safe while accessing in thread & memory.
  • Memory is managed automatically, and enforcement of exclusive access to memory guards against many programming mistakes.

4. What are the major differences between Swift and Objective-C?

Syntactic sugar
Syntactically verbose and complex
Static & Strongly typed
Dynamic typed
Full ARC support
Some API doesn’t support ARC
Apache open-source
GPL (General Public License)
Nil Handling
Optionals are better way to handle nil values.
The ability to send a message on a nil object without crashing might lead to bugs
Type inference
Yes. Also support explicit types.
Need to specify the type explicitly.
Better support
Since Objective-C is built on top of C, it lacks namespacing. All classes in an Objective-C application should be globally unique.Hence UI, NS prefixes are used to make classes unique.
Multiple return types using tuples
Can’t return multiple values
No such thing as a traditional pointer in swift.
But there’s a way, using Unsafe Pointers.
Explicit pointers exist.
Functional Programming Patterns
Swift incorporates a number of functional programming features, such as map and filter.Also it is easy to create user defined higher order functions using extensions.
Objective-C has no built-in support for functional programming.
Enums are very powerful in swift, then can contain methods and be passed by value. Also, Indirect statement allows enums to refer to itself.
Typical C type enums
Guard & Defer
Guard statements are most useful when combined with optional bindings.

A defer block is executed just before the control transferred out of the current scope.
String Manipulation
Better support with String interpolation. No need to worry about mutability.
String manipulation like concatenation,interpolation, formatting etc,. is cumbersome.

Please check out this tutorial on Youtube.
Your comments help us a lot, Please ask if you have any questions. 


Popular posts from this blog

UILabel text animation as in UIButton click

Animating UILabel textcolor is not easy as it seems, normal animation won't work on this property. The below code snippet animates the UILabel textcolor as in button click.
Change the duration and UIViewAnimationOptions to play with it. extensionUILabel{ func flashLabel() { func animate(duration:Double,alpha:CGFloat,completion:(()->())?){ UIView.transition(with: self, duration: duration, options: UIViewAnimationOptions.transitionCrossDissolve, animations: { self.textColor = self.textColor?.withAlphaComponent(alpha) }) { _ in completion?() } } animate(duration: 0.1, alpha: 0) { animate(duration: 0.3, alpha: 1, completion: nil) } } } Happy coding...

UIAlertController made easy

Once I read somewhere on the internet, creating an Alert in IOS is comparatively harder than JavaScript. I'm not a big fan of HTML & CSS, but I do know a little Javascript, creating an Alert popup in Javascript is pretty much easier, just like this. alert("I am an alert box!"); That's all it takes to create an alert using JS. If you want to try other types of popups using JS, check out here.
But, let's try with Swift. let alertVC = UIAlertController(title: "Alert", message: "Testing", preferredStyle: .alert) let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertVC.addAction(okAction) Looking at the above, IOS needs a little extra code, doesn't it? But, that's not the end, let's make it lesser, reusable and better. How about this? alert(title: "Alert", message: "Auto dismiss in 4 sec") I've written an extension for UIViewController, which uses just the above…

Stack Problem - Balancing of Symbols

Stack Problem - Balancing of Symbols
Problem: Given an expression string exp , write a program to examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.

Input: exp = “[()]{}{[()()]()}”
Output: Balanced Input: exp = “[(])”
Output: Not Balanced
Create a stack.while ( end of input is not reached ) {If the character read is not a symbol to be balanced, ignore it.If the character is an opening delimiter like ( , {  or [ , PUSH it into the stack.If it is a closing symbol like ) , } , ] , then if the stack is empty report an error, otherwise POP the stack.If the symbol POP-ed is not the corresponding delimiter, report an error.At the end of the input, if the stack is not empty report an error.Implementation :
func balancingOfSymbols(string:String)->Bool{ guard string.count > 0else { return false } let symbols:[Character:Character] = ["{":"}","[":"]","(":")",] v…