Primary
Question 1-swift 1.0 or later
What is the optional type, which is used to solve the problem?
Answer: The optional type is used to indicate that any type of variable can represent a missing value. In Objective-c, a variable of a reference type can be missing and worthwhile, and nil is used as a missing value. Basic data types such as int or float do not have this function.
Swift extends the concept of missing values in basic data types and reference types with optional. A variable of type optional, at any time can save a value or nil.
Question 2-swift 1.0 or later
In Swfit, when to use a struct, when to use a class?
The answer: There has always been the argument that it is better to use a class than a struct or a struct to be superior to a class. Functional programming tends to be value types, and object-oriented programming prefers classes.
In swift, classes and structs have many different characteristics. Here's a summary of the differences:
Class supports inheritance, structs do not support.
Class is a reference type, struct is a value type
There is no general rule that determines which of the structures and classes is better used. The general advice is to use the smallest tool to accomplish your goal, but a good experience is to use structs more often, unless you use inheritance and referential semantics.
To learn more, click here.
Note: At run time, the structure is better than the class in terms of performance, because the method invocation of the struct is a static binding, and the method invocation of the class is implemented dynamically. This is another good reason to use structs instead of classes as much as possible.
Question 3-swift 1.0 or later
What is a generic type? What kind of problem is generics used to solve?
Answer: Generics are a type of work used to make types and algorithms secure. In Swift, generics, such as classes, structs, and enumerations, can be used in both functions and data structures.
Generics are generally used to solve the problem of code reuse. One common scenario is that you have a function that takes a parameter with a parameter type of a, but you have to copy the function when the parameter type changes to B.
For example, the second function in the following code is to copy the first function-it simply replaces the integer type with the string type.
12345678910 |
func areIntEqual(x: Int, _ y: Int) -> Bool {
return
x == y
}
func areStringsEqual(x: String, _ y: String) -> Bool {
return
x == y
}
areStringsEqual(
"ray"
,
"ray"
)
// true
areIntEqual(1, 1)
// true
|
Objective-c developers might want to use the NSObject class to solve this problem, the code is as follows:
12345678 |
import Foundation func areTheyEqual(x: NSObject, _ y: NSObject) -> Bool { return x == y } areTheyEqual( "ray" , "ray" ) // true areTheyEqual(1, 1) // true |
This code works as expected, but it is not secure at compile time. It allows strings to be compared to integers, like this:
The application does not crash, but allowing strings and integers to be compared may not be the expected result.
By using generics, you can combine these two functions into one and keep type safety at the same time. Here is the code implementation:
123456 |
func areTheyEqual(x: T, _ y: T) -> Bool { return x == y } areTheyEqual( "ray" , "ray" ) areTheyEqual(1, 1) |
The above example is to test whether the two parameters are equal, and the type of the two parameters must be constrained to follow the equatable protocol. The above code achieves the desired result and prevents the passing of different types of parameters.
Question 4-swift 1.0 or later
Under what circumstances do you have to use implicit unpacking? explain why.
Answer: The most common reasons for using implicit unpacking for optional variables are as follows:
1. Object properties cannot be nil at initialization time, otherwise they cannot be initialized. A typical example is a property of the interface Builder outlet type, which is always initialized after its owner initializes. In this particular case, it is assumed that it is not nil until it is properly configured in Interface Builder--outlet is used.
2. Solve the cyclic problem of strong references-when two instance objects are referenced to each other and the value of the referenced instance object cannot be nil. In this case, the referenced party can be marked as unowned and the other party uses an implicit unpacking.
Recommendation: Do not use implicit unpacking on the option type unless necessary. Improper use can increase the likelihood of a run-time crash. In some cases, crashes may be intentional behavior, but there are better ways to achieve the same results, for example, by using the FatalError () function.
Question 5-swift 1.0 or later
How many ways to split a optional variable? and evaluated in terms of safety.
Answer:
Force the Unpacking! Operator--Unsafe
Implicit unpacking variable declaration--not safe in most cases
Optional bindings--security
Self-judging links (optional chaining)--security
Nil coalescing operator (null-value merge operator)--security
New features of Swift 2.0 guard statement--security
New features of Swift 2.0 optional pattern (optional mode)--Security (@Kametrixom support)
Intermediate
Question 1-swift 1.0 or later
Is Swift an object-oriented programming language or a functional programming language?
Answer: Swift is a hybrid programming language that contains both of these programming patterns. It implements the three basic principles of object-oriented:
Packaging
Inherited
Polymorphic
Speaking of Swift as a functional programming language, we have to say what is functional programming. There are many different ways to define functional programming languages, but they express the same meaning.
The most common definitions are from Wikipedia: ... It is a programming specification ... It calculates computer operations as mathematical functions, avoiding state changes and data changes.
It is hard to say that Swift is a mature functional language, but it already has the foundation of functional language.
Question 2-swift 1.0 or later
Are the following feature features included in Swift?
1. Generic type
2. Generic structure
3. Generic protocol
Answer:
Swift includes 1 and 2 features. Generics can be used in classes, structs, enumerations, global functions, or methods.
3 is achieved through the Typealias section. Typealias is not a generic type, it is simply the name of a placeholder. It is usually referenced as an association type and is defined only when the protocol is referenced by a type.
Question 3-swift 1.0 or later
In Objective-c, a constant can be defined like this:
A similar swift is defined as:
What's the difference between the two? If so, please explain why.
Answer: A const constant is a variable that is initialized at compile time or when the compilation is parsed. Created by let is a run-time constant, which is not becoming. It can be initialized with the stattic or the dynamic keyword. Keep in mind that its value can only be assigned once.
Question 4-swift 1.0 or later
Declaring a static property or function, we often use the static modifier of the value type. Here is an example of a struct:
123 |
struct Sun { static func illuminate() {} } |
For classes, it is possible to use the static or class modifier. The effect they use is the same, but they are different in nature. Can you explain why it's different?
Answer:
Static-modified properties or decorated functions cannot be overridden. But with the class modifier, you can override a property or function.
When static is applied in a class, static becomes an alias of class final.
For example, in the following code, when you try to override the illuminate () function, the compiler will error:
12345678910111213 |
class Star {
class func spin() {}
static func illuminate() {}
}
class Sun : Star {
override class func spin() {
super
.spin()
}
override static func illuminate() {
// error: class method overrides a ‘final‘ class method
super
.illuminate()
}
}
|
Question 5-swift 1.0 or later
Can you save a property by extension (extension)? Please explain why.
Answer: No. An extension can add new behavior to the current type, but cannot change its own type or its own interface. If you add a new stored property, you need additional memory to store the new value. Scaling does not enable such a task.
Senior
Question 1-swift 1.2
In the Swift1.2 version, can you explain the problem of declaring enumerations with generics? Take the either enumeration in the following code to illustrate it, it has two generic type parameters T and V, the parameter T is used with the associated value type left, and the parameter V is used in the case of an association value of RIHGT, the code is as follows:
1234 |
enum Either{ case Left(T) case Right(V) } |
Tip: Verify that the above conditions need to be in the Xcode project, not in Playgroud. Also note that this issue is related to Swift1.2, so the version of Xcode must be more than 6.4.
Answer: The above code will have a compilation error:
1 |
unimplemented IR generation feature non-fixed multi-payload enum layout |
The problem is that the memory size of T is not pre-determined because it relies on the T type itself, but an enum requires a fixed-size payload.
The most common workaround for
is to tell the generic type to be wrapped in a reference type, often called box, with the following code:
1234567891011 |
class box{    let value: t    init (_ value: t) { self.value = value } }   enum either{    case left (Box) case right (Box) |
This issue appeared in Swift1.0 and later versions, but Swift2.0 was resolved.
Question 2-swift 1.0 or later
Are closures a reference type?
Answer: Closures are reference types. If a closure is assigned to a variable and the variable is copied to another variable, then they refer to the same closure and their capture list is copied.
Question 3-swift 1.0 or later
The UINT type is used to store unsigned integers. The following code implements the initialization method for a signed integer conversion:
Init (_ Value:int)
However, in the following code, when you give a negative value, it produces a compile-time error:
Let mynegative = UInt (-1)
We know that the internal structure of negative numbers is a positive number using twos complement, how to convert a negative integer to an unsigned integer while keeping the negative memory address constant?
Answer: Use the following initialization method:
UInt (Bitpattern:int)
Question 4-swift 1.0 or later
Describes a situation in which a circular reference occurs in swift and explains how to resolve it.
Answer: Circular referencing now when two instance objects have strong referential relationships with each other, this causes a memory leak because neither pair of images is released. As long as an object is strongly referenced by another object, the object cannot be freed, and each object will persist because of the existence of a strong reference.
The solution to this problem is to break the circular reference by using a weak or unowned reference instead of one of the strong references.
Question 5-swift 2.0 or later
Swift2.0 adds a new keyword to implement recursive enumeration. The following example is an enumeration type that has two associated value types T and list under the node condition:
123 |
enum List{ case Node(T, List) } |
What keywords can implement recursive enumeration?
Answer: Indirect key value can allow recursive enumeration, the code is as follows:
123 |
enum List{ indirect case Cons(T, List) } |
Where to Go from here?
Congratulations to you at the end of the article, and don't be frustrated if you don't know the answers to all the questions.
Because some of the problems above are quite complicated, and Swift is a expressive language, there is a lot that we need to learn. In addition, Apple has been improving the new features of Swift, so even the best people will never know everything.
"Interview Essentials" Swift face questions and Answers