Language Mixing: Model and View - c++

Consider developing an application where the model will be written in C++ (with Boost), and the view will be written in Objective-C++ (with Cocoa Touch).
Where are some examples showing how to integrate C++ and Objective-C++ for developing iPhone applications?

Take it straight from the source: Apple has documentation on using C++ With Objective-C.
There really isn't much more to it besides, in my opinion, trying to keep the C++ and Objective-C parts as cleanly seperated as possible.
In your case it comes natural:
limit definition of C++ classes et al to the C++ model
restrict the Objective-C part to the view related code and using the C++ model
I don't know of any actual simple examples, but any cross-platform project that has a native GUI on the mac uses the same approach. One mostly clean example would be the Chromium source.

Related

Objective-C++ Absolute Confusion Over UI; Best Practices

I've been working at iOS and some Mac programming for years now, and have started reading the documentation for c++. It interests me to learn how to use c++ in iPhone apps, but all that I've read assumes that you've jumped the precipice of a learning curve that comes with c++ AND ObjC. So, ill start with the basics.
Let's say I have class A, and class A wants to implement a simple function from .cpp file B. I would then Create a .mm file, so class A could mix objC and c++, but my problem is that I have no idea WHERE the c++ code goes...
In a regular objC method, I would declare methods inside the .h after the #interface line, but with c++, it's just a mess to me...
But then a harder example. What if I want to display UI elements from a C++ class on the iPhone? I have found little to no blog posts or discussions regarding c++ UI (save those lauding the QT library), and nothing related to iOS UI in c++.
Does anybody have code, or best practices for tightly integrating c++ code into an iOS app?
I think you have the wrong impression of what objective-C++ is. Objective-C++ just lets your Objective-C code work with C++ and vice versa. It doesn't bridge the two languages or object models, it only supports integration within the same source file.
For example, when using UI elements you would still need to use objective-C classes. However, those classes could then interact with C++ classes in the same implementation file.
C++ classes are usually defined in headers as well, just like Objective-C classes. Then in the source file you'd implement their methods etc.
I recommend a good C++ introduction to get familiar with C++.
What if I want to display UI elements from a C++ class on the iPhone? I have found little to no blog posts or discussions regarding c++ UI
That is because that, while possible, is simply not necessary. As you can mix ObjC and C++ together in ObjC++ source files, you can just use the usual libraries.

Utilizing C++ in iOS and Mac OS X applications

I'm somewhat moderate in my C++ knowledge but know next to nothing regarding Objective-C. I am planning (and even starting to) learn Objective-C so I can attempt to write a few iOS and even Mac OS X applications but I'm very curious about something I haven't been able to find yet.
Is it possible to write an iOS and / or a Mac OS X application entirely with C++ including the UI components? If it isn't possible to do it entirely in C++ then to what degree is it possible to use mostly C++?
I haven't seen any examples that demonstrate either of these items. I am planning on writing a few mobile applications for iOS and Android and, where possible, I'd like to contain most of my logic inside of C++ code for maximum portability. I already know I can do this but I am unsure of the degree.
Short answer, yes.
For pure C++, you should take a look at the QT framework.
Otherwise, you'll have hard time for the UI part.
But also remember that Objective-C can be mixed with C++.
That's called Objective-C++ (.mm files).
You can then write code that mix C++ and Objective-C code.
With this, you can have the UI parts in Objective-C (may be better, as it will use the native frameworks for the UI components), and the other things in C++.
If you've decided to learn Objective-C, but still want to code in C++ for some parts, I would recommend this approach, instead of pure C++.
On iOS, this is also the only way. While you can code in C++, you have to use Objective-C for the UI part.
EDIT
Here are a few links to get started with Objective-C++:
Strategies for Using C++ in Objective-C Projects
From C++ to Objective-C
CocoaDev
Look this question
I don't know about Mac OS, but in IOS applications you can use C++ in logic but you have to write user interface on Objective-C.
Is it possible to write an iOS and / or a Mac OS X application
entirely with C++ including the UI components? If it isn't possible to
do it entirely in C++ then to what degree is it possible to use mostly
C++?
I think it is possible to use pure c++ if you want to depend on some 3rd part lib, then yes.
Without that 3rd part lib you have to write all the UI using objective-c the rest you can use c++ as much as you like.
Changes required in build settings to use C++ files in iOS project
under "Apple LLVM compiler 4.2 - Language" Option
C++ Language Dialect: Compiler Default
C++ Standard Library: Compiler Default
You could use Cocos2d-x engine and write pure C++ for your iOS app, including UI (cocos has basic ui classes like buttons, scroll views, table views, etc - but you need to develop your own Controllers, and Models using various C++ libraries like Hiberlite, or raw SQLite etc)
Cocos has its own interface with iOS/MacOS (a few basic .mm objective-c classes to setup Application run cycle - like app delegate, or runloop and drawing cycle)
So you just write your user code with C++

