Is It Possible to Combine Objective C with C++

Discussion in 'iOS Programming' started by teguh123, Mar 28, 2011.

  1. teguh123 macrumors member

    Joined:
    Mar 22, 2011
    #1
    Also Objective C seems to be an over kill I think.

    For example

    Say I declare class

    And say I have NSstring * Name as a member of that class.

    What if I want NSstring member rather than NSstring * member so I do not have to worry about memory when dealing with this class. All the class member names will be destroyed when the class is destroyed. After all if the class is in a heap, so will all it's members too?

    Can objectivec class have a C++ member variable?

    How do I convert STL string object into NSString, for example.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Stop thinking in C++ when writing in Objective-C! For, what are or were good reasons, all Objective-C objects are allocated on the heap so accessed via pointers. There is no way to get NSString rather than NSString *.

    Edit to add: for good reasons why read this.
     
  3. teguh123 thread starter macrumors member

    Joined:
    Mar 22, 2011
    #3
    So screw C++

    Objective C all the way.

    How do I sort vector? Does objective C has standard library.

     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes. But not the C++ one. The "standard library" is a large number of frameworks. Some are available on pretty much all (Apple) platforms. Some are platform specific (UIKit is iOS specific for example). It seems you might well be looking for the sort of functionality provided by the Accelerate framework which is, in fact, a low-level C framework rather than Objective-C. Unless you really mean and array, rather than a vector.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    See this reply on your other thread:
    http://forums.macrumors.com/showthread.php?p=12261166&highlight=#post12261166

    The Collections Programming Topics link has as its first section "Arrays: Ordered Collections". That has a sub-section "Sorting Arrays".
    http://developer.apple.com/library/...ys.html#//apple_ref/doc/uid/20000132-BBCCJBIF

    I suggest you read the entire Collections Programmings Topics document, and then look at the second link, which lists all the classes in the Foundation framework.
     
  6. teguh123, Mar 31, 2011
    Last edited: Apr 1, 2011

    teguh123 thread starter macrumors member

    Joined:
    Mar 22, 2011
    #6
    Is It Possible to Combine Objective C with C++

    For example,

    Can I write a class like this:

    Code:
    @interface Badger  : NSObject {
    
    } 
    @property (retain) NSString *  Title;
    @property (retain) NSString *  BuildingAddress;
    @property (retain) NSString *  Street;
    @property (retain) NSString *  District;
    @property (retain) NSString *  Country;
    @end
    
    I want to write

    Code:
    @interface Badger  : NSObject {
    <string> Title;
    <string> BuildingAddress;
    <string> Street;
    
    ...
    } 
    
    @end
    
    Will there be a bridge back and forth to convert <string> to NSString* and via versa?

    So some internal hard work can be done with C++ and the UI can be in objective C?

    No body write Mac program using C++ isn't it?
     
  7. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #7
    There is no desperate need to make the property's, if you are gonna use them just in class 1.
    And in Xcode 4 u can combine the 2, but like anyone would say. u should read through the documentation or notes..
     
  8. teguh123 thread starter macrumors member

    Joined:
    Mar 22, 2011
    #8
    I updated the code.

    The thing with using NSString* is you need to at least jot down the property name 3 times.

    Once in property declaration.
    Once when you synthezie it
    Once when you deallocate it

    It seems to make sense to declare it once.
     
  9. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #9
    Yes but it's only useful for cases where you already have internals in C++ that need to be reused or need to maintain portability. If this is an iPhone/iPad exclusive app then don't do it.

    I'm interpreting this as "Nobody writes Mac programs using C++ do they?"
    where I'd answer: Yes a heap of us do.
     
  10. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #10
    The other way around is what I meant
    If lets say you are working with just 1 class.

    @interface Badger : UIViewController {
    NSString *string1;
    }

    @end

    And tada! u can acces ur string1.

    But giving it the property and synthesizing, creates the getters/setters. better then java --> Typing 2 whole new blocks of code just for the getters/setters of each ****** thing, no? :)
    Just saying x)
     
  11. teguh123, Apr 1, 2011
    Last edited: Apr 1, 2011

    teguh123 thread starter macrumors member

    Joined:
    Mar 22, 2011
    #11
    It has to be more than that. First when I set String 1, I would need to retain it. Then I will have to deallocate it. Of course, using @synthesize is already an easy way to do so.

    With C++ I can just write

    Class Badger
    {
    string Title;
    }

    No need to deallocate it. string::~string will automatically be called when Badger Class is deallocated. I do not even need to write destructor for Badger class.

    By the way, should I write this Badger Classes with lots of properties or should I use some form of database like Core? This is the first time I am trying to make a world class software and I don't even know what databases is.
     
  12. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #12
    Conventions...

    You really should try to follow the conventions of the community, including members (messages or values) starting with lower case.

    I don't think that's entirely fair - here, the focus tends to be around the iOS APIs, which are predominantly Obj-C, therefore you won't see a lot of C++ discussed. I suspect the chatty people are fully capable of discussing C++ topics.

    Solve problems with appropriate tools. Programming languages are tools. If you already have C++ code that implements non-UI capabilities, you should use it.

    The Xcode gcc compiler handles pretty much all of these languages universally and interchangeably - heck, you could probably throw some FORTRAN code in there and that would work as well (oh, you know, for linear algebra types of things).

    Now that I've had my posturing, it's time to ask you a question: exactly why do you want to bridge between these types? What is it that you anticipate as a benefit?
     
  13. teguh123 thread starter macrumors member

    Joined:
    Mar 22, 2011
    #13
    I am making a software that display data about all nearby restaurants.

    I want to then display the data on some table on IOS.

    I see that using C++ std::string class will require less code. But then I have to transfer that code IOS objective c interface somehow.

    As for convension, in Visual basic, public member variables are usually written in capital letter.
     
  14. Comrade Yeti macrumors newbie

    Joined:
    Nov 3, 2010
    #14
    By the time you bridge all your C++ code to ObjC I think you will find that you need MORE code than if you just wrote it in ObjC in the first place.

    Sounds like you are suffering from premature optimisation. C++ objects are not necessarily faster because they are allocated on the stack. Have you timed it? Compared against ObjC? Perhaps in the future you'll want to try multi-threading? NSString is thread safe... std::string isn't.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    By which you mean Core Data, correct? Accuracy is important in programming.
     

Share This Page