Is Objective C Strongly Typed

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

  1. teguh123 macrumors member

    Joined:
    Mar 22, 2011
    #1
    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 VB.net
     
  2. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #2
    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 vb.net 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

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #3
    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:


    Code:
    - (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

    Joined:
    Mar 22, 2011
    #4
    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.

    say

    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

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    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


    Code:
    @interface Restaurant : NSObject
    {
    }
    
    @property (nonatomic, copy) NSString* name;
    @end
    
    // 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

    Joined:
    Aug 9, 2009
    #6
    You should look at the Foundation classes. Specifically, look at NSArray.

    http://developer.apple.com/library/...ssic/_index.html#//apple_ref/doc/uid/20001091

    http://developer.apple.com/library/...ollections.html#//apple_ref/doc/uid/10000034i
     

Share This Page