Objective C to C++ conversion [duplicate]

I have no Objective-C experience whatsoever but have a strong C++ background. Is there an automated tool/script or, worst case, some manual method using some excellent reference to port code written in Objective-C to C++? What are the difficulties involved?
Edit:
I'm told the code uses Objective-C fairly trivially. It's an iPhone app that probably doesn't use much in the way of OS-level UI. The C++ version is meant for a non-Apple platform where GNUStep is not an option, so Objective-C++ is not an option.
I worked on same problem. And have some solutions:
Microsoft now offers its own Objective C "bridge", albeit only for UWP apps.
https://github.com/Microsoft/WinObjC
In 2007 I tried to support Objective-C in Visual Studio and wrote my own ObjC runtime, but I do not have more time for writing parser for it. If you have, you can :)
http://code.google.com/p/qobjc/
I wrote the basic functionality of Foundation Framework in C++. In this case, you need to port manually; your code will be on C++ but it will be like on Objective-C. This library worked on iPhone.
http://code.google.com/p/dcocoa/
There are no automated tools that I'm aware of. The dynamic nature of Objective-C is very hard to translate to C++, so quite a bit of brain effort is going to be required for all but trivial Objective-C code. Are you willing to stay on OS X (or keep the GNUStep dependency if you're one of the few people using Objective-C on an OS besides OS X)? If so, the easiest approach is to use Objective-C++ to build a bridge beteen the Objective-C and C++ code. Objective-C++ is an Apple extension to Objective-C and the GCC compiler that allows you to mix Objective-C and C++ code. You can thus create Objective-C objects that call or reference (but not inherit from) C++ objects and you can send Objective-C messages to Objective-C instances from within C++ code.
Objective-C was just a pre-processor (and a runtime library). You'll find more problems in porting all the libraries than the end-user code.
Nil-eating behavior is going to be fun when translating.
You are likely to start seeing bad design in all c++ code after using Objective-C frameworks for a while.

What is the best way to port from Objective-C to C++?

I have no Objective-C experience whatsoever but have a strong C++ background. Is there an automated tool/script or, worst case, some manual method using some excellent reference to port code written in Objective-C to C++? What are the difficulties involved?
Edit:
I'm told the code uses Objective-C fairly trivially. It's an iPhone app that probably doesn't use much in the way of OS-level UI. The C++ version is meant for a non-Apple platform where GNUStep is not an option, so Objective-C++ is not an option.
I worked on same problem. And have some solutions:
Microsoft now offers its own Objective C "bridge", albeit only for UWP apps.
https://github.com/Microsoft/WinObjC
In 2007 I tried to support Objective-C in Visual Studio and wrote my own ObjC runtime, but I do not have more time for writing parser for it. If you have, you can :)
http://code.google.com/p/qobjc/
I wrote the basic functionality of Foundation Framework in C++. In this case, you need to port manually; your code will be on C++ but it will be like on Objective-C. This library worked on iPhone.
http://code.google.com/p/dcocoa/
There are no automated tools that I'm aware of. The dynamic nature of Objective-C is very hard to translate to C++, so quite a bit of brain effort is going to be required for all but trivial Objective-C code. Are you willing to stay on OS X (or keep the GNUStep dependency if you're one of the few people using Objective-C on an OS besides OS X)? If so, the easiest approach is to use Objective-C++ to build a bridge beteen the Objective-C and C++ code. Objective-C++ is an Apple extension to Objective-C and the GCC compiler that allows you to mix Objective-C and C++ code. You can thus create Objective-C objects that call or reference (but not inherit from) C++ objects and you can send Objective-C messages to Objective-C instances from within C++ code.
Objective-C was just a pre-processor (and a runtime library). You'll find more problems in porting all the libraries than the end-user code.
Nil-eating behavior is going to be fun when translating.
You are likely to start seeing bad design in all c++ code after using Objective-C frameworks for a while.

Is it possible to program iPhone in C++

