Friday, November 25, 2016

Objective Questions in Swift2.2 @iPhone - Set A

Q.1: Which syntax is wrong in Swift2.2
A.  var str1
B.  var str2 : String
C.  var  str3 = “Hello “
D. var str4 : String  = “Hi….”


Q.2: What will be the output
var str = “Hello”
print(“Hello ”+str)
      A.         Error
      B.         Hello
      C.         Hello Hello
      D.        None of the above

Q.3: What will be the output
var int  = 9
int = int/2

     A.         4
     B.         Error 
     C.         3
     D.        4.5

Q.4: What will be the output
var  int : Int = 9
print(int)

     A.         Compile Time error
     B.         Run time error
     C.        
     D.        none of the above

Q.5: What will be the output
var int = 2
var num  : Double = 8.4
print(int * num)

      A.         Compile time error
      B.         Run time error
      C.         16.8
      D.        16

Q.6: What will be the output
var int = 2
print("The value is int ")

A. The value is 2
B. Compile time error
C. The value is int
D. None of the above

Q.7: What will be the output
var int = 2
print("The value is \(int) ")

A. The value is 2
B. Compile time error
C. The value is int
D. None of the above

Q.8 : var isFemale : Bool  = false  is a valid syntax or nor
A. Yes
B. No
C. May Be
D. None of the above


Q.9: What will be the output
var int = 2
var num  : Double = 8.4
print( num * Double (int))

      A.         Compile time error
      B.         Run time error
      C.         16.8
      D.        16

Q.10: Which is not a valid data type keyword in Swift2.2
A. Int
B. Bool
C. Char
D. Character




  A    C    A    C     A    C      A       A      C      C  



Sunday, November 20, 2016

Animation in iPhone


I had added added Animation with five images . 
Observe carefully the output. 
It's only the matter of coordinates arrangements and property settings.

Just Follow these steps  
1. Create Project
2. Take five ImageView on Layout
3. Create Image View References in ViewController
4. Observe the output carefully

A. Code is as 

import UIKit
class ViewController: UIViewController {
    @IBOutlet var img1: UIImageView!
    @IBOutlet var img2: UIImageView!
    @IBOutlet var img3: UIImageView!
    @IBOutlet var img4: UIImageView!
    @IBOutlet var img5: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, 
           typically from a nib.
    }
   
    override func viewDidLayoutSubviews() 
       // This function invoked before Layout become Visible 
    {
        img1.center = CGPointMake(img1.center.x ,     
                     img1.center.y + 1200// Bottom to Top
        img2.center = CGPointMake(img2.center.x - 400
                      img2.center.y// Left to Right
        img3.center = CGPointMake(img3.center.x + 600
                      img3.center.y )   // Right to Left
        img4.center = CGPointMake(img4.center.x
                      img4.center.y - 400) // Top to Bottom
        img5.alpha = 0 ;     // Disappear
    }
    
    override func viewDidAppear(animated: Bool
      // Just invoke after App Launched 
   {
        
  UIView.animateWithDuration(2// Animation Duration 2 secs
        {
        self.img1.centerCGPointMake(self.img1.center.x
                            self.img1.center.y - 1200 )
        self.img2.centerCGPointMake(self.img2.center.x
                            400 , self.img2.center.y  )
        self.img3.centerCGPointMake(self.img3.center.x
                            600 , self.img3.center.y  )
        self.img4.centerCGPointMake(self.img4.center.x
                            self.img4.center.y + 400 )
        self.img5.alpha = 1 ;
        }
    }

}



B. Output is as after 2 second of App launched



WebView in iPhone

As you know if WebView is widget to render web content in App.
In iPhone there is a UIWebView class which is used to embed web content in your App.

Steps are 
1. Create a project with WebView in Layout
2. Create a WebView reference in ViewController class
3. Create a String variable to store URL
4. Convert this string variable to NSURL
5.  Call methods with webView reference 

See my example i had added in two way 
1. First is direct load as shown in web apps
2. Second is to customise content - means as a string (generally 
    used to read data from web services)

1 in first approach direct load the output will be as 


2. Code is as 

import UIKit

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView! //Reference of WebView
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let baseUrl = "http://www.stackoverflow.com" 
                               // String constant
        let myurl = NSURL(string: baseUrl)!
                        // Convert it into NSURL 
        webView.loadRequest(NSURLRequest(URL: merl))
                        // load data to webView
  
    }

}



2. Second approach is better to work with string response 
a. Output


b. code is as 

//
//  ViewController.swift
//  WebEE
//
//  Copyright © 2016 Ravi. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let baseUrl = "http://www.stackoverflow.com"
        let myurl = NSURL(string: baseUrl)!

        let session = NSURLSession.sharedSession().
                      dataTaskWithURL(myurl) 
                      { (data, response, error) in
            
            if let getData = data
            {
                let webData =  NSString(data: getData, 
                             encoding: NSUTF8StringEncoding)
                
                dispatch_async(dispatch_get_main_queue(), 
                {
                    self.webView.loadHTMLString
                          (String(webData!), baseURL: nil)
               })
                
            }
            else
            {
               print("Errors....")
            }
            
        }
        session.resume()
    }

}




Saturday, November 19, 2016

Permanent Storage with Default System in iPhone

Sometimes we required to store our choices with the Device
like:
 Language Selection ,  Units of Measurement etc

With iPhone you can do all these by using class 
 NSUserDefaults , it provides a programmatic interface for interacting with the defaults system. 
The defaults system allows an application to customize its behavior to match a user’s preferences. 

You can store six types 
1. NSData
2. NSString
3. NSNumber ( Int ,Float , Double , Bool )
4. NSDate
5. NSArray
6. NSDictionary

For Writing the way will be as 
let data = NSUserDefaults.standardUserDefaults(); // Default
data.setObject("Value for Key ", forKey: "KeyName")
     // Set Value for the Key 


Like i am going to add my First Name  as Ravi with key name
data.setObject("Ravi", forKey: "name")

For Reading the way will be as 

let data = NSUserDefaults.standardUserDefaults();
let getData =  data.objectForKey("fname")!  

Now getData can be used for any purpose as per your App Requirements.

The another Methods are 
  • func setBool(Bool Value , "KeyName")
  • func setInteger(Int Value ,  "KeyName")
  • func setFloat(Float Value ,  "KeyName")
  • func setDouble(Double Value ,  "KeyName")
  • func setObject(value: AnyObject?,  "KeyName")
  • func setURL(url: NSURL?, f "KeyName")
keyName should be String.
      




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
    }

}