Showing posts with label Swift. Show all posts
Showing posts with label Swift. Show all posts

Sunday, November 27, 2016

Objective Questions in Swift2.2 @iPhone - Set C


     
Q.21 : var arr = [4,5,6,7,”Ravi"]  
           Is a
A.         Valid Declaration and Initialization of an Array
B.         Compile Time Error
C.         Run Time Error
D.        None of the Above

Q.22 : What will be the output
          var array  = [11,22,33,44,55]
          print(array.length)          
A.         Compile Time Error
B.         Run Time Error
C.         5
D.        None of the above

Q.23 : What will be the output
           var array  = [11,22,33,44,55]
           print(array.count)
    A.         Compile Time Error
B.       Run Time Error
C.         5
D.       None of the above

Q.24 Which syntax is valid for array declaration

A.        var arr : Array
B.        var arr : NSArray
C.        Both
D.        None

Q.25: What will be the output
          var array  = [11,22,33,44,55]
         print(array[3])
          
A.         Compile Time Error
B.         33
C.         44
D.        22

Q.26:  What will happen
          var array  = [11,22,33,44,55]
          array.append(1)
          print(array)
          
A.         Compile Time Error
B.         [11,22,33,44,55,1]
C.         [11,22,33,44,1]
D.        [1]

Q.27: What will be the output
          var array  = [11,22,33,44,55]
          array.removeAtIndex[2]
          print(array)
          
A.         Compile Time Error
B.         [11,22,33,55]
C.         [11,22,44,55]
D.        [11,22,33,44]

Q.28: What will be the output
          var array  = [111,22,33,404,-55]
          array.sort()
          print(array)
          
A.         Compile Time Error
B.         [22,33,111,404,-55]
C.         [-55,22,33,111,404]
D.        [111,22,33,404,-55]

Q.29: Which syntax is valid for Dictionary
     
      A.  var dictionary : NSDictionary
      B. var dictionary : Dictionary
      C. Both
      D. None

Q.30 What will be the output
        var dictionary = [“Android” : “Mobile OS”, ”Chess” :       
                                      ”Game”, ”Ravi” : “Trainer”]
       print(dictionary[“Ravi”])

A.         Mobile OS
B.         Game
C.         Trainer
D.        Compile Time Error

Answers   A     A    C     B     C     B    C    C    A    C 

Saturday, November 19, 2016

How to Make a Simple TableView with iOS 9 and Swift2.2 @iPhone


If you are familiar with Android then you can consider  TableView in iOS as  ListView in Android.
To add  Table View in iOS the steps are
1. Create a new Project 
2. Add Table View in Main.storyboard
    2.1 : Click on Table View and set Prototype Cells 1 in the   
            property window of table 
    2.2 : Specify this cell with a name as an identifier ( i named as 
            mycell)
3.  Then click on Table with control and add dataSource and 
     delegate 
4. Create a reference of TableView in ViewController.swift 
5. Add UITableViewDelegate with you controller file as 
class ViewController: UIViewController , UITableViewDelegate
6.Add below mentioned function to add number of rows in your table 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
   return intValue;  //intValue means a valid integer
}
7. Add below mentioned function to customise cell with data source as 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    return cell; //cell should be created by you 
}

8. You should have a valid data source from where you can get the data

See my example i am going to display a Table  of you inserted Number.

Output will be like 


code is as 

//  ViewController.swift
//  Table of Number
//

//  Copyright © 2016 Ravi. All rights reserved.
//

import UIKit

class ViewController: UIViewController , UITableViewDelegate {
    var getData : Int = 0   // Simple Variable 
    @IBOutlet var editText: UITextField
                      // TextField for number  insertion
    @IBOutlet var tableView: UITableView!  
                       // Reference of Table
    @IBAction func clickMe(sender: AnyObject
                       // Action on Button click
    {
        getData = Int(editText.text!)! 
                           // Read Data from TextField
        tableView.reloadData()  // Each Button Click Reload
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, 
            typically from a nib.
    }

    func tableView(tableView: UITableView
           numberOfRowsInSection section: Int) -> Int
    {
        return 10    // 10 rows created 
    }
    func tableView(tableView: UITableView
           cellForRowAtIndexPath indexPath: NSIndexPath) -> 
           UITableViewCell
        
    {
        // Create Cell 

        let cell = UITableViewCell(style: 
                   UITableViewCellStyle.Default
                   reuseIdentifier: "mycell")
        
       // Update Cell
        cell.textLabel?.text = String (getData
                               (indexPath.row+1))
        return cell    // Return Cell
    }

}