Asked  6 Months ago    Answers:  5   Viewed   22 times

I've been learning Objective-C and Cocoa by working my way through the Hillegass book and it occurs to me that I might be better off using Objective-C++. Objective-C seems like the clear choice for developing UIs but I have a very strong C++ background and would love to develop application back-ends in C++ and use Objective-C++ to do the UI integration. But I wonder if Apple will keep developing Objective-C++ or will it become a dead end.

Is anyone out there using Objective-C++?

 Answers

68

Disclaimer: I don't work or speak for Apple, so this is my opinion:

I can't speak for the major dev shops, but in my small group, we've used Objective-C++ both for integrating C++ libraries, and as you propose for writing backends in C++. As @alxp mentions, things like exception handling across the language boundary are painful, but with a little planning, most of these pains can be avoided. For experienced C++ devs, the gains can be well worth the pain.

In terms of support, I think you can assume that support in its current state won't go away any time soon. It's part of the GCC code base and the Clang toolchain (Apple's next compiler toolchain) fully supports Objective-C++. On the other hand, there isn't any official guarantee that Apple will continue to develop the integration—fixing some of the warts, for example.

For current projects, I would say that if using Objective-C++ provides benefit, it is safe to rely on the existing support and you should use it.

Thursday, June 17, 2021
 
iceduck
answered 6 Months ago
71

NSTask is pretty easy to do this with. For a synchronous call, you can use something like this fragment:

NSString *path = @"/path/to/executable";
NSArray *args = [NSArray arrayWithObjects:..., nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];

The -waitUntilExit call makes sure it finishes before proceeding. If the task can be asynchronous, you can remove that call and just let the NSTask do it's thing.

Wednesday, June 30, 2021
 
iceduck
answered 6 Months ago
99

You can define a block as a global variable to get an effect similar to functions.

NSComparisonResult (^globalBlock)(id,id) = ^(id lhs, id rhs) {
    if([lhs intValue] < [rhs intValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    } else if([lhs intValue] > [rhs intValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    return (NSComparisonResult)NSOrderedSame;
};

Then, in the method doing the comparison:

int index1 = [array indexOfObject:number 
                    inSortedRange:NSMakeRange(0, [array count]) 
                          options:NSBinarySearchingFirstEqual 
                  usingComparator:globalBlock]; 

To put the block in a header, for external use:

NSComparisonResult (^globalBlock)(id,id);
Sunday, August 8, 2021
 
okapi
answered 4 Months ago
84

I had the same problem with Xcode 5, Cocoapods and AFNetworking 1.3.3.

I found my answer in this Google Groups discussion: AFNetworking unrecognized selector

Make sure your project and target settings aren't overwriting the values in the xcconfig file.

Each level should have (at least) $(inherited) to inherit the settings of the parent.

In Build Settings, make sure you have $(inherited) in Other Linker Flags.

Build Settings before I added $(inherited) (choose Levels, not Combined):

Other Linker Flags without $(inherited)

Build Settings after I added $(inherited):

Other Linker Flags with $(inherited)

That fixed it for me.

Wednesday, September 22, 2021
 
Jamie Taylor
answered 3 Months ago
79

You misunderstand the point of categories. Categories add methods to an existing class. They must never be used to override existing methods. Doing so is undefined behavior (technically only undefined in one case, but you can't predict that case, so you must assume it applies).

If you need to override methods, you must subclass, not use categories. See the top answer to the question you linked.

Sunday, October 10, 2021
 
tsmbl
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share