Understanding Constructors & Initialization in Swift

This video is best viewed at 720p

Advertisements

COLLECTION TYPE, SEQUENCE TYPE & INDEXABLE TYPE – Update

This is an update to the topic covered earlier. You can read about the Protocols in detail in the original article. Collection Type, Sequence Type & Indexable Type

Most of the things are the same here are some of the changes:

  1. The Indexable protocol is now not necessarily required. All the aspects of the indexable protocol now fall under the Collection Protocol
  2. The names of the protocols have changed from SequenceType & CollectionType to Sequence & Collection
  3. The keyword Generator has been renamed to Iterator. Accordingly the generate function has been renamed makeIterator function.
  4. The collection protocol now requires the implementation of the function index, this function returns the value of the next index.

The sample code below should clarify

class CustomStack
{
    var data : [Element] = [Element]()

    func push(Element newElement : Element)
    {
        data.append(newElement)
    }

    func pop() -> Element
    {
        return data.removeLast()
    }
}

//Additional Implementations - not strictly required
extension CustomStack
{
    typealias Index = Int

    var startIndex : Index
    {
        return 0
    }

    var endIndex: Index
    {
        return (data.count - 1)
    }

    subscript (position : Index) -> Element
    {
        return data[position]
    }
}

extension CustomStack : Sequence
{
    typealias Iterator = AnyIterator

    func makeIterator() -> Iterator
    {
        var index = 0
        return AnyIterator({() -> Element? in
            if index < self.data.count
            {
                let res =  self.data[index]
                index += 1
                return res
            }
            return nil
        })
    }
}

extension CustomStack : Collection
{
    typealias SubSequence = CustomStack

    subscript (bounds: Range) -> CustomStack.SubSequence
    {
        let newStack : CustomStack = CustomStack()

        for i in bounds.lowerBound...bounds.upperBound
        {
            newStack.push(Element: data[i])
        }
        return newStack
    }

    /// Returns the position immediately after the given index.
    /// - Parameter i: A valid index of the collection. `i` must be less than
    ///   `endIndex`.
    /// - Returns: The index value immediately after `i`.
    func index(after i: Int) -> Int
    {
        if i < endIndex
        {
            return i + 1
        }
        return i
    }
}

When to use Swift & when to use Objective-C?

Over the past few years I have received a number of questions with regards to Swift & Objective-C. Specifically related to the future of the 2. I will try to address those questions in the form of an FAQ.

Should I learn Swift or Objective-C?

This is a question that I get from developers new to iOS/macOS App Development. Ideally speaking, you should learn Swift. As that is going to become the main language for App development on Apple’s ecosystem. However, the reality is a little different. There are a large number of applications that are written in Objective-C. You are likely to encounter them at your workplace. You may also have to maintain, upgrade & improve those apps. In such a case, it makes sense to learn Objective-C too.

Can I mix Swift & Objective-C in the same project?

Yes! But remember that you should check for feature compatibility between the 2 languages. Adding Swift code to an Objective-C project may not be very beneficial as only those features that are compatible with Objective-C can be written in Swift.

Going the other way round is not a problem. You can read more about that here:Mixing Swift & Objective-C

Will Objective-C be deprecated in the future?

That is an interesting question. There is no formal announcement from Apple stating the Objective-C is going to be deprecated. However, one can expect more attention to be paid to Swift. That is where most of the newest techniques, tools & technologies are going to be available. Objective-C will keep running as it is as of now.

Can I mix Swift with other Programming Languages?

Swift can easily be mixed with Objective-C. If you wish to incorporate C++ or C code in your Swift Project then wrapping them in Objective-C code allows you to achieve this.

Apart from that Swift does support working with C code code. You can read about that here:Interacting with C APIs.

Swift does not provide interoperability support for any other languages as of now.

Which version of Swift should I use?

It is recommended that you use the latest available version of Swift. However, the actual version that you work on depends on many other factors like: compatibility with OS Versions, support & business related choices.

Why shouldn’t we just convert all our Objective-C code to Swift and keep things simple?

