Objective-C Index Casting

I am new to Objective-C and come from a Java background. I just switched to casting in Objective-C, but in the book I am using, I was unable to explain the use of the '*' / pointer when casting. Here is an example they gave me:

myFraction = (Fraction *) fraction;

Are not pointers to specific objects, so do they have their own unique memory location? So why should I use a pointer just referring to the class? In this case, the fraction.

Thank you, I hope this makes sense, and I know that this is a simple question that I should know and understand, but I could not find anything to explain.

+3
source share
4 answers

A symbol *has several meanings (in addition to multiplication :):

  • (). , pointerToInt, .

    (*pointerToInt) = 5;
    
  • . int *, " ".

    int x = 5;
    int * xPtr = &x
    

- , . . , 99% , * ( :) : :

  • NSString *= NSString ( NSString)
  • Fraction *= Fraction Fraction Fraction

, " ", " ".


**. NSError, methodWithError:(NSError **)errorPtr.

: int int *, NSError * NSError **.

. ( ), . , ? ! *, - .

NSError *error = nil; // Empty.
NSError **errorPtr = &error; // Reference to our local `error` variable.

[data writeToURL:URL options:kNilOptions error:errorPtr];
// That method uses:   (*errorPtr) = [NSError errorWith...];

NSLog(@"Error: %@", error); // Our local error is no longer empty.

, , Java. C, .

+3

* - , .

myFraction fraction , ( ), , Objective-C, ).

(Fraction*) -- ( fraction).

+2

, - , .

Objective-C, , , , , .

( ). (void *). , , .

, , . -, Java.

+1

... * */ ...

, , . :

  • Fraction - - , , , Fraction Number.

  • Fraction * - Fraction. Objective-C , ClassName *.

- , . , , Number Number *, , , , Fraction. - , Fraction, , , Number - Number *. , : " , , Number Fraction, , , Number Fraction *." :

Fraction *f = (Fraction *)number;

, * , , Fraction * - , Number.

+1
source

All Articles