iOS Development Knowledge Brush Up 10-10 : Part 1


Sometimes what happened in our life, we use something but when someone asks we cannot answer clearly. While I learn something, I take notes and often think to share notes as blog post. It helps me to brush up my knowledge when necessary. In this article, I am sharing 10 things I noted a few days ago during my iOS app development learning using Swift.  After first 10 things I have posted 10 coding challenges I tested myself during study. If you like you may try to solve, at the end of the post all the answers are given.

1. What is Cocoa and Cocoa Touch

Cocoa is Apple’s native object oriented based API – Application programming interface that includes the Foundation and AppKit framework to develop application for macOS.

Cocoa Touch is the foundation and UI – User interface based framework to build application for iOS.

2. What is C?

C is a general purpose structured programming language originally developed by Dennis Ritchie in Bell labs which is first appeared in 1972. It has been widely used for developing operating system and firmware application.

3. What is Objective-C

Objective-C was the primary programming language to develop application for macOS and iOS before 2014. Still it can be used to develop application for apple platform. This language is a superset of C programming language. It adds some new features, like Object oriented programming, dynamic runtime. It inherits all the C language features, syntax, primitive types, control flow statements and adds new syntax for defining classes and methods.

4. What is Swift

Swift is a programming language developed by Apple Inc. which first appeared in 2014. It is a general purpose compiled programming language which is used the open source LLVM – Low Level Virtual Machine compiler. It is a multi-paradigm programming language to develop software for macOS, iOS, watchOS and tvOS.

5. What is type safe language

Swift is a type safe language. It checks the data type before passing value. For example, if part of code expects String data, type safety prevents to provide Int data and will show error. Similarly, if part of code expects optional String, type safety prevents to provide non-optional String data.

let x = 20 //Integer value

let y:Double = x //can not compile, type safety prevents passing Int to Double

6. What is type inference

Swift supports type inference. Which means the compiler can automatically detect the expression type while compiling the code simply by examining the values provided.

let x = 20 // type inference automatically set this value to Integer

7. What is type annotation and when it is required

Type annotation defines what type is the constant or variable is. If a variable and constant is declared without providing initial value type annotation is must.

var x // error

var x:Int // okay

8. When semicolon (;) is required in Swift

When multiple statement is defined in single line.

var x = 100; var y = 200.0

9. How to use auto conversion in Swift

Swift does not support auto conversion between types at all. All conversion should be defined explicitly.

10. How many ways optionals values can be used

There are 3 ways to unwrap optional use:

1. Optional Binding:  if let

2. Nil Coalescing Operator ??

3. Force unwrap !

var score:Int?

score = 100

//if score is nil the if block will not work

/*if score has value, the if block create a new constant score and assign

the score value to the new constant score which is unwrapped and available

within the if block

*/

if let score = score{

score

}

score ?? 0 //provide a default value when there is nil

score! //when u sure the optional must have a value other than nil

Some coding challenges which I noted and created during my learning time. All the codes here I tested in playground. If you like you can think the challenges and try to solve. At the end I wrote the answer as well.

1. Is this code correct?

typealias Ajaira = Int

var num:Ajaira = 100

var num2:Int   = num

2. Is this implementation correct?

let xxx = 100

if Bool(xxx){

print("XXX")

}

3. What is the value of x and y ?

let http404Error = (404, "Not Found")

let x, y = http404Error

4. What is the value of score?

let score:Int ?

5. Is this correct optional binding?

var score:Int?

score = 100

if var score = score{

print(score)

}

6. What type is newScore

var score:Int?

var newScore = score

7.What is wrong with the following code

var score1:Int?

var score2:Int?

score1 = 100

score2 = Int("200")

if let score1 = score1, score2 = score2{

score1

score2

}

8. What is the value of b

var a? = 100

var b = a

9. What is the value of c

var a:Int? = 100

var b = a

var c:Int = b

if let c = c{

print(c+c)

}

10. What is wrong in this code

let a:String? = "Optional Value"

let b:String  = a!

let c:String! = b

let d:String  = c

ANSWERS:

1. Yes, because typealias just give a new alternative name for existing type, so the new name and existing type is same.

2. No, integer or other data type can not be converted to boolean type

3. This code will not work, tuples groups multiple values in a single compound value. The x,y must be enclosed by (x,y) brackets to infer this as tuple.

4. nil, remember swift’s nil is not same as Objective-C nil.

5. Yes, it is correct. Normally if let is used for optional binding, but if var is also correct and if there is a necessity of modifying unwrapping optional value, then if var can be used.

6. newScore is an optional Int type by automatically defined swift type inference.

7. It will not compile. score2 must be followed by either let or var keyword

8. The code will not compile. Before defining something optional you must explicitly defined the type. so var a:Int? = 100 will fix the error and run the program.

9. The code will not compile. Here a is defined optional and set value 100. b is declared and assigned value of a. As a is optional Int, b is automatically optional Int by compiler detected type inference. But as c is declared non-optional Int, so b cannot be assigned to c as b is optional. Two ways to solve this problem. c:Int? defining c variable as optional and c:Int! defining the c as implicitly unwrapped optional.

var c:Int? = b  // defining c as optional

var c:Int! = b    // defining c as implicitly unwrapped optional

10. The code is absolutely right.

let a:String? = "Optional Value"

let b:String  = a! //force unwrapped optional value

let c:String! = b //implicitly unwrapped optional value

let d:String  = c //no need for exclamation mark, implicitly unwrapped optional automatically unwrapped the value when it accessed
mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like

LEAVE A COMMENT