couple of background questions

Discussion in 'iOS Programming' started by zedzeek, Nov 11, 2011.

  1. zedzeek, Nov 11, 2011
    Last edited by a moderator: Nov 13, 2011

    zedzeek macrumors newbie

    Joined:
    Nov 11, 2011
    #1
    Im thinking of trying my hand at iphone/pad game development esp opengl (which Ive used for over 10 years)

    My Background -
    heres some of the games Ive written (excluding commercial games)
    http://anarchistsunited.com/games/propaganda/
    so Im pretty experienced in a variety of techniques/languages.

    A/
    One thing I dont know is objective C, now Ive looked at it a little bit and it doesnt seem to click for some reason, this isnt my code, but surely you can write the obj-c function much cleaner (something like the c way),
    if so how?

    c++ ///////////////

    Code:
    a = new A[10];
    for(int i=0; i<10; i++)
           a[i] = whatever;
    objective c ///////////

    Code:
    a = [[NSMutableArray alloc] initWithCapacity:10];
    for(int i=0; i<10; i++) {
           A *myObj = [[A alloc] init];
           myObj = whatever;
           [a addObject:myObj];
           [myObj release];
    }
    B/
    I see some ppl have programmed on the iphone with 99% c++ (and just using obj-c to setup the basis) are there any issues with doing it this way?

    cheers zed
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    It take a good programmer about 2 weeks of using Objective C before its form of punctuation abuse seems as normal as any other programming language's form of punctation abuse and syntax.

    Many of my programs are over 90% C, but Obj C makes coding the UI stuff a ton easier.

    The latest Xcode compilers with ARC no longer requires as much manual memory management.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Hmm, I'm pretty sure this is a memory leak. Why alloc myObj and then just reassign it?
     
  4. zedzeek thread starter macrumors newbie

    Joined:
    Nov 11, 2011
    #4
    cheers guys for the fast responses

    @firewood - Im not planning to use UI stuff much (if at all)
    Ill be doing stuff like this (needs a chrome browser & gmail account) https://chrome.google.com/webstore/detail/pdcjmcoakgklifndgbjfcidadbadlfaa here 99% of the code is nothing to do with html5 or widgets etc.
    so by 90% C you mean you use malloc or new to allocate memory for objects and not the obj-c way eg A *myObj = [[A alloc] init];

    @dejo yes youre prolly right, the myObj = whatever; was just a nonsense line standing for do something with the newly created object

    perhaps this is clearer (or would this also leak memory)
    myObj.id = i;

    From my layman's look it seems like creating an array of objects in obj-c is far more obfusticated than most other languages, is is this assumption wrong. How would you write similar code, simpler?

    cheers zed
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    I'm not overly familiar with C++ anymore (it's been many, many years since I've done anything with it), but does:
    Code:
    a[i] = whatever;
    create a copy of whatever or is it just a simple pointer assignment? If the latter, here's a very simple Objective-C version I would code:
    Code:
    a = [NSMutableArray array];
    for (int i=0; i<10; i++)
        [a addObject:whatever];
     
  6. jiminaus, Nov 11, 2011
    Last edited: Nov 11, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    The default interpretation would be sort of akin to an Objective-C copy. But the actually behaviour is up to A's assignment operator. It could actually start deleting all your files. :D I love C++ (somewhat sarcastically).

    But I think dejo's sample is closest to the sprit of Objective-C. The exact semantics of the C++ code wouldn't be done in Objective-C, because objects are never treated as values in Objective-C, but they often are in C++.

    For the OP, in Objective-C you would so what would be this in C++.
    Code:
    a = new A[COLOR=red]*[/COLOR][10];
    for(int i=0; i<10; i++)
           a[i] = whatever;
    
    where whatever evaluates to a pointer to an A object allocated on the heap.
     
  7. Vizin, Nov 12, 2011
    Last edited: Feb 28, 2013
  8. firewood, Nov 12, 2011
    Last edited: Nov 12, 2011

    firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    Objective C is a strict superset of ANSI C. Any line of code that looks like C is C and acts like C. The base OS under iOS in BSD Unix with the usual posix interface.

    But note that if you don't use UIKIt, your app might look or act strange to potential customers, which could cost your app sales/popularity/good reviews.

    It's also possible, but a ton more code, to make your app as power efficient, rotation, multi-touch input, and multi-tasking aware without Objective C, as that's the interface to many iOS APIs.

    So even some 3D games that are 99% C and Open GL ES use Obj C for the UI front layer.
     
  9. zedzeek thread starter macrumors newbie

    Joined:
    Nov 11, 2011
    #9
    OK thanks guys, I'll keep at it.
    I think I'll just use c++ and a little bit of obj-c, but over time when my confidence builds up with obj-c, I'll perhaps use that more.

    Another quick question, XCode & tabs, OK I see its only recently added support for tabs, so I guess there are teething issues with this feature, but one annoying thing is say my app crashes it opens up the file with the line where is crashes (which is good) the problem is it replaces the file Im currently viewing with this file, which is nuts. how can I fix this?
    In fact I have many issues with the tabs eg often I have the same file in 5 tabs, surely its possible to have an option to tell XCode only open each file up once.
    Its not so much of an issue now where my project is ~4 files but on bigger projects ~40 files this will be a huge mess. Ta guys
     
  10. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #10
    Just it the back button in the nav bar the to top of the editor and you'll get back to file you were editing. Or hit Control+Cmd+Left Arrow.
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    You didn't show "the c way". You showed the C++ way.

    Here's a more concise, and roughly equivalent, Objective-C for the C++ shown:
    Code:
    a = [NSMutableArray new];
    for(int i=0; i<10; i++)
      myObj = whatever; 
    Of course, the correctness of this depends entirely on the whatever, which must yield an object reference.

    The new method is equivalent to calling alloc followed by init with no parameters. Nothing significant is gained here by init'ing with a predetermined capacity. An NSMutableArrray can hold a varying number of objects. It can also have items inserted or removed from it, and the objects above the insert/delete point will automatically move their position. So NSMutableArray isn't exactly the same as a C++ plain array.
     
  12. zedzeek thread starter macrumors newbie

    Joined:
    Nov 11, 2011
    #12
    yes c++ way ;)
    OK thanks for that (NSMutableArrray sounds like stl::vector) and also cheers to jiminaus.
    Ive gotten my first program working, some balls bouncing around the window.
    One thing Ive discovered is obj-c headers cant include c++ headers, not a major issue. Now onto writing some code.

    Final question, Whats the best way of emulating the accelerometer

    A/ OK I assume hooking up a iphone :p but is it possible to somehow do a semi basic approximation with the mouse or keyboard.
    B/ after I compile my program it takes maybe 3 secs for IOS simulator to start up so I can test the program which is good, now OK say Ive got the iphone hooked up (I dont have one so I cant try this) I assume I have to transfer the program over to that, so my question is approx how long after the program has compiled can I test it on the iphone, 10 secs, 1 minute? i.e. is it a quick painless operation?
     
  13. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #13
    The headers themselves shouldn't care. You need to be #importing them into a ".mm" file (Objective-C++), though. Otherwise their code will be interpreted as plain Objective-C. If I understand the problem correctly.
     

Share This Page