A very tempting proposition. However, practical realities prevent us from doing this. The process of converting from Objective-C to Swift is time consuming. Apart from having to convert the syntax, the code also needs to be optimised taking into account the new features that are available. This will mean extensive testing and quality assurance. Most companies will not invest their resources into this endeavour.

A better approach is to migrate to Swift gradually. Here are some ways to do this:

  1. If its a brand new product/app that you are creating, start it in Swift.
  2. Any new reusable code components that are being created should be done in Swift (they should be Objective-C compatible if you intend to use this code in Objective-C projects).
  3. If any part of a product is going to undergo heavy change, either due to a bug fix or a new feature. This is a good time to convert it into Swift.

A good example is how Apple is approaching the process of migrating to Swift. They are doing it component by component.

I have been developing apps in Objective-C for some time. I am able to create any reasonably complicated app now. If Objective-C hasn’t been deprecated then should I start making apps in Swift?

This is a choice that you have to make. It is recommended that new apps (at the very least) be made in Swift as that is the language that will undergo the maximum amount of changes & improvements in the future.

What do you suggest as a trainer?

Another question that I get very often. It depends on the situation. I would say learn both Swift & Objective-C. You can skip learning Objective-C if you are confident that you will not have to work with any projects written in that language.

If I am starting on a brand new project I would use Swift. But if its an Objective-C project I would stick to Objective-C.

Can Swift development only be done on macOS?

No! Swift development can also be done on Linux. However, iOS/macOS/tvOS/watchOS App Development can only be done on macOS through Xcode.

How should I migrate to Swift?

There are different approaches that one can use. It all depends on the situation and needs of your organisation. Here are some things that you can do:

  • Start development of brand new apps (from scratch) in Swift.
  • If you are creating a brand new library which will be used for future projects then go ahead with Swift.
  • If a major component of an existing app is going to be changed significantly then you can go ahead with Swift.

You can do all or some of the above. There may be other strategies too. You should also factor in the cost of migration from one language to another.

 

Collection Type, Sequence Type & Indexable Type

This is for Swift Version 2.2 & earlier. I will be adding the snippet of code for the changes the Swift 3.x have introduced.

What are the Collection Type & Sequence Type Protocols?

The Collection Type, Sequence Type & Generator Type Protocols define rules that govern how different data structures or collections of data can be used, interacted with and operated within the Swift programming language. The CollectionType is a special case of the SequenceType.

Why do we need such Protocols?

Lets take the example of the Swift For-Loop.

var arrOfStrings : [String] = [String]()

arrOfStrings.append("Jill")
arrOfStrings.append("Jack")
arrOfStrings.append("John")
arrOfStrings.append("Jane")

for name in arrOfString
{
     print("The name is \(name)")
}

Now, if we have created our own data type. We would not be able to use the above for-loop as it would not conform to the … type protocols. The for-loop is expecting a data structure that acts and behaves in a way that is governed by the … protocols.

Just like the for-loop example above there are many other features within the Swift Programming Language that expect data structures to act and behave in a particular way. By designing our data structures to conform to these protocols we can make the easily compatible with the existing code and language features out there.

How do we use these protocols for our own data structures?

First we need to decide what kind of collection are we making. For the sake of this example I will create a Custom Stack.

class CustomStack<Element>
{
    var data : [Element] = [Element]()

    func push(Element newElement : Element)
    {
        data.append(newElement)
    }

    func pop() -> Element
    {
        return data.removeLast()
    }
}

The above code is very simple for the purpose of this exercise. Its a stack. Which is internally really an Array. It has functions to push data and pop data. We are now going to convert this type to a collection to conform to the CollectionType protocol.

Implementing the Indexable Protocol methods

As a first step we are going to make our CustomStack conform to the Indexable Protocol.

extension CustomStack : Indexable
{
    //INDEXABLE PROTOCOLS
    typealias Index = Int

    var startIndex : Int
    {
        return 0
    }

    var endIndex: Int
    {
        return (data.count - 1)
    }

    subscript (position : Int) -> Element
    {
        return data[position]
    }
}