I'm all for language diversity, but Objective C is insane. So I'm curious: is it possible to code iPhone apps with C++ while using the Cocoa API, etc?
Although Objective-C does indeed appear to be "insane" initially, I encourage you to stick with it. Once you have an "a-ha" moment, suddenly it all starts to make sense. For me it took about 2 weeks of focused Objective-C concentration to really understand the Cocoa frameworks, the language, and how it all fits together. But once I really "got" it, it was very very exciting.
It sounds cliché, but it's true. Stick it out.
Of course, if you're bringing in C++ libraries or existing C++ code, you can use those modules with Objective-C/Objective-C++.
Short answer, yes, sort of. You can use Objective-C++, which you can read about at Apple Developer Connection.
If you know C++ already, learning Objective-C would be pretty simple, if you decided to give that a try. More info on that topic is at the ADC as well.
I use Objective-C to slap the UI together.
But the hard guts of the code is still written in C++.
That is the main purpose of Objective-C the UI interface and handling the events.
And it works great for that purpose.
I still like C++ as the backend for the code though (but that's mainly becuase I like C++) you could quite easily use Objective-C for the backend of the application as well.
First off, saying Objective-C is "insane" is humorous- I have the Bjarne Stroustrup C++ book sitting by my side which clocks in at 1020 pages. Apple's PDF on Objective-C is 141.
If you want to use UIKit it will be very, very difficult for you to do anything in C++. Any serious iPhone app that conforms to Apple's UI will need it's UI portions to be written in Objective-C. Only if you're writing an OpenGL game can you stick almost entirely to C/C++.
Having some experience of this, you can indeed use C++ code for your "core" code, but you have to use objective-C for anything iPhone specific.
Don't try to force Objective-C to act like C++. At first it will seem to you this is possible, but the resulting code really won't work well with Cocoa, and you will get very confused as to what is going on. Take the time to learn properly, without any C++ around, how to build GUIs and iPhone applications, then link in your C++ base.
You have to use Objective C to interface with the Cocoa API, so there is no choice. Of course, you can use as much C++ as you like behind the scenes (Objective C++ makes this easy).
It is an insane language indeed, but it's also... kind of fun to use once you're a bit used to it. :-)
I'm not sure about C++, but you can definitely code iPhone applications in C#, using a product called MonoTouch.
You can see this post for detailed discussion on MonoTouch Vs Obj-C: How to decide between MonoTouch and Objective-C?
I'm in the process of porting a computation-intensive Android app written in Java to iOS6. I'm doing this by porting the non-UI parts from Java to C++, writing the (minimal) UI parts in Obj-C, and wrapping the former in a (small) C interface using the standard C/C++ technique, so that it can be accessed from Obj-C, which is after all a superset of C.
This has been effective so far, and I haven't encountered any gotchas. It seems to be a legitimate approach, since Xcode lets you create C++ classes as well as Obj-C classes, and some of the official sample code does things this way. I haven't had to go outside any officially supported interfaces to do this.
There wouldn't seem to be much to gain from writing my remaining UI code in C++ even if it were possible, in view of the help given to you by the interface builder in Xcode, so my answer would be that you can use C++ for almost all your app, or as much of it as you find appropriate/convenient.
Yes but Thinking that you can program every kind of program in a single language is a flawed idea unless you are writing very simple programs. Objective C is for Cocoa as C# is for .NET, Use the right tool for right job, Trying to make C++ interact to Cocoa via writing bridging code and trying to make C++ code behave according to Cocoa requirements is not a good idea neither expecting C++ performance from Objective C is. You should try to layout design and architecture of app keeping in view existing skills and determine which part should be written in which language then build accordingly.
I'm currently writing an Objective-C++ framework called Objective-X, wich makes PURE C++ iPHONE PROGRAMMING possible. You can do like this:
#import "ObjectiveX.h"
void GUIApplicationMain() {
    GUIAlert Alert;
    GUILabel Label;
    GUIScreen MainScreen;
    Alert.set_text(#"Just a lovely alert box!");
    Alert.set_title(#"Hello!");
    Alert.set_button(#"Okay");
    Alert.show();
    Label.set_text(#"Ciao!");
    Label.set_position(100, 200, 120, 40);
    MainScreen.init();
    MainScreen.addGUIControl(Label.init());
}
and compile it using GCC's appropriate commandline options. I've already compiled this helloworld app&it w0rkX0rz like a charm. ;-) It'll available soon on GoogleCode. Search for Objective-X or visit http://infotronix.orgfree.com/objectivex approx. a week later!
Updated (but apparently inactive) URL: http://code.google.com/p/objectivex/
It may be a bit offtopic, but anyway. You can program c++ right on iOS devices. Check out CppCode ios app - http://cppcode.info. I believe it helps to learn c and c++ and objective-c later.