• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • About
  • Portfolio
  • Privacy Policy

Archives for May 2015

How to reactivate skype credit

May 11, 2015 By Ravi Shankar 1 Comment

Skype credit gets automatically de-activated after 180 days unused period. But you can reactivate the credit by login to Skype website. Listed below are the steps to reactivate Skype credit.

Step 1: Launch your Skype Mac App.

Step 2: Click Add Credit option below your Skype username

201505112149.jpg

Step 3: Click the link next to Manage account. This should take you to the Skype Account by launching your default web browser.

201505112150.jpg

Step 4: Navigate to Your Skype Credit is inactive and click the link with caption as Reactivate it now

201505112153.jpg

Step 5: In the Reactivate Skype Credit screen, click Reactivate Credit button to get back your unused Skype Credit.

201505112155.jpg

Now you should see the remaining Skype credit under your user name.

201505112158.jpg

Filed Under: General Tagged With: Reactivate Credit, Skype, Unused Credit

Class and Struct in Swift

May 10, 2015 By Ravi Shankar 3 Comments

Download the playground file from github (Classes and Struct)

Class

A class is a blue print for a real-word entity such Player, Person etc. and it is used for creating objects. Class can have properties to store values and methods to add behaviour. Let us see this with an example class called Rectangle which has some properties and two methods for calculating area and for drawing a rectangle.

[code language=”swift”]class Rectangle {

var name:String = ””
var length:Double = 0
var breadth:Double = 0

func area() -> Double {
return length * breadth
}

func draw() -> String {
return “Draw rectangle with area \(area()) “
}
}

let rect = Rectangle()

rect.length = 20
rect.breadth = 10
rect.draw()
[/code]

 

In the above example, we have a class named Rectangle, with name, length and breadth as properties, area and draw are functions. rect is a instance variable or object of Rectangle class. On setting the length and breadth and calling draw function should provide the following output in Playground.

201505101309.jpg

Similarly the below code create a Square class

[code language=”swift”]class Square {

var name:String = ””
var length:Double = 0

func area() -> Double {
return length * length
}

func draw() -> String {
return “Draw a square with area \(area()) “
}
}

let squr = Square()
squr.length = 20
squr.draw()
[/code]

 

Now instead of repeating property and functions in each classes let us use class inheritance to simplify these classes.

Class Inheritance

Let us create a parent class called Shape and its properties and functions will be inherited by Sub Classes Rectangle and Square.

Parent Class – Shape

[code language=”swift”]class Shape {
var name: String = ””

func area() -> Double {
return 0
}

func draw() -> String {
return “Draw a \(name) with area \(area()) “
}
}
[/code]

 

Sub Class – Square

[code language=”swift”]class Square:Shape {

var length: Double = 0
override func area() -> Double {
return length * length
}
}

let squr = Square()
squr.name = “My Square”
squr.length = 5
squr.draw()
[/code]

 

Sub Class – Rectangle

[code language=”swift”]class Rectangle:Shape {
var length: Double = 0
var breadth: Double = 0

override func area() -> Double {
return length * breadth
}
}

let rect = Rectangle()
rect.name = “My Rectangle”
rect.length = 5
rect.breadth = 10
rect.draw()
[/code]

 

Parent class Shape has been created with name property and functions area and draw. The child class Square and Rectangle will inherit these property and methods. Apart from the parent class property, Square can have its own property length and Rectangle has length and breadth. The parent class area function has been overridden by Square and Rectangle class to calculate corresponding areas. Now if you want add one more Shape such as Triangle, Circle etc the new class has to inherit Parent class (Shape) and add its own property and methods (or override methods).

Initialisers

Initialisers in Class and Struct are used for setting the default values for properties and for doing some initial setup. Here is a typical example of initialiser in a Class where the name property is initialised at the time of creating an instance.

[code language=”swift”]class Shape {

var name: String
init(name: String) {
self.name = name
}

func area() -> Double {
return 0
}

func draw() -> String {
return “Draw a \(name) with area \(area()) “
}
}

