Few objective c questions

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

  1. Torchi12 macrumors newbie

    Joined:
    Oct 2, 2011
    #1
    1.
    Watched a tutorial about substrings.
    Here is some code:
    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?

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

    3.
    When I make a new file, it automatically adds the following:
    Code:
    #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
    @end
    
    Should it always look like that? With that init, and dealloc?
    The way I've learnt Obj c is just simply

    Code:
    #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?

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

    5.
    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

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.

    http://developer.apple.com/library/mac

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

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    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

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

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    1.*NSString is here:
    http://developer.apple.com/library/...lasses/NSString_Class/Reference/NSString.html
    NSRange is here:
    http://developer.apple.com/library/...oundation_DataTypes/Reference/reference.html*
    They are different sorts of things. Start your research there for immediate answers, but head here for more of a foundation:
    http://developer.apple.com/library/mac/documentation/cocoa/conceptual/objectivec/objc.pdf

    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:
    http://developer.apple.com/library/...onceptual/MemoryMgmt/Articles/MemoryMgmt.html
    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.

    -Lee
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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":
    http://en.wikipedia.org/wiki/C_syntax
     
  7. lee1210 macrumors 68040

    lee1210

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

    -Lee
     
  8. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #8
    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