Swift Programming Language Interview Questions And Answers

Interview Questions

1. What is Swift Programming Language?

Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.

2. How to define Variables in Swift Language?

Constants and variables must be declared before they are used. You declare constants with the let keyword and variables with the var keyword.

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

3. How to define kSomeConstant as an integer?

In this case kSomeConstant is implicitly defined as an integer. If you want to be more specific you can specify which type it is like so:

let kSomeConstant: Int = 40
4. What is best way to add a Table View?

Open storyboard file in Xcode and lets drag in a “Table View” object from the Object Library (don’t use a table view controller.) Position this full screen in your app window and make sure it lines up with the edges. Then resize the height by dragging down the top edge and giving a little bit of space (this gives room for the status bar at the top of the phone.) If you run the app at this point, you should see an empty table view in the simulator.

5. How to connect UI in Swift Language?

Same as like c-objective. There is no change in binding process only core level has been changed. You can choose button/label on xib file and binding as is it.

6. What is difference between ‘let’ and ‘var’ declaration ?

constants are expressed with the ‘let’ keyword. So once assigned value can not be change, but assigned values using ‘var’ keyword can be change.

In terms of objective -c or compare with objective -c, ‘var’ is Mutable and ‘let’ is NonMutable.

let kMyConstant = 40
Or
let kMyConstant: Int = 40

var myString = “This is my string.”
7. What is Initialization ?

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.
You implement this initialization process by defining initializers, which are like special methods that can be called to create a new instance of a particular type. Unlike Objective-C initializers, Swift initializers do not return a value. Their primary role is to ensure that new instances of a type are correctly initialized before they are used for the first time.

Initializers
Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters, written using the init keyword:
init() {
// perform some initialization here
}

In Swift How to connect UI under Swift Environment ?

UI connectivity has no change, its similar to objective-c. Xib/Storyboard under Interface builder.
8. How to make an API (Web service) Call?

To make API (Web Service ) call in Swift you can do,

var params = [“MIN”:”f8d16d98ad12acdbbe1de647414495ec”, “MobileType”:”IOS”,”format”:”json”,”OperatorID”:”500200000000010025″] as Dictionary // Its the parameters required in the webservice.

var request = NSMutableURLRequest(URL: NSURL(string: kDataURL)) // Here kDataURL is the marco for URL.
var session = NSURLSession.sharedSession()
request.HTTPMethod = “POST”

var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
request.addValue(“application/json”, forHTTPHeaderField: “Content-Type”)
request.addValue(“application/json”, forHTTPHeaderField: “Accept”)

var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
// println(“Response: \(response)”)
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println(“Body: \(strData)”)

var err: NSError?
var json2 = NSJSONSerialization.JSONObjectWithData(strData.dataUsingEncoding(NSUTF8StringEncoding), options: .MutableLeaves, error:&err1 ) as NSDictionary

println(“json1\(json2)”)


Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top