Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

teguh123

macrumors member
Original poster
Mar 22, 2011
62
0
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.
 
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?

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.
 
So screw C++

Objective C all the way.

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

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.
 
How do I sort vector? Does objective C has standard library.

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.
 
How do I sort vector? Does objective C has standard library.
See this reply on your other thread:
https://forums.macrumors.com/showthread.php?p=12261166&#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.
 
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?
 
Last edited:
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..
 
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..

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.
 
So some internal hard work can be done with C++ and the UI can be in objective C?

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.

No body write Mac program using C++ isn't it?

I'm interpreting this as "Nobody writes Mac programs using C++ do they?"
where I'd answer: Yes a heap of us do.
 
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)
 
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)

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.
 
Last edited:
Conventions...

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

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

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?

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?
 
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?

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.
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.