1) Explain what is Swift Programming Language?
Swift is a programming language and system for creating applications for iOS and OS X. It is an innovative programming language for Cocoa and Cocoa Touch.
2) Explain how you define variables in Swift language?
Variables and constants must be declared before they are used. You announce constants with the let keyword and variables with the var keyword. Both variables and dictionaries are described using brackets. For example,
Var Guru99 = “This is Guru99”
Let ksomeconstant = 30
3) Explain how Swift program is deployed?
Swift program deploys the application in a Tomcat installation by default. The deploy script bundles the client code into JavaScript, gathers all the server side classes required and packages them into file Hello.war. This file together with a GWT jar and a Swift runtime jar is copied into the Tomcat installation. If CATALINA_HOME is not set, these files require to be copied manually.
4) Mention what are the features of Swift Programming?
- It eliminates entire classes of unsafe code
- Variables are always initialized before use
- Arrays and integers are checked for overflow
- Memory is managed automatically
- Instead of using “if” statement in conditional programming, swift has “switch” function
5) Mention what is the difference between Swift and ‘Objective-C’ language?
Difference between ‘C’ and ‘Swift’ language is that
| Swift | Objective-C |
|
|
6) Mention what are the type of integers does Swift have?
Swift provides unsigned and signed integers in 8, 16, 32 and 64 bit forms. Similar to C these integers follow a naming convention. For instance, unsigned integer is denoted by type UInt8 while 32 bit signed integer will be denoted by type Int32.
7) Mention what is the Floating point numbers and what are the types of floating number in Swift?
Floating numbers are numbers with a fractional component, like 3.25169 and -238.21. Floating point types can represent a wider range of values than integer types. There are two signed floating point number
- Double: It represents a 64 bit floating point number, it is used when floating point values must be very large
- Float: It represents a 32 bit floating point number, it is used when floating point values does not need 64 bit precision
8) Explain how multiple line comment can be written in swift?
Multiple line comment can be written as forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/).
9) What is de-initializer and how it is written in Swift?
A de-initializer is declared immediately before a class instance is de-allocated. You write de-initializer with the deinit keyword. De-initializer is written without any parenthesis, and it does not take any parameters. It is written as
deinit {
// perform the deinitialization
}
10) Mention what are the collection types available in Swift?
In Swift, collection types come in two varieties Array and Dictionary
- Array: You can create an Array of a single type or an array with multiple types. Swift usually prefers the former one
Example for single type array is,
Var cardName : [String] = [ “Robert” , “Lisa” , “Kevin”]
// Swift can infer [String] so we can also write it as:
Var cardNames = [ “Robert”, “Lisa”, “Kevin”] // inferred as [String]
To add an array you need to use the subscript println(CardNames[0])
- Dictionary: It is similar to a Hash table as in other programming language. A dictionary enables you to store key-value pairs and access the value by providing the key
var cards = [ “Robert”: 22, “Lisa” : 24, and “Kevin”: 26]
11) List out what are the control transfer statements used in Swift?
Control transfer statements used in Swift includes
- Continue
- Break
- Fallthrough
- Return
12) Explain what is optional chaining?
Optional chaining is a process of querying and calling properties. Multiple queries can be chained together, and if any link in the chain is nil then, the entire chain fails.
13) How base-class is defined in Swift?
In Swift the classes are not inherited from the base class and the classes that you define without specifying its superclass, automatically becomes the base-class.
14) Explain what Lazy stored properties is and when it is useful?
Lazy stored properties are used for a property whose initial values is not calculated until the first time it is used. You can declare a lazy stored property by writing the lazy modifier before its declaration. Lazy properties are useful when the initial value for a property is reliant on outside factors whose values are unknown.
15) Mention what is the characteristics of Switch in Swift?
- It supports any kind of data, and not only synchronize but also checks for equality
- When a case is matched in switch, the program exists from the switch case and does not continue checking next cases. So you don’t need to explicitly break out the switch at the end of case
- Switch statement must be exhaustive, which means that you have to cover all possible values for your variable
- There is no fallthrough in switch statements and therefore break is not required
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 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”)
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)”)
// 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
var json2 = NSJSONSerialization.JSONObjectWithData(strData.dataUsingEncoding(NSUTF8StringEncoding), options: .MutableLeaves, error:&err1 ) as NSDictionary
println(“json1\(json2)”)
1. Why is the language called Swift?
We could make the obvious popular culture joke, but in all seriousness, the language was designed with two goals in mind:
- swift to code
- swift to execute
In terms of speed, Swift uses the LLVM compiler, and compiles Swift code to optimized native code depending on target device. In terms of learning curve, the Swift syntax was designed to be clean and easy to read.
2. Should I learn Swift or Objective-C?
Whether you should learn Swift or Objective-C was the basis of a discussion back in September, and the answer has not changed – Swift! Apple has made it clear that Swift is the cornerstone of the future of iOS development. Plus, you can still utilize Objective-C files alongside Swift code, so you won’t miss out on any pre-existing libraries and code.
3. How easy is Swift to learn?
Swift was designed to be friendly for new programmers, and as a result it is incredibly easy to learn. According to Apple, Swift is the “first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.” Some have even called Swift the new BASIC.
5. Why did Apple decide to create a new language?
Objective-C has been Apple’s primary programming language for app writing since OS X was created. In that time, programming languages and practices changed drastically, especially in mobile development. Rather than adopt a new, already existing language, Apple created a new language tailored specifically for development on their own hardware.
6. Can I create an app in Swift and submit it to the app store?
Absolutely! In fact, you were able to as soon as Xcode 6 and iOS 8 launched.
7. Why was there a need to go from Objective-C to Swift?
As mention in the answer to question five, after 20 years, Objective-C was starting to show its age. Plus, Objective-C is a difficult language for new programmers to learn, so the barrier to entry is pretty high. Swift provides a modern language tailor-made for Apple hardware.
8. How stable is Swift?
As with any new language, there is a potential for bugs. While you may encounter some trouble with the Swift language, the majority of issues were addressed before the 1.0 release.
The thing to most look out for is changes to the Swift language during each update. For example, when updating from 1.0 to 1.1, Apple introduced a new feature: failable initializers. You can expect that the language will change as more people use it and give feedback to Apple. Stay apprised of changes using the revision history for The Swift Programming Language.
9. What other languages is it similar to?
Swift is probably most similar in look and feel to Ruby or Python. Though you’ll also probably recognize some C syntax.
10. Is it compatible with iOS 6 & 7?
Swift can be run on iOS 7, but not iOS 6
Difference between categories and extensions?
| ||||||||
No comments:
Post a Comment