The above change makes the data structure conform to the Indexable protocol. This is a requirement for it to be of type CollectionType. In order to conform to the Indexable protocol we need to implement a few computed properties. Let us look at the changes

typealias Index = Int

This line informs the system that the Indexing type for my data structure is an Int.

var startIndex : Int
{
    return 0
}

var endIndex: Int
{
    return (data.count - 1)
}

The next 2 are computed properties. Each provides the implementation of the startIndex  and endIndex properties. Note that the type for both is Int as we have declared the Index type earlier as Int.

subscript (position : Int) -> Element
{
    return data[position]
}

The last implementation is of subscript. This provides the implementation to access an Element from the Stack using the Subscript operator.

Implementing the Sequence Type Protocol

Next we will implement the Sequence Type Protocol methods.

extension CustomStack : SequenceType
{
    typealias Generator = AnyGenerator<Element>
    
    func generate() -> Generator
    {
        var index = 0
        
        return AnyGenerator(body: {() -> Element? in
            if index < self.data.count
            {
                let res =  self.data[index]
                index += 1
                return res
            }
            return nil
        })
    }
}

Let us examine this code line by line.

typealias Generator = AnyGenerator<Element>

Objects of type Generator allow us to navigate through our collection. Quite like how iterators  work in C++. This line specifies the type to be AnyGenerator for Elements.

func generate() -> Generator

Next we start the implementation of the generate function. This is required as part of the SequenceType protocol.

var index = 0

This index variable is used to track the element that is currently being accessed.

return AnyGenerator(body: {() -> Element? in
            if index < self.data.count
            {
                let res =  self.data[index]
                index += 1
                return res
            }
            return nil
        })

The return statement is the main statement. Here we are creating an object of type AnyGenerator. As an argument to the constructor call we are passing in a closure that will be used to iterate through the sequence. Note that the closure captures the index variable and holds a reference to its value even though we have left the original function.

Implementing the Collection Type Protocol

Next we will implement the Collection Type Protocol methods. We don’t really need to implement a lot in order to conform to the CollectionType protocol. In fact, if we just conform to the CollectionType protocol and use the implementations of the previous 2 extensions we should be just fine. However, for the sake of demonstration we are implementing the subscript functionality within the CollectionType.

extension CustomStack : CollectionType
{
    typealias SubSequence = CustomStack<Element>
    
    subscript (bounds: Range<CustomStack.Index>) -> CustomStack.SubSequence
    {
        let newStack : CustomStack<Element> = CustomStack<Element>()
        
        for i in bounds.startIndex...bounds.endIndex
        {
            newStack.push(Element: data[i])
        }
        return newStack
    }
}

Let us look at the code line by line.

typealias SubSequence = CustomStack<Element>

Again, as before this line indicates that the SubSequence type is actually a CustomStack.

subscript (bounds: Range<CustomStack.Index>) -> CustomStack.SubSequence

Here we start the implementation of the subscript functionality.

let newStack : CustomStack<Element> = CustomStack<Element>()
        
for i in bounds.startIndex...bounds.endIndex
{
     newStack.push(Element: data[i])
}
return newStack

The rest of the code is the implementation of the subscript range behaviour. One can have different implementations to achieve the same result.

CollectionType Video

Conclusion

As we can see, by designing our data structure to conform to a particular set of protocols. We have made it possible for our data structure to take advantages of the different features, functionalities and even API’s available within the Swift Language and the Frameworks used as a part of iOS, macOS, watchOS & tvOS development.

Creating Frameworks for iOS/OS X App Development

Creating Swift Frameworks

