Interview Questions ”; Previous Next What is Xcode? Xcode is Apple’s integrated development environment (IDE) that you use to design apps for Apple products. It provides various tools to manage your entire development workflow from creating your app, to testing, submitting and optimizing it to the App store. How multiple line comments can be written in swift? Multiple line comments can be written as forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). What are the collection types available in Swift? Multiple line comments can be written as forward-slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). What are the control transfer statements used in Swift? Control transfer statements used in Swift include − Break Continue Fallthrough Return 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. What is the characteristics of Switch in Swift? It supports any kind of data and not only Synchronizes it, but also checks for equality. The Switch statement must be exhaustive, which means that you have to cover all possible values for your variable. What is the question mark (?) in Swift? The question mark (?) is used during the declaration of a property. It tells the compiler that this property is optional and whether the property may hold a value or not. What is the use of double question marks (??)? To provide a default value for a variable. What is the difference between let and var in Swift? The let keyword is used to declare constants, while var is used for declaring variables. What is a guard statement in Swift? The guard statement evaluates a Boolean condition and proceeds with program execution if the evaluation is true. What is GCD? How is it used? GCD is the most commonly used API to manage concurrent code and execute operations asynchronously at the UNIX level of the system. GCD provides and manages queues of tasks. What is Synchronous vs. Asynchronous in GCD? A synchronous function returns only after the completion of a task that it orders. An asynchronous function, on the other hand, returns immediately, ordering the task to be done, but not waiting for it. What is MVC? MVC is a design pattern that stands for Model View Controller. This design pattern separates the data from its display, mediated by a View Controller. What are delegates? Delegates are a design pattern. A delegate is just an object that another object sends messages to when certain things happen. What is Core Data? Core Data is an object graph manager, which also has the ability to persist object graphs to a persistent store, on a disk. What is the purpose of the reuseIdentifier? Reusability of an already allocated object. Print Page Previous Next Advertisements ”;
Category: ios Development With Swift2
Making Applications with Swift ”; Previous Next In this chapter, we will create two new Applications using Swift. First Application – “Guess the Number” In this section, we will create an Application called “Guess the number”. To make this application, create a new iOS Single View Application and name it whatever you want. Click on the main.storyboard and select your main view. Add a text label → Change the text to “Guess the number”. Change the color, size properties and make it as per your requirement. Add an Input field, stretch to full view. Add a button and name it “Guess. Add one more label, stretch it, and clear the text box. This is how your view should look like after adding all the elements. Now Switch to assistant editor and Click on drag from your UI element to view controller file, then connect the text field as an outlet and name it userInput. Similarly, Connect empty label as outlet and name it as resultLabel. Connect Guess button as action and name it as guessButtonPressed. What is the logic? The logic is simple, we will generate random numbers between 0-9 and see if that is equal to the number the user entered, or not. If it is equal, we will show “you are right”, else we will show “you are wrong!”. Applying the logic To generate a random number between 0-9, we will use the following command. let rollIt = String(arc4random_uniform(10)) Then we will use the following logic to check whether it is same as the user input or not. if userInput.text == rollIt { resultLabel.text = “You”re right!” } else { resultLabel.text = “Wrong! It was a ” + rollIt + “.” } This is how your final logic in button action function will look like. @IBAction func guessButtonPressed(_ sender: Any) { let rollIt = String(arc4random_uniform(10)) if userInput.text == rollIt { resultLabel.text = “You”re right!” } else { resultLabel.text = “Wrong! It was a ” + rollIt + “.” } } Your final application should look like this now. Let us now run our Application and check its output. The opening screen should look as follows − Next, feed a number in the input area. Let’s feed another number and check its output − We have completed one more Application. Try to run this application, and enter different inputs. Second Application – “Is It Prime” In this application, we will be taking an input from the user and we will check whether that number is prime or not − Layout − Similar to the previous application, we need an input, a button and an output label. Challenges − Create the UI and connect the elements to code. Also, try if you can create the complete project yourself. If you managed to create it by yourself, then it is great and you are doing excellent with iOS Development. If you could not manage, do not worry. Look at the following image and try to do the same. Try to create a view like this, if you are not able to do it yet, please read the previous section where we have developed a Guess Game. What is the Logic? Prime numbers are the numbers that cannot be divided by any other number except 1 and the number itself. Example − 7 is a prime number as any other number except 1 and 7 cannot divide it. How to Implement? Try to write a code for checking prime numbers. Then take the user input and see if that is a prime or not. If yes, then show prime; otherwise show not prime in your result label. Here is the code to check whether a supplied number is “prime” or not − @IBAction func isItPrimeButtonPressed(_ sender: Any) { if let userEnteredString = userInput.text { let userEnteredInteger = Int(userEnteredString) if let number = userEnteredInteger { var isPrime = true if number == 1 { isPrime = false } var i = 2 while i < number { if number % i == 0 { isPrime = false } i += 1 } if isPrime { resultLabel.text = “yes. (number) is prime!” } else { resultLabel.text = “No. (number) is not prime” } } else { resultLabel.text = “Please enter a positive whole number” } } } This is how your button action should look like. Following is the image of the final code and view − This is how your running application should look like if you followed the procedure. Now, let us test our Application by supplying input values − Print Page Previous Next Advertisements ”;
Accessing Web Services
Accessing Web Services ”; Previous Next In our application, we might need to connect to API and retrieve data from that API and use in our application. Firstly, we need the URL, which will provide us the data. api.openweathermap.org/data/2.5/forecast?id=524901&APPID=1111111111 After that, we need to add transport layer security exception to allow our application to communicate to the web service, if the service is not https. We will make these changes in the info.plist file. Finally, we will create an URLSession to create a network request. let urlString = URL(string: “your URL”) // Making the URL if let url = urlString { let task = URLSession.shared.dataTask(with: url) { (data, response, error) in // Creating the URL Session. if error != nil { // Checking if error exist. print(error) } else { if let usableData = data { // Checking if data exist. print(usableData) // printing Data. } } } } task.resume() This is how you can Use Web services in your application using URL sessions. Alamofire Alamofire is a HTTP networking library written in swift. It can be used to make URL Requests, Post Data, Receive Data, Upload File, Data, Authentication, Validation, etc. To install Aalmofire, you can go to Alamofire officially on GitHub, and read their installation guide Making a Request in Alamofire For making a request in Alamofire, we should use the following command. Import Alamofire Alamofire.request(“url”); Response Handling The following command is used for response handling. Alamofire.request(“url”).responseJSON { response in print(response.request) // original URL request print(response.response) // HTTP URL response print(response.data) // server data print(response.result) // result of response serialization if let JSON = response.result.value { print(“JSON: (JSON)”) } } Response Validation The following command is used for response handling. Alamofire.request(“https://httpbin.org/get”).validate().responseJSON { response in switch response.result { case .success: print(“Validation Successful”) case .failure(let error): print(error) } } These are the basics of making URL request, using URL Sessions and Alamofire. For more advanced Alamofire, please visit Alamofire Documentation, and you can read about it in detail. Print Page Previous Next Advertisements ”;
iOS Development – Discussion
Discuss iOS Development with Swift 2 ”; Previous Next iOS (formerly iPhone OS) is a mobile operating system created and developed by Apple Inc. exclusively for its hardware. It is the operating system, which presently powers most of the mobile devices. The most popular ones include the iPhone, iPad and iPod Touch. In this tutorial, we will be covering some basic to advance concepts of iOS, after which you will find yourself at an intermediate level in iOS development. Print Page Previous Next Advertisements ”;
iOS Development – Home
iOS Development with Swift 2 Tutorial PDF Version Quick Guide Resources Job Search Discussion iOS (formerly iPhone OS) is a mobile operating system created and developed by Apple Inc. exclusively for its hardware. It is the operating system, which presently powers most of the mobile devices. The most popular ones include the iPhone, iPad and iPod Touch. In this tutorial, we will be covering some basic to advance concepts of iOS, after which you will find yourself at an intermediate level in iOS development. Audience This tutorial is designed for programmers who want to understand the iOS Application development using the Objective Swift Programming Language. In this tutorial, we will be implementing and Learning at the same time. This tutorial is intended to make you comfortable in getting started with iOS Development with Swift and its various functions. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages, especially Swift language, which will help you learn the concepts of iOS programming faster, (though not compulsory as we are going to cover every concept). Print Page Previous Next Advertisements ”;
Making the App Interactive
Making the App Interactive ”; Previous Next In this chapter, we will introduce some new things and UI features that iOS provides for interaction with the user. We will be adding − Text Fields Labels Buttons and their actions Additionally, we will be writing the code in swift for a Dynamic Label, which will show the calculated result of the input entered by the user. By the title “Making our app interactive”, we mean making our application interact with the user. Therefore, here we give user the power to interact and control the application. Adding Text Fields Here, we will again make a new project. It should be easily manageable, as we have already discussed how to create a new project in Xcode. Okay, so we will now create a New Project Called “My Dog’s Age.” After creating this project, we will click on our “Main.storyboard” File and follow the steps given below. In the utility Pane’s Search bar (located in the bottom right corner of Xcode), search for Label. Click and Drag that Label to your main.storyboard / (View). Then, double click on the label and rename it as − “My Dog’s Age”. Search for “text field”, click and drag that text field to your View. While this text field is selected, go to attribute inspector and change the keyboard type to Number pad, so that only number can be entered as shown in the screenshot below. Adding Buttons to our View Now search for a Button in the search bar. Drag that to your view, double click on it and rename as “Calculate”. Adding Label to View Search for label and add it below the button, to show the age output. Double click and make the label empty and stretch it a little bit, so that the complete output is visible. Tip − If you are not able to rename by double clicking, then select the item and in the utility pane − Attribute inspector, there is the title of that item, modify there and press Return as shown in the following screenshot. Now, your Main.storyboard should look like the following screenshot. We do not stop here, now we will be discussing how to add images to the main.storyboard. Adding Images to Our View To begin with, we should first search for an image, which we want to add in the project. You can download the image given below − Copy this image into your project, or drag this image to your project, then you will see the following window. Make sure you select, copy items if needed and create groups. Click on the Finish button. Now, go to Utility Pane → Object Library and search for Image views. Drag the image view to your view. Now your view should look like the screenshot given below. Now, click on this Image View, you just dragged in your view and then you will see that in the utility area there is an option named “Image” to select the image. Click on that arrow and you will see all the images available. Make sure you have selected this newly added image view. Now that you have selected the image for your image view, your final view should look like the following screenshot. This is the only thing that we will do with our main.storyboard, for this application. This is our view now after adding all the UI elements. After this, we have a logical implementation of our code that we will continue if you have completed until this step. Now, select your view controller and open assistant editor by clicking the assistant editor button in the top right corner (as shown in the screenshot below). Now, our view should look like the following screenshot. Adding Functionality to Our Application Until now, our application is just a Static Application, which does not respond to anything and does not change on the user interaction. Now comes the main part of connecting our UI Elements to our Code and the UI will change according to users Input. The “ViewController.swift” file is our main file in which we will be writing code for our Current view. Note − Currently we are working with single views, later when we discuss about multiple views. We will discuss how different files control different Views. Click on the text field, press control and drag your cursor to Second part of screen, i.e. viewcontroller.swift file. You will see a blue line connecting our view and swift file. When you release the mouse, you will see a popup, asking for input. TIP − Fill the Name field with any Name that resembles to your Input field. One important point is that the name cannot have a space, so you can write it like shown in the earlier image, i.e. if the name has multiple words, then the first word should be written in small case, then the first alphabet of every next word will be capital. Follow the same procedure and connect the rest of the Elements. Remember that for a text field and a label, the type is Outlet. However, while adding a button, the type must be action as shown in the screenshot below. At this stage, our viewcontroller.swift will look like − Now, inside your button action, add the following lines − var age = Int(enteredAge.text!)! * 8 yearsLabel.text = String(age); Tip − We do not need to add a semicolon to end a line in swift, but even if we put a semicolon, the compiler would not report any error. In the above code, the first line declares a variable ‘age’, which we will discuss in the next chapter. Then we assign the value entered by the user, by converting it into an Integer, and multiplying it by 8. In the second line, we assign the value of ‘age’ to our output label. At this stage, our view controller will look as follows − Now, we will run our application and this