Friday 21 August 2015

Swift String

Swift's String and character types provide a fast, Unicode- complaint way to work with text in our code. The  syntax for string creation and manipulation is lightweight and readable, with a string literal syntax that is similar to C.

Swift's String type is a fast, modern string implementation. Every string is composed of encoding- independent. Unicode characters, and provides support for accessing those characters in various Unicode representations.

Swift’s String type is bridged with Foundation’s NSString class. If you are working with the Foundation framework in Cocoa, the entire NSString API is available to call on any String value you create when type cast to NSString, as described in AnyObject. You can also use a String value with any API that requires anNSString instance.

1) To create empty string literal:

      var emptyString = ""

2) To initialize a string use the initializer syntax:

     var  strName = String()

3) For example:

    var strName = "Jitendra"

   if strName. isEmpty {
     println("string is empty")
}
else{

println("string is not empty")
}

Swift's String type is  a value type. If you create a new String value, that String value is copied when it is passed to a function or method, or when it is assigned to a constant or variable. In each case, a new copy of the existing String values is created, and  the new copy is passed or assigned, not the original version.

4) Swift’s String type represents a collection of Character values in a specified order. You can access the individual Character values in a string by iterating over that string with a for-in loop.

example: 
              for character in strName{
              println(character)
             }

5) To retrieve a count of the Character values in a string, call the global count(_:) function and pass in a string as the function’s sole parameter.

6) String values can be added together with the addition with the addition operator(+) to create a new String value : 

  let firstName = "Jitendra"
  let lastName = "Solanki"

  var fullName = firstName + lastName

Working with index:

Each String value has an associated index typeString.Index, which corresponds to the positions of each Character it contains.

As mentioned above, different characters can require different amounts of memory to store, so in order to determine which Character is at a particular position, you must iterate over each Unicode scalar from the start or end of that String. For this reason, Swift strings cannot be indexed by integer values.

7) Use the startIndex property to access the position of the first Character of a String, and the endIndexproperty to access the posision of the last.

8) A String.Index value can access its immediately preceding index by calling the predecessor() method, and its immediately succeeding index by calling the successor() method.

9) Use the global function indicies(_:) to create a Range of all of the indexes used to access individual characters in a string.


Inserting and Removing :

10) To insert a character into a string at a specified index, use the insert(_:atIndex:) method.
  • var welcome = "hello"
  • welcome.insert("!"atIndexwelcome.endIndex)
11) To insert another string at a specified index, use the splice(_:atIndex:) method.
  •         welcome.splice(" there", atIndex: welcome.endIndex.predecessor()

12) To remove a character from a string at a specified in index, use the removeAtIndex(_:)method.
13) To remove a substring at a specified range, use the removeRange(_:)method:
    example:
                let range = advance(fullName.endIndex, -6)..<fullName.endIndex
               fullName.removeRange(range)

Comparing Strings:

Swift provides three ways to compare textual values: string and character equality, prefix equality, and suffix equality.

14) String and character equality is checked with the “equal to” operator (==) and the “not equal to” operator (!=), 

Prefix and Suffix Equality:

15) To check whether a string has a particular string prefix or suffix, call the string’s hasPrefix(_:) and  hasSuffix(_:) methods, both of which take a single argument of type String and return a Boolean value.

Friday 7 August 2015

Integrate Objective C code into Swift

Apple provide a way by which we can use the code written in Objective C to our swift  project and call methods as they were part of this swift project.

It's very easy to create a new objective c file in swift project or use existing Objective C file in our Swift project.

To understand whole process step by step , lets start a new project in XCode and set language to Swift .

1) After creating simple project we get the list of inbuilt files in Xcode for our project. Now we can either create a new Objective C .h & .m file in this project or we can add existing Objective c file directly into our project .

2) Create a new Objective C .h & .m file :

      A) Right click on project and choose new File .
     
      B) Select Objective C file and click next .

      C) Give an appropriate name to your file, for example it is DemoObjC and create it .

      D) After Clicking Create button XCode ask for configure an Objective -C bridging header. You                must say YES . 
           The given dialogue box shown only once when first Objective -C file creates in Swift Project. 

configure an objective


Now we have an Objective- C bridging header and DemoObjC.m files in our project . But we don't have  DemoObjC.h in our project, so create a header file and named it same as DemoObjC .m .

    E) Right Click on project and choose New File .

    F) Choose header file and click Next .

   G) Give file name , this name must be same as a .m file which is previously created .

    Now we have both  .m & .h file, do code now of your requirement .

After writing code in your objective c file , you have to connect it with swift environment with  the help of Bridging Header . Simply import this newly created Objective C file into Bridging Header and that's all .


Now to use the code of this objective C file, simply create object of this  and call the methods .

Lets assume we have a method name doWelcome:text(NSString*), to call this method from swift code, just do :

var demoObj =  DemoObjC()
demoObj.doWelcome("WelCome To  Swift")


3) Add existing Objective C file in Swift project :

    A) Right Click and click Add Files to ..

    B) Choose your folder and select both .h & .m  file make sure that checkbox of copy item if needed is checked and click Add.
  
   C) To use code from this file we have to add the header of this file to Bridging Header.


That's all , how simple it is to  combine the code from Objective C and Swift .