Creating Swift Frameworks is easy. The steps below walk you through creating a Swift Framework. The steps below have been performed on Xcode 7.3

  1. Launch Xcode.
  2. Select Create New Project. Or from the menu bar select File > New > Project
  3. From the Template chooser select the Framework & Library  Option under iOS
  4. Select Cocoa Touch Framework1
  5. Give your project a name.
  6. Make sure the language selected is Swift.
  7. Feel free to enter values of your choice for organisation name and organisation identifier.
  8. Save your project. Optionally, if you have a version control repository like Git you may save it there.
  9. In left hand side bar make sure you have selected the Project Navigator.
  10. Within the Project Navigator make sure you have selected the folder named after your project.
  11. Click on File > New > File.
  12. Make sure iOS Source is selected on the left hand side.
  13. Select the file type as Swift.IMG_3525
  14. Write down the code that you want to make available through a framework.
  15. Now this is the key point. Place the keyword public before all the elements that you want to make publicly accessible.Why do we need to do this? To understand this we need to understand the scope of different elements within a typical Swift project. IMG_3521

    Different variables/classes/functions that are declared within a module are accessible freely within the module. Swift files contain code & are themselves found within Swift modules. So a module can mean project or a framework.So, to access the variables/functions/classes from module A in module B, we have to make those elements of module A public in order to access them in module B.

    For more information, do read Apple’s Swift Documentation.

  16. The next steps depend on what your ultimate objective is. If you wish to build a framework for distribution then you need to follow a process that is similar to distributing an app. You need to get the code signing done & prepare the project for distribution.
  17. If however, you plan to release it internally, or even just test it. Then you can follow the steps below.
  18. Firstly, our objective is to make this framework run on both OS X(macOS) as well as iOS.
  19. To do that we will be adding a new target. Click on File > New > Target.
  20. Select OS X & the Frameworks & Libraries from the sidebar.
  21. Select Cocoa Touch Framework
  22. Give your framework a unique name. Something that indicates this framework is for OS X(macOS).
  23. Now, we don’t need to rewrite the code for the Mac. We can simply make the file we have written a member for the OS X Framework Target.
  24. To do that make sure that the right hand side sidebar is visible.
  25. In the left hand side sidebar make sure that you have selected the new Swift file with the code you have written in there.
  26. In the right hand side sidebar select the Document Inspector.
  27. Under Target Membership make sure that both the Targets are checked. The target for iOS should already be checked.IMG_3520
  28. Thats it. If you do not wish to make your code available for both iOS & OS X then skip steps 19 – 27.
  29. The next part is building the framework. We will be building this framework for use internally. We will first build the iOS framework.
  30. From the tool bar, make sure the target selected is for iOS. For the device you can select any device that you wish.
  31. Then click on Product > Build to build the framework. If all goes well then you should get the message Build Succeeded on your screen.
    IMG_3519
  32. To get hold of the framework, expand the product folder from the left hand side sidebar.
  33. Select the Framework you have just built. Note that it should be black in colour. If you have opted to make a framework for OS X, then you should see that framework listed too, it should be in red colour. The red colour indicates that it has not yet been built.IMG_3524
  34. Control-click on the iOS version of the framework and select Show in Finder.
  35. This will take you directly to the folder containing the framework. Copy paste it to the desktop or to any other location to easily access it when required.
  36. Repeat steps 30 – 34 to build the OS X version of the Framework. Make sure that the target selected is OS X.
  37. Once we have done that, we need to test the framework we just created.
  38. Create a dummy iOS Project for testing.
  39. From the left hand side project navigator make sure that the blue project settings file is selected.
  40. Make sure that the Target is selected within the settings screen.
  41. Under the General tab scroll down to the Embedded Binaries section.
  42. Click on the ‘+’ sign to add a framework.IMG_3523
  43. Click on Add other
  44. Navigate to the folder where you saved the Framework and select it.
  45. Click Open
  46. Select Copy Items if needed
  47. The framework should be added to your project.
  48. In the ViewController.swift file import your Framework: import CustomStack
  49. Replace CustomStack with your frameworks name.
  50. Try to write the code which uses the elements you have packaged within the framework.

Creating Mixed Frameworks (Swift & Objective-C)

