Resolved NSObject Class

Discussion in 'iOS Programming' started by Nnavick, Jan 31, 2012.

  1. Nnavick, Jan 31, 2012
    Last edited: Feb 1, 2012

    Nnavick macrumors regular

    Joined:
    Oct 13, 2010
    #1
    Hi,
    In C# I can create a class and assign attributes to the object and than pull the attributes out by saying myObject.getPhoneNumber();
    (getPhoneNumber is a method I wrote in myObject class)

    I can't figure out how to do it in Objective C(NSObject class).
    How do I do it?

    Thanks.
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    if you create a property for your object, and synthesize it, Objective-C creates getters/setters.
    So basically
    Code:
     @property (nonatomic, retain) NSString *helloLabel 
    in the header file
    in the implementation file, you just do
    Code:
     @synthesize helloLabel; 
    If you import this class in another class, you can do
    Code:
    Class *pointer = [Class alloc]*init];
    [pointer getHelloLabel];
    pointer.helloLabel (which is the same, dot notation is more Objective C 2.0; previous mentioned is 1.1)
    
     
  3. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010
    #3
    And how do I set attributes when I alloc the object?
    Like in C# myObject obj= new myObject("a",32)
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You would create a custom initWith... (where ... is replaced with sensibly named parameters) that correctly calls the init method of super and assigns the self pointer. This is very basic Objective-C. What book or tutorial are you following to learn the language?
     
  5. KnightWRX macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #5
    Objective-C is not really different from other object oriented programming languages. You have constructors (init functions), you have destructors (the dealloc method), you have methods you can call (your original example does not really need the @property or @synthesize stuff, just make a method named getPhoneNumber in your class).

    Basically, it looks to me like you need to read a book about Objective-C syntax and features. Apple has a free one :

    The Objective-C Programming Language

    There's a more "tutorial" document found here :

    Objective-C 2.0 Essentials

    You could read that tutorial's chapter about Object Oriented programming in Objective-C as that is what you seem to be interested in :

    An Overview of Objective-C Object Oriented Programming

    But really, all this stuff is found in the stickies at the top of the forum, did you read them before asking your questions ? You should definately get on to reading this material, it will answer a lot of your questions instead of just plainly asking about random Objective-C trivia. If you have concepts you don't understand from the material, then feel free to ask for clarifications. I bet you didn't learn C# by asking on a forum about every little tidbits of syntax, you read a book or tutorials or took a class. ;)
     
  6. Nnavick thread starter macrumors regular

    Joined:
    Oct 13, 2010

Share This Page