General questions Flashcards
What is swift? Explain some features
Swift, a modern programming language, was announced by Apple Inc. in June 2014.
- Fast (static dispatch for methods)
- Safe (type safe)
- Friendly syntax (more concise than objective-c)
Some key features unique and powerful are : generics, tuples, closures and type inference, optional.
What is the difference between static typing and dynamic typing?
Swift is using Static typing, which is able to give you a compiler error. Objective-C is using dynamic typing, where it performs type checking at run-time. This means that code can compile even if they contain errors that will prevent the script from running properly.
What is type inference?
A feature that enables compiler to automatically set data type on a variable without setting data type, by looking at the value.
What is generics?
A type that can take in any type. It is type safe, meaning that compiler will complain at compile time. For instance, if you pass a string as a generic type and use it as integer, there will be compile-time error. This is good, because you can detect errors beforehand.
Can objects be nil in Objective-c and Swift?
In Objective-c, any object can be nil. In swift, only Optionals can be nil
What are protocols?
Protocol is an interface, meaning that they contain abstract methods, constants and variables. Protocols enables shared functionality across classes. You can implement multiple protocols.
What are tuples?
Because they are value types, they are like an immutable array, but the way declared and read is different. They are useful when you want to have multiple return types. var person = ("Saaya", "Age is secret")
var firstName = person.0 // Saaya var lastName = person.1 // Age is secret To declare, you need a parenthesis (), and to access, you need to give a (.)dot notation. func getTime() -> (Int, Int, Int) { return ( hour, minute, second) } var times = getTime()
How is mutability achieved in swift and objective-c?
In swift, a constant is a constant, a variable varies. The mutability is defined when initializing a variable with a keyword, not a defined by a data class.
In objective-C, mutability is limited by certain classes. For instance, you have to use NSMutableArray type to be able to change the array size.
What is the difference between Any Object and Generics?
AnyObject does not specify any type, so it will let you pass any type, which might end up in a run-time error(which is bad). When you use generics, you need to specify a type, so it will not let you pass if you input the wrong type at compile-time(which is good).
What is an optional?
Optional is a type that can store nil value. When it stores a value, it wraps around the value. In order to get the value wrapped in an optional, you need to unwrap it.
Write 3 ways to unwrap an optional
if-let unwrapping (safe unwrapping)
var myString:String?
if myString != nil { print("Value exists!") }else { print("Nil value") } guard-if unwrapping (also safe) var someStr: String? guard if unwrapped = someStr else { return } Print (unwrapped) forced unwrapping (use only when 100% sure there is value) var someStr: String? Let unwrapped3 = someStr!
What is the difference between Struct and Class?
Structs are value types, while classes are reference types. Class In OOP, a class is a blueprint from which individual instances are created. A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). We define a class using the class keyword. Struct A struct is similar to a class in terms of definition and instance creation. We define a struct using the struct keyword. What are the common factors between struct and class? Define properties to store values Define methods to provide functionality Define subscripts to provide access to their values using subscript syntax Define initializers to set up their initial state Be extended to expand their functionality beyond a default implementation Conform to protocols to provide standard functionality of a certain kind Classes have additional capabilities that structures don’t have: Inheritance enables one class to inherit the characteristics of another. Struct or enum cannot do inheritance. But they can confirm to protocols. Type casting enables you to check and interpret the type of a class instance at runtime. Deinitializers enable an instance of a class to free up any resources it has assigned. Reference counting allows more than one reference to a class instance. Since a class is a reference type and it supports inheritance, the complexity increases. In most cases, a struct should be enough to meet your needs. Use a class when they’re appropriate or necessary.
Name two types of data structure for memory allocation
Stack and heap memory
What is the difference between stack and heap memory?
Stack is a simple FIFO(first-in-first-out) memory structure. Stack is always used to store the following two things: The reference portion of reference-typed local variables and parameters and the Value-typed local variables and method parameters.
Heap memory has non-lined random objects stored in the memory. The advantage is that it allows objects to be allocated or deallocated in a random order. This will require you to use garbage collectors functions to free the memory though.
In heap memory, the following is stored: the content of reference-typed objects.
What are closures?
Self-contained functions organized as blocks. They can be passed around and nested. You can pass a closure as an argument of a function or you can store it as a property of an object.