The process of creating a mixed library is straightforward. Its almost the same as above with some minor differences.

  1. Follow the steps mentioned above to add your Swift Code.
  2. Add your objective-C files to the project.
  3. While adding the files make sure that the checkbox for the targets is selected appropriately. Screen Shot 2016-08-05 at 1.20.37 PM
  4. Write the code that you wish to write in Objective-C. Of course, if you are including prewritten files then you do not need to do this.
  5. To make the Objective-C code accessible in Swift you need to make the following changes:
    1. In the umbrella header of your framework add the line to import the header
      #import "<FrameworkName>/<HeaderName>.h
    2. Modify the access property located within the target membership of the Objective-C header file. IMG_3527
  6. This should make your Objective-C code accessible to the Swift files.
  7. Test the changes by accessing your Objective-C code in your Swift files within the framework.
  8. Test the changes further by embedding your mixed language framework into a project & then try to access both the Swift as well as Objective-C versions of the code in your new project.
  9. To make your Swift code accessible to Objective-C File make the following changes:
    1. Make sure that your Swift code is compatible with Objective-C. There are 2 ways of doing this. One you can make your Swift class inherit from NSObject. The second way is to use the @objc keyword before your class declaration.
    2. In the Objective-C header file add the line to add the bridging header which is auto generated. You do not need to create your own bridging header.
      #import "<FrameworkName>/<FrameworkName>-Swift.h"

      Replace the word FrameworkName with the name of your Framework.

    3. This should allow you to access your Swift code in your Objective-C header file within the same Framework Project.
  10. This way you can make a single framework which contains code written in both Swift & Objective-C.

 

 

Writing Swift Programs on Linux

xc7-swiftlogo_2x1Swift Programming on Linux

The steps below walk you through the process of downloading, installing & using the Swift Programming language on Linux. For this I will be using Ubuntu Linux 14.04.3 LTS version.

Setup & Configuration

  1. Downloading Swift
    The first step is to download Swift from the link given below. Select the correct version of the OS.
    https://swift.org/download/#latest-development-snapshots
  2. Install clang
    The next step is to get hold of clang which is  a compiler front end for C,C++,Objective-C & Objective-C++. For more information on clang:
    https://en.wikipedia.org/wiki/Clang
    http://clang.llvm.org
    To install clang run the command:
    sudo apt-get install clang
  3. Extract the Swift files you downloaded and place them in a folder of your choice.
  4. Next we will add swift to our path. Do that by running the command:
    export PATH=/path to your swift folder/usr/bin/:”${PATH}”
  5. Verify whether it works by trying the following 2 commands
    which swift
    swift –version

Testing the REPL

  1. Next we try the REPL for swift. To invoke this just type the command:
    swift
  2. Write something like let myName : String = “Swift Code”
    and then hit enter.
  3. Follow it by print(myName) and then hit enter.
  4. You should see the output printed. This is a nice way to test individual Swift statements.

Creating a Single File Project

  1. To create a file based project:
    1. Create a folder of your choice, lets call it Hello World:mkdir HelloWorld
    2. Create a manifest file for the Package with the command: touch Package.swift
    3. The source files for all your projects must be in the Sources folder, let’s create that: mkdir Sources
    4. Create a file called main.swift using an editor of your choice. Place the statement print(“Hello, World”) in it.
    5. Step out of the sources folder and then run the command to build the project: swift build
    6. The executable will be inside a hidden folder called .build/debug
    7. The name of the executable will be the same as the name of the project folder.
    8. To run it simply type: .build/debug/HelloWorld

