New

Long-Term Learning

Learn efficiently and remember over time.

Start Long-Term Learning

Get personalized study reminders at intervals optimized for better retention.
Track your progress on this set by creating a folder
Or add to an existing folder

Add this set to a folder

  • The interface file (.h) tells the computer what a Fraction class looks like: it has six instance methods. The first three methods don't return a value. The next two return an int, and the last one returns a double. The setNumerator: and setDenominator: methods each take an integer argument.

    Note

    importing files

    <> system files
    "" local files

    Two files created when you make a new class

    header (.h) [interface]
    implementation (.m)

    Synthesized Accessor Methods

    As of Objective-C 2.0, you can have your setter and getter methods (collectively known as accessor methods) automatically generated for you
    1. create the @property in the interface file
    2. create the @synthesize in the implementation file

    @property example

    @interface Fraction : NSObject

    @property int numerator, denominator;

    @synthesize example

    @implementation Fraction

    @synthesize numerator, denominator

    Creates four methods:
    numerator
    setNumerator
    denominator
    setDenominator

    You don't need to use the @synthesize directive. If you don't, the instance variables will have an _ as the first character of its name.

    Note

    The Dot Operator

    Used to access properties:
    [myFraction numerator]
    is the same as
    myFraction.numerator

    General format
    instance.property

    Assign values
    instance.property = value

    Comparison code

    [myFraction setNumerator: 1];
    [myFraction setDenominator: 3];

    same as..

    myFraction.numerator = 1;
    myFraction.denominator = 3;

    Dot notation should only be used for properties. You should use the brackets for methods.

    Note

    Method with multiple arguments

    Simply set each extra argument with a colon following it

    [myFraction setNumerator: 1 andDenominator: 3];
    [myFraction set: 1 over: 3]

    another way:
    set::
    [aFraction set:1 :3]

    local variables

    variables that can only be accessed from within the method that they are defined.

    - (void) reduce
    {
    int u = numerator;
    }

    You must set local variables to a value before you can use them.

    The static keyword

    You can have a local variable retain its value through multiple invocations of a method by placing the keyword static in front of the variable's declaration.

    static int hitcount = 0;
    Has an intial value of 0.

    The self keyword

    Use this keyword to refer directly to the receiver of the current message.

    Use it inside a method

    Inside the add: method
    [self reduce]
    this would reduce the current object

    Please allow access to your computer’s microphone to use Voice Recording.

    Having trouble? Click here for help.

    We can’t access your microphone!

    Click the icon above to update your browser permissions above and try again

    Example:

    Reload the page to try again!

    Reload

    Press Cmd-0 to reset your zoom

    Press Ctrl-0 to reset your zoom

    It looks like your browser might be zoomed in or out. Your browser needs to be zoomed to a normal size to record audio.

    Please upgrade Flash or install Chrome
    to use Voice Recording.

    For more help, see our troubleshooting page.

    Your microphone is muted

    For help fixing this issue, see this FAQ.

    Star this term

    You can study starred terms together

    NEW! Voice Recording

    This is a Plus feature