Few objective c questions

Discussion in 'Mac Programming' started by Torchi12, Oct 11, 2011.

  1. Torchi12 macrumors newbie

    Oct 2, 2011
    Watched a tutorial about substrings.
    Here is some code:
    NSString *d = @"Dont feed grapes to dogs!";
    NSRange range = [d rangeOfString:@"grapes"];
    Now, my question is, why is the NSString object a pointer but not the NSRange object?

    I don't quite understand static, could someone try to explain?

    When I make a new file, it automatically adds the following:
    #import "testLolIgnoreMe.h"
    @implementation testLolIgnoreMe
    - (id)init
        self = [super init];
        if (self) {
            // Initialization code here.
        return self;
    - (void)dealloc
        [super dealloc];
    //and other methods u create yourself
    Should it always look like that? With that init, and dealloc?
    The way I've learnt Obj c is just simply

    #import <Foundation/Foundation.h>
    #import "FakeClass.h"
    int main(int argc, const char* argv[])
    NSAutoreleasePool *pool = [ [NSAutoreleasePool alloc] init];
    FakeClass *object = [ [FakeClass alloc] init];
    //do some stuff
    [object release];
    [pool drain];
    return 0;
    Is my way wrong, outdated or something?
    Should I start doing it like the example code? The one that shows up when i create a new file?

    About objects, I do it by doing = [ [Class alloc] init ];
    But I've seen other ways, like autorelease.
    Should I continue with alloc, init?

    Autoreleasepools are only used when u initialize objects with autorelease?
    And send a release message to all obejcts that use autorelease?
    Something like that? Read it somewhere, always understand better when people explain to me though.
  2. jiminaus macrumors 65816


    Dec 16, 2010
    Too many questions which would each require an essay because you're lacking fundamental understandings.

    I suggest you start reading good material instead of watching tutorials. If you aren't willing or able to get your hands on a book, then start reading the Apple documentation.


    Read the documents under the Getting Started tab. Then move on to the documents under the Required Reading tab.
  3. gnasher729 macrumors P6


    Nov 25, 2005
    Instead of trying to get it on the internet for free, why don't you try to hire someone to give you lessons? That's how I made a bit of money when I was sixteen; you should have no problem finding someone who will give you lessons for $15 or $20 an hour.
  4. iPhone4UK macrumors 6502

    Jun 15, 2010
    I'd love someone to give me lessons maybe over webcam or something. Any one know where to look?
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    1.*NSString is here:
    NSRange is here:
    They are different sorts of things. Start your research there for immediate answers, but head here for more of a foundation:

    2. This means different things in different contexts. In a class declaration context this means the variable is a class member variable, so whenever this variable is referenced at the class level or from an instance method, the exact same variable is used. This is for storing class-level state, such as a pointer to a singleton instance.
    In a plain C function this means that the variable lives on the heap, and the variable has program lifetime. It will persist across invocations, contrasting with automatic, stack-local variables, the "normal" case.

    3. This is the difference between a barebones class implementation file and a basic entry point to a program. main is the entry point to all C and Objective-C programs. As others have noted, this is as basic as it comes. What docs have you read so far?

    4. Read the memory management programming guide:
    It will tell you about retain, release, and autorelease. It will explain ownership, if you own an object returned from a method with init in the name, and lots of other critical pieces of information.

    5. Read the guide from step 4 again. You'll need to at least twice, anyway. It's that important. You always need an autorelease pool. Even if you don't send a single autorelease, code you're calling will. The specifics of when autoreleased objects get released isn't important, ownership is.

  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Lifetime is correct. Storage class isn't.

    A static variable has static storage class, never heap.

    See here. Look for "storage class", or at subheading "Storage duration specifiers":
  7. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Touché. A bit sloppy on that one, I meant "not on the stack", but that doesn't mean the heap.

  8. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    Which may or may not just be a difference in vocabulary, as some compiler/linker-loader/runtime combinations allocate C static storage class memory from the heap, before linking it to a runtime global base pointer.

Share This Page