Creating a multi file project

  1. Create a folder of your choice, lets call it Hello World:mkdir CentigradeToFahrenheit
  2. Create a manifest file for the Package with the command: touch Package.swift
  3. The source files for all your projects must be in the Sources folder, let’s create that: mkdir Sources
  4. Create a file called converter.swift using an editor of your choice.
  5. Write the following code in it.:
    //note the code below is for demonstrating multi file projects & may not necessarily be accurate or correct
    func centigrade_to_fahrenheit(temperatureInCentigrade : Float) -> Float

    {
              return ((temperatureInCentigrade*9.0/5.0)+32.0)
    }
    func string_to_float(input : String) -> Float
    {
              var number : Float = 0.0;
              var result : Float = 0.0
              for charac in input.characters
              {
                        switch charac
                        {
                                  case “0”:
                                            number = 0.0;
                                  case “1”:
                                            number = 1.0;
                                  case “2”:
                                            number = 2.0;
                                  case “3”:
                                             number = 3.0;
                                  case “4”:
                                            number = 4.0;
                                  case “5”:
                                            number = 5.0;
                                  case “6”:
                                            number = 6.0;
                                  case “7”:
                                            number = 7.0;
                                  case “8”:
                                            number = 8.0;
                                  case “9”:
                                            number = 9.0;
                                  default:
                                            break
                        }
                        result = (result * 10.0) + number;
              }
              return result
    }
  6. Create a second file called main.swift
  7. Write the following code in it:
    if Process.arguments.count != 2
    {
              print(“USAGE: centigradeToFahrenheit 33.4”)
              print(“You are missing an argument”)
    }
    else
    {
              let temperatureInCentigrade = string_to_float(Process.arguments[1])
              print(“\(temperatureInCentigrade) is equal to \(centigrade_to_fahrenheit(temperatureInCentigrade))”)
    }
  8. Step out of the sources folder and then run the command to build the project: swift build
  9. The executable will be inside a hidden folder called .build/debug
  10. The name of the executable will be the same as the name of the project folder.
  11. To run it simply type: .build/debug/CentigradeToFahrenheit 100
    1. Try with different input values and no input value.
  12. That’s it. You are now ready to start typing code in Swift.

 

Clarifying Swift & Objective-C

swiftApple made a surprising announcement at the recent WWDC held between June 2-6 2014. While everyone was looking forward to the launch  of the new OS for both the Mobile as well as Mac platforms, Apple also announced a new programming language for developers. SWIFT.

It was described as Objective-C without C. Over the past few months I have gone over the features of the language & even converted many of the apps into Objective-C. While the initial learning curve is there, it is a fairly easy language to pick up. I will be going over a few interesting features of the language in this article. But what I will also be touching upon is the future & relevance of Objective-C.

Reduction in Lines of code: One of the first things that you will notice once you start programming in Swift is the reduction in the number of lines of code. Swift does away with the long syntax which you find in Objective-C. This makes the code look a lot more compact & less intimidating.

Lets take the example of creating an object in Swift & compare the syntax with that of Objective-C

Objective – C version: NSString *myString = [[NSString alloc] initWithString:@”This is a string”];

Swift version: var myString : String = String(“This is a string”)

As we can see, there is a great deal of simplicity that comes along. Reduction in the number of lines of code is of great importance to software developers. This means more compact code & improves readability. However, this comes at a price. While Objective-C code was more elaborate & did use many more lines of code it was far easier to understand by simply reading through it. In case of Swift the code will have to be complemented with details comments about what it does.

Single File: Another new feature there in Swift is the lack of header files. People coming from a Java background would find this familiar. The declaration of classes & implementation of its functions is done in .swift file now. This removes the necessity of having to import various header files. In case, certain frameworks need to be used all that needs to be done is to import that framework at the top of the file.

import framework

Replace the keyword framework with the name of the framework you wish to include.

Mixed Language Programming: Unlike Objective-C, Swift does not work well with other languages. In fact, Swift only works with Objective-C. So if you have any API’s or pieces of code that you have written in C or C++, you will have to create Objective-C wrapper classes for that C/C++ code so that you can incorporate it into your project.

This means that Objective-C is not going to be deprecated any time soon. Also, it will be equally important to learn Objective-C & can’t be ignored by people.

COMMON QUESTIONS:

– I already know Objective-C should I also learn Swift?
There is no pressing need to immediately learn Swift, though it would be a good idea to learn it anyways. None of the apps you have already made will stop working. However, you may come across code written in Swift or would need to incorporate programs written in Swift, so having knowledge of the same would be a good idea.

– I don’t know Swift or Objective-C which should I learn?
Again, as in the previous question knowing both is a really good idea. If you are confident that you will be making apps using Swift only, then you can learn just that for starters. If you feel that there is a good chance of having to read/modify or write Objective-C code then learning Objective-C before Swift makes more sense.

