Is Objective C Strongly Typed

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

  1. teguh123 macrumors member

    Mar 22, 2011
    I am making an application that'll take stuff from database and then display the data.

    1. Should I do the backoffice in C++ instead of Objective C?
    2. Is Objective C Strongly Typed? Does it support templates like C# or
  2. Hansr macrumors 6502a

    Apr 1, 2007
    1) Depends on which database and requirement for speed. Generally the speed increase with large data sets is actually negligible between the two with explicit memory management.
    2) In the general sense no. C# and don't have templates they have generics. Pretty much the same thing but there are subtle differences. In Cocoa it's the same everything is derived from NSObject. If you want to spoof templating for methods you can achieve it by creating a new subclass from NSObject and declaring methods and then go and make subclasses of that. It's not needed and rather cumbersome but possible.

    I'd recommend a good book on the topic to get started.
  3. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    Nope and nope. Like C, Objective-C is typically considered a static, weakly typed language but it does support aspects of dynamic typing. For example, consider the below method:

    - (void)printObject:(id)obj {
        NSLog(@"%@",[obj stringValue]);

    In this case, id[/id] can be any Objective-C object and, as long as the stringValue method returns an value of type id (an ObjC object)*, things will work. Unfortunately this also means that non-ObjC objects, such as the primitive data types, must be wrapped in an ObjC object.

    * In this case, the returned object must also response to the - (NSString*)description selector, which all NSObject subclasses do.
  4. teguh123 thread starter macrumors member

    Mar 22, 2011
    I am making a program that allows people to see rating and locations of all restaurants around his location.

    So there would be a restaurant objects that I have to sort, based on locations, ratings, etc.

    Should I program that in C++ or objective C?

    Does objective C has generic array of some object, for example.

    Can I, having an objective C object of restaurant Object.


    restaurant : NSobject

    Say I have an array of that. Say I want to sort that based on a function. Can I do that using objective c built in sorting algorithm or should I make my own sorting function.
  5. PhoneyDeveloper, Mar 26, 2011
    Last edited: Mar 26, 2011

    PhoneyDeveloper macrumors 68030


    Sep 2, 2008
    Yes, of course you can write your own classes that are subclasses of NSObject or of other classes in the frameworks.

    Yes of course you can sort an array of objects, usually in one or two lines of code.

    For most developers the only reason to develop in C++ or in other languages than Objective-C is portability to other OSes. If you're doing OpenGL then it's a C api (mostly) so you might want to use C for that.

    Think of Objective-C as weakly typed. The compiler will warn you of mismatched types in many cases but will compile and run the code anyway. The language does have a philosophy that is different from C++. In C++ efficiency is paramount. Also strong typing is extremely important. As a result you have a language that is very complicated. Objective-C is less efficient, in some ways, and more flexible. C++ developers will get the willies when first working with Objective-C. After a while they either accept it for what it is or go away.

    Objective C has more introspection than C++ and has functionality that allows executing methods based on their names as strings. As an example you can sort an array of your Restaurant objects simply like this

    @interface Restaurant : NSObject
    @property (nonatomic, copy) NSString* name;
    // Some other code where restaurantList is a mutable array of Restaurant objects
    NSSortDescriptor* d = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    [restaurantList sortUsingDescriptors:[NSArray arrayWithObject:d]];
  6. chown33 macrumors 604

    Aug 9, 2009
    You should look at the Foundation classes. Specifically, look at NSArray.

Share This Page