class Square: Shape {
var length: Double = 0

init() {
super.init(name: “MySquare”)
}

override func area() -> Double {
return length * length
}

override func draw() -> String {
return “Draw a \(name) with area \(area()) “
}
}

let squr = Square()
squr.length = 10
squr.draw()
[/code]

 

The sub class Square initialises the name property in init function by calling super.init and after creating the Square instance you need to pass value for the length property.

Designated and Convenience Initialisers

Initialiser which initialises all the properties in a class is known as designated initialiser. A convenience initialiser will initialise only selected properties and in turn will call the designated initialiser in init function. Listed below is a Square class with designated initialiser and convenience initialiser

[code language=”swift”]class Shape {
var name: String
init(name: String) {
self.name = name
}

func area() -> Double {
return 0
}

func draw() -> String {
return “Draw a \(name) with area \(area()) “
}
}

class Square: Shape {
var length: Double

// Designated Initializer
init(length:Double, name:String) {
self.length = length
super.init(name: name)
}

// Convenience Initializer
convenience init(length: Double) {
self.init(length:length, name:“MySquare”)
}

override func area() -> Double {
return length * length
}

override func draw() -> String {
return “Draw a \(name) with area \(area()) “
}
}

let squr = Square(length: 10,name: “MySquare”)
squr.draw()

let squrNew = Square(length: 20)
squrNew.draw()[/code]

Computed Property

A property in swift can be used for performing operation at the time of assigning value. Here is an example, where the length of Square is computed based on assigned area.

[code language=”swift”]class Sqaure {
var length: Double = 0
var area: Double {
get {
return length * length
}

set (newArea) {
self.length = sqrt(newArea)
}
}
}

let square = Sqaure()
square.area = 4 // set call
square.length = 6
square.area // get call[/code]

lazy Property

Swift also provides lazy property whose value is assigned when the user access the property.

[code language=”swift”]class Person {
var name: String
init (name: String) {
self.name = name
}

lazy var message: String = self.getMessage()

func getMessage() -> String {
return “Hello \(name)”
}
}

let person = Person(name: “Jason”)
person.message[/code]

in the above code example, the value for message property is not set at the initialisation and will be set only when call the message property in person object. Some typical where you could due lazy property is when retrieving values from performance intensify operation such as Network or Read/Write.

Property Observers

Swift provides two property observers, willSet and didSet. These methods gets triggered when a value is about to be set for a property or after setting the property.

[code language=”swift”]class Square {
var length: Double = 0 {
willSet(newLength) {
println("Setting length \(self.length) to new length \(newLength)")
}

didSet {
println(“Length is modified – do some action here”)
}
}

var area: Double {
get {
return length * length
}

set (newArea) {
self.length = sqrt(newArea)
}
}
}

let square = Square()
square.length = -6
square.area
[/code]

 

In the above example, Square class length property has a willSet and didSet observers.

Struct

Struct and Class can both have properties, methods, protocols, extensions and initialisers. You can use struct to hold simple values and when you want to pass around those across your program. A typical example would be using Struct to hold values from a web service call. Listed below is a web service call that we had seen earlier in Xcode and Playground overview. In the below code example you should find a GeoDetails struct for storing the values returned from geoip web service.

[code language=”swift”]struct GeoDetails {
var country: String
var ip: String
var isp: String
var latitude: Double
var longitude: Double
var timeZone: String

init(country: String, ip: String, isp: String, latitude:Double, longitude:Double, timeZone:String) {
self.country = country
self.ip = ip
self.isp = isp
self.latitude = latitude
self.longitude = longitude
self.timeZone = timeZone
}

func description() -> String {
return “Country ” + self.country + “, ip ” + self.ip + “, isp ” + self.isp + “, latitude \(self.latitude), longitude \(self.longitude) “
}
}

var geoDetails: GeoDetails?
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)