CONCLUSION: Swift is a very easy to use language & will sit nicely on familiar eyes. A lot of the code that will be written in the coming apps will definitely use Swift. However, this does not mean the end of the road for Objective-C. Nor does it mean that Objective-C will become redundant or obsolete. A developer can choose to use any of the languages to make the app. It will be nice for a new developer to first learn Objective-C & then move on to Swift as it will give the user a more all round understanding of the entire development framework .

 

C++ in iOS – 2

Continuing from the previous post. This one will look into a working example for the same. The code used here is pretty trivial. It is organized in the following sets of files. A c++ header file which contains the declaration of a class. A c++ implementation file (.cpp) where the member functions are implemented. An Objective-C++ header file which holds the C wrapper struct & an Objective-C wrapper object. An Objective-C++ implementation file (.mm) which implements the C struct & all the member functions for both the C struct as well as the Objective-C class.

CppStruct.h


#include <string>
class MyObject
{
     public:
          std::string getName();
          void setName(std::string tempName);
          MyObject();
      private:
           std::string name;
 };

CppStruct.cpp

#include "CppStruct.h"
#include <string>
#include <iostream>

std::string MyObject::getName()
{
     return name;
}

void MyObject::setName(std::string tempName)
{
     name = tempName;
}

MyObject::MyObject()
{
     name = "NIL";
}

CppStructWrapper.h

struct PersonWrapper;

@interface CppStructWrapper : NSObject
{
     struct PersonWrapper *obj;
}

-(void) makeAPerson;
-(void) setName:(NSString *) personName;
-(NSString *) getName;
@end

CppStructWrapper.mm

#import "CppStructWrapper.h"
#include "CppStruct.h"
#include <string>

@implementation CppStructWrapper

struct PersonWrapper
{
     MyObject *personObj;
     std::string getName();

     void setName(std::string tempName);
     PersonWrapper();
};

std::string PersonWrapper::getName()
{
     return personObj->getName();
}

void PersonWrapper::setName(std::string tempName)
{
     personObj->setName(tempName);
}

PersonWrapper::PersonWrapper()
{
     personObj = new MyObject;
}

-(id) init
{
     self = [super init];
     if(self)
     {
          obj = nil;
     }
     return self;
}

-(void) makeAPerson
{
     obj = new PersonWrapper;
}

-(void) setName:(NSString *) personName
{
     std::string temp([personName UTF8String]);
     obj->setName(temp);
}

-(NSString *) getName
{
     NSString *str = [[NSString alloc] initWithUTF8String:obj->getName().c_str()];
     return str;
}

@end

ViewController.m

//this code could be anywhere I have put it inside the ViewController.m within the init message.
#import "ViewController.h"
#import "CppStructWrapper.h"

-(id) init
{
     self = [super init];
     if(self)
     {
          CppStructWrapper *obj = [[CppStructWrapper alloc] init];
          [obj makeAPerson];
          [obj setName:@"ABC"];
          NSString *str = [obj getName];
          NSLog(@"%@",str);
     }
     return self;
}

 

C++ in iOS

As we know there are many libraries out there that are written in C++. Now it would be really nice & handy if we could integrate them into our iOS App. There are many blogs out there that discuss the same.

I found this particular article quite good: http://philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects

The article talks about the PIMPL approach. There is another link to the same related article by the same author:http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++

The following is one approach towards integrating C++ API into your App. To make things simpler there is a lot of wrapping to do.

Declare your class in the C++ header file as is.

Now to use this class in Objective-C we need a wrapper. To create this wrapper go ahead & make a simple Objective-C class which inherits from NSObject. But once you are done making, rename the extension of the implementation file from .m to .mm, this makes it an Objective-C++ file.

In this file we will be forward declaring a struct which will eventually wrap our C++ class. Create a pointer variable of the struct to be a member of the Objective-C++ class.

Implement the struct in the .mm file to have an object to our c++ class & some member functions to access it. Remember a struct is a valid “C” type. We have created an Opaque struct, but in the implementation it is ok to have member functions (which is allowed in C++). So this struct acts as a bridge between C & C++.

Once you have done this you are set.

The flow for the entire code will be in the form

IOS App Code -> Your Objective-C++ Wrapper -> Your struct wrapper -> Your C++ class in the header file

Continue reading