let url = NSURL(string: “http://www.telize.com/geoip”)

NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if error == nil {
var error:NSError?
if let result = data {
if let dict = NSJSONSerialization.JSONObjectWithData(result, options: NSJSONReadingOptions.AllowFragments, error: &error) as? NSDictionary {
geoDetails = GeoDetails(country: dict[“country”] as! String, ip: dict[“ip”] as! String, isp: dict[“isp”] as! String, latitude: dict[“latitude”] as! Double, longitude: dict[“longitude”] as! Double, timeZone: dict[“timezone”] as! String)
println(geoDetails?.description())
} else {
println("Error")
}
}[/code]

Download the playground file from github (Classes and Struct)

Filed Under: Apple, Programming Tagged With: Apple, Classes, Structures, Swift

Swift – Beginners Tutorial

May 8, 2015 By Ravi Shankar Leave a Comment

Swift is the latest programming language released by Apple for developing OS X and iOS apps.

  • Best of C and Objective-C
  • Adopts safe programming patterns and modern features
  • Supports Playground, a tool for seeing the result immediately.
  • Provides access to Cocoa libraries and can work in conjunction with Objective-C
  • Combines Procedural and Object-Oriented programming.
  • No need to use semicolon at the end of statement. Use it only when you have more than one statement in single line.
  • Swift uses var and let. only mutable variable needs var.
  • Swift uses type inference.
  • Supports unicode characters. You can use any character as variable.
  • Prefer usage of constant (let) for immutable than using var.
  • Optional variables can contain value or nil. – var givenName : String? = “Ravi”
  • Swift’s Switch supports all kinds of datatype and operations and does not need a break statement after each case statement.
  • No need to enclose your expression in brackets with if statements
  • Swift function supports default value for the parameter and variable parameter.
  • Closures are like blocks in Objective-C ( ) -> ( )
  • No need to specify header file in Swift
  • No need to specify base class and there is no universal base class
  • No difference between instance variable and properties
  • Tuples – Grouping of multiple values.
  • Nested multiline comments are allowed.
  • Use typealias to provide different name to an existing type.
  • Swift nil represents absence of value and objective – C nil represents pointer to a non-existent object.
  • Implicitly unwrapped optional let pincode : String! = “E151EH”
  • Provides Assertion to end code execution where certain criteria is not met.
  • Swift’s String is value type and not passed by reference.
  • Supports Optional Binding and Optional Chaining

Variables and Constants

[code language=”swift”]
// Variables and Constants

var myStr = "Swift"
var myValue = 23.1 //(Implicit variable declaration or type inference)
var myDoubleValue: Double = 23 //(Explicit variable declaration)

// let

let myAge = 38
let message = "My age is " + String(myAge) //(Converting value to a String)
let newMessage = "My age is \(myAge)" //(Converting value to a String using backslash or interpolation)

[/code]

Data types in Swift

  • String
  • Int (Range -2,147,483,648 to 2,147,483,648)
  • Double (15 digit precision)
  • Float (6 digit precision)
  • Bool

Fun with String

[code language=”swift”]// String
var movie:String = "Independence Day "
count(movie) // count of string

// Use NSString to format a double or float value
var range = NSString(format: "%.2f", 24.5)

// Concatenate String values
movie += String(range)[/code]

 

Collection Types

Array

[code language=”swift”]// Declarations
// var fruits = ["Orange", "Apple", "Grapes"] – Short declartion
// var fruits:Array = ["Orange", "Apple", "Grapes"] – Long declaration
// var fruits:[String] = [] – Assign empty array

var fruits:[String] = ["Orange", "Apple", "Grapes"] // short declaration with type.

// insert item at index
fruits.insert("Mangoes", atIndex: 2)

// append item to the last
fruits.append("Pine Apple")

// count of array
fruits.count

// remove item
fruits.removeAtIndex(1)

// sort array elements
fruits.sort { (a, b) -> Bool in
a < b
}

// retrieve index using find
find(fruits, "Mangoes")
[/code]

Dictionary

[code language=”swift”]// Dicionary

// Declaration

// var employees = [1:"John",2:"Peter",3:"David"] // Short form

// var employees:Dictionary = [1:"John",2:"Peter",3:"David"] // Long form

// var employees:[Int:String] = Dictionary() // Empty dictionary

var employees:[Int:String] = [1:"John",2:"Peter",3:"David"] //Short form with type

// Add new item to dictionary

employees[4] = "Bob"

// Remove an item using key

employees.removeValueForKey(3)
[/code]

Assignment Operator

  • a = b
  • let (a,b) = (2,3) – supports tuple.
  • Does not return value.

Arithmetic Operators

  • Addition (+), Subtraction (-), Multiplication (*), Division (/)
  • + can be used for string concatenation.
  • % – Returns remainder for both +ve and -ve numbers. Also returns remainder for floating point numbers.
  • Increment and Decrement operators, ++i (i = i + 1), —i (i = i – 1).
  • Supports i++ and i— (increments or decrements after returning the value).
  • Unary Minus and Unary Plus

Compound AssignmentOperator

  • x += 2 is same as x = x + 2.

ComparisonOperators

  • x == y
  • x != y
  • x > y
  • x < y
  • x >= y
  • x <= y
  • === and !== used for testing object references.

Ternary Conditional Operator

  • a = flag ? 10 : 20 (if flag is true then a will updated to 10 and 20 incase it is false).

RangeOperators

  • Closed Range – e.g.:- 1…10, has three dots and includes values from 1 to 10.
  • Half Closed – e.g.:- 1..10, has two dots and includes values from 1 to 9

LogicalOperators

  • NOT ( !x )
  • AND ( x && y )
  • OR ( x || y )

Control Flow

[code language=”swift”]// Control flow

// if else
if fruits[0] == "Grapes" {
println("for breakfast")
} else if fruits[0] == "Apple" {
println("for lunch")
} else {
println("Nothing")
}

// for statements
// exclusive range

for index in 0..<h3><u>Comments</u></h3>[code language="swift"] Single line comments // examples
Multiline comments /* example1
example2 */

[/code]

Function

[code language=”swift”]func sum(number1:Int, number2: Int) -&gt; (Int) {
return number1 + number2
}[/code]

The above function is an example of multiple input parameters. It does the addition of two numbers where number1 and number are arguments of type Int and it returns a value of type int. And a function without parameter looks like this.

[code language=”swift”]
func sum() -&gt; (Int) {
return 10 + 5
}[/code]

The above function is an example of multiple input parameters. It does the addition of two numbers. number1 and number are arguments of type Int and it returns a value of type int. And a function without parameter looks like this. Swift function can also have multiple return values.


Define external parameter name

[code language=”swift”]func sum(addNumber1 number1:Int, withNumber2 number2: Int) -&gt; (Int) {
return number1 + number2
}

println(sum(addNumber1: 10, withNumber2: 20))
[/code]

 

addNumber1 and withNumber are external parameter names for two parameters. And you use # to tell local and external parameter name are same.

[code language=”swift”]
func sum(#number1:Int, #withNumber2: Int) -&gt; (Int) {
return number1 + withNumber2
}
println(sum(number1: 10, withNumber2: 20))[/code]

Function with default parameter value

[code language=”swift”]func sum(number1:Int, withNumber2: Int = 20) -&gt; (Int) {
return number1 + withNumber2
}
println(sum(10))[/code]

The above function has default value for the second parameter.

Variadic Parameters

 

A function with variadic parameters can accept zero or more values. Maximum of one parameter is allowed in a function and it is always last in the list.

[code language=”swift”]
// Variadic parameters
func totalSum(numbers:Int…) -&gt; Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
totalSum(1,2,3,4,5)[/code]

Variable and inout parameters

Variable parameters in a function are indicated by var keyword and the scope is available only within the function. If you want to access the modified variable value outside the function then you specify them as inout parameter. And prefix with & while passing the parameter in the function call.

[code language=”swift”]
// inout parameters
var employee = "Ravi"

func greetings(inout employee:String) {
employee += "!"
}
println(greetings(&amp;employee))
println(&amp;employee)[/code]

Filed Under: Apple, ios, Mac Tagged With: Apple, Quick Reference, Swift

Optional binding and Optional Chaining

May 7, 2015 By Ravi Shankar 6 Comments

Swift has a feature that lets users to assign optional value to a variable or a constant. Optional variable or constant can contain a value or a nil value. Let us take the following example which tries to find a given string in a array of string.

Optional Binding

[code language=”swift”]var fruits = ["Apple","Orange","Grape","Mango"]
let searchIndex = find(fruits, "Apple”)[/code]

The searchIndex would return value if the fruit exists or nil value if it doesn’t exist.

[code language=”swift”]println("Fruit index is \(searchIndex)”)
[/code]

 

The proper way to handle this by using Optional binding method.

[code language=”swift”]if let searchIndex = searchIndex {
println("Fruit index is \(searchIndex)")
} else {
println("Not available")
}[/code]

This would ensure only when searchIndex has a value the println with searchIndex gets executed.

Optional Chaining

Optional chaining is the way by which we try to retrieve a values from a chain of optional values. Let us take the following example classes.

[code language=”swift”]class School {
var director:Person?
}

class Person {
var name: String = ""
init(name: String) {
self.name = name
}
}
[/code]

 

[code language=”swift”]
var school = School()
var person = Person(name: "Jason")
school.director = person
school.director?.name
[/code]

The director property in School class is optional, when you try to access subsequent values from director property becomes optional (? mark after director when accessing name property). You can handle these optionals as shown below.

[code language=”swift”]
if let name = school.director?.name {
println("Director name is \(name)")
} else {
println("Director yet to be assigned")
}[/code]

Filed Under: Apple, ios Tagged With: Apple, Optional bindings, Optional chaining, Swift

How to change text case in Word 2013 and Word 2010

May 6, 2015 By Ravi Shankar Leave a Comment

Microsoft Word 2013 and Word 2010 allows users to change the CASE of the selected text using the Change Case option available as part of the Home menu. For example if you have the following text in your word document and you want to change the CASE by capitalizing each word.

How to change the text case in Word 2010

Then select above the text in the Word document and navigate to Font section in Home menu. Click the drop down arrow below Aa.

Word 2010 Font section

This would display the following menu option

CASE options in Word 2010

Select Capitalize Each Word option from the list of available menus. Similarly the other CASE related options includes changing the selected text to the following

  • Sentence case
  • lowercase
  • UPPERCASE
  • toGGLE  case

Filed Under: Office 2010, Word 2010 Tagged With: Capitalize First Word, Change Case, Change Text Case, Word 2010

Change the page colour in Word 2010 and Word 2013

May 5, 2015 By Ravi Shankar 2 Comments

The standard page colour of a word document is white. But in Word 2013 and Word 2010, you can also change the page colour using the Page Layout menu option. You can do the following to change the word document page colour in Word 2010.

From the File menu, click Page Layout menu option

Navigate to Page Background section.

Page Background Section in Word 2010

To change the page colour, click the drop down arrow below the Page Colour option select your desired colour.

Page Colour in Word 2010

If you want to see more colours, then click the More Colours option to launch the Colour Palette.

Page Colour Palette in Word 2010

If you want fill with effects then click Fill Effects menu to bring the Fill Effect dialog box.

Page Fill Effects in Word 2010

Filed Under: Office 2010, Word 2010 Tagged With: Change Page Colour, Fill Effects, Page Colour, Word 2010

Changing column width in excel 2013 and excel 2010

May 4, 2015 By Ravi Shankar 1 Comment

There are different ways for changing the column width of cell in Excel 2010 and Excel 2013. Some of them are listed below

Double Clicking Column Header

The easiest one for adjusting the column width is to double click the Column Header of cell whose width needs to be changed depending on the longest length of text entered in the Cell. The mouse pointer needs to placed on the border line before doing a double click

Using Mouse Drag and Drop

Mouse Drag and Drop Column Width

Another way for changing the column width is to use mouse drag and drop feature and adjust the cell column width depending upon the requirement.

Using Format menu option

Excel also provided option for changing the column width by using the Format menu option. In the home menu, navigate to Cells section and click the drop down arrow below the Format menu option.

image 

This would display the following list of available menus.

image

To Change the column width of the selected cell, select Column Width from the available menus and enter the desired column width for the cell in Column width text box.

image

And if you want to change the width for all the columns in the Worksheet then select Default Width menu from Format menu option and specify the Standard Width in Standard column width text box.

image

Filed Under: Excel, Excel 2010, Office 2010 Tagged With: Adjust, Cell Width, Change Column Width, Excel 2010, Standard Width, Worksheet

Change message format in Outlook 2007, 2010, 2013 and Outlook 2011 for Mac

May 2, 2015 By Ravi Shankar 3 Comments

Microsoft Outlook allows users to receive and send emails in their preferred email format. The three email formats provided to the users are HTML, Rich Text and Plain Text. This tutorial is about the steps required to change email format in Outlook.

Change message format in Outlook 2010 and Outlook 2013

The message format of email message in Outlook 2010 and Outlook 2013 can be changed using the Format Text menu option. This option is available as part of the compose new message window. To access the Format Text option, In the new message window, navigate to Format Text menu and then to Format section.

format Text in Outlook 2010 and Outlook 2013

And using HTML, Plain Text and Rich Text menu options, one change the Format while composing a new email message.

HTML

This email messages can contain formatting and compatible with most of the email readers.

Plain Text

Plain Text messages contain no formatting but can be read by every one.

Rich Text

Rich Text messages contain formatting but are compatible with Microsoft Outlook and Microsoft Exchange.

Outlook 2010 also provides option for changing the default format for new messages. This can be done using the Mail Options. To change the format for compose messages, click File menu and then Options link. In the Outlook Options window, click Mail Options and then navigate to Compose messages section.

Compose messages in this format in Outlook

Using the Compose messages in this format, you can set the default format for new messages. After selecting desired value, click OK to apply and save the changes.

Message format in Outlook 2007

In Outlook 2007 message or mail format can be changed to any of the following three types

  • HTML
  • Rich Text
  • Plain Text

These options can be set depending upon user preferences. This setting can be accessed using Tools -> Options menu.

The options dialog box will display a Mail Format. Under Mail Format tab, the Message format contains drop down control – Compose in this message format, where you can specify format values such as HTML, Plain Text or Rich Text.

Compose in message format in Outlook 2007

Change email message formatting in Outlook 2011 for Mac

Outlook 2011 for Mac allows users can send a new email message either in HTML or Plain Format. Users can change the email message format using the Message Options.

New message Outlook 2011

Click the Create a new message icon available under the Home menu. Then click the Options tab in the New Message Window.

Message Options Outlook 2011

The default composing email format in Outlook 2011 is set to HTML. If you want to change the format to plain then click the grey button next HTML.

Change new message format Outlook 2011

Similarly if you want to change it from Plain to HTML then click the grey button before Plain

Outlook 2011 Preferences

Outlook 2011 for Mac also provides users with option for changing the default HTML format to Plain. Click the Outlook menu and select Preferences from the menu list.

201110101845.jpg

In the Preferences window, click Composing available under Email preferences.

Composing Preferences in Outlook 2011

Navigate to Format and account section under Composing Preferences and un mark the check box with label as Compose messages in HTML by default. This would turn off the default HTML format for new composing message.

HTML message format in Outlook 2011 for Mac

Also See: How to always read mail in plain text using Outlook 2010

Filed Under: MS Office, Outlook 2007, Outlook 2010, Outlook 2013 Tagged With: HTML message, Message Format, Microsoft Outlook, Plain text, Rich Text message

  1. Pages:
  2. «
  3. 1
  4. 2
  5. 3
  6. »
« Previous Page
Next Page »

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to know the size of the folders in iCloud
  • Errors were encountered while preparing your device
  • We have blocked all requests from this device – Firebase Phone Authentication
  • iPhone is not available error message in Xcode
  • Clear CocoaPods cache, re-download and reinstall all pods

Copyright 2021 © rshankar.com