Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c symbols ** & +-

Just when I think I'm getting comfortable with Objective-c the mentioned symbols totally throw me down a rabbit hole...

**     a double pointer??

&     what sort of things can I do with &reference, is a @property? a full on object? weird pointer razzledazzle?

±     I see both a + or a - before method declarations; I've seen Java annotate some datatype declarations by manually typing the + and the magic of compiling in Eclipse would change them to a -

I'm likely asking repetitious questions and/or way outta the ballpark on my guesses; thanks for answers/edits.

like image 966
edwardsharp Avatar asked Aug 30 '11 05:08

edwardsharp


People also ask

What does* do in Objective-C?

The asterisk symbol in C programming language is used as a pre-fix before a variable name to specify that the variable can store address reference of a memory location, i.e. asterix (variable name) makes the variable a pointer.

What is the symbol in Objective-C?

The @ symbol is used in a few places in Objective-C, and basically it's a way to signal that whatever it's attached to is special to Objective-C and not part of regular C. This is important when the computer compiles Objective-C code.

What @{} means in Object C?

The @ character isn't used in C or C++ identifiers, so it's used to introduce Objective-C language keywords in a way that won't conflict with the other languages' keywords. This enables the "Objective" part of the language to freely intermix with the C or C++ part.

What are methods in Objective-C?

Basically in Objective-C, we call the function as method. The Objective-C foundation framework provides numerous built-in methods that your program can call. For example, method appendString() to append string to another string.


2 Answers

You're getting into the C portion that objective-c is built on top of.

** is a pointer to a pointer. Since functions in C take arguments by value, it means you can't change the value of the argument in that function. But, by providing a level of indirection and passing a pointer to the pointer, you can change the value.

& means it's a reference. If an argument takes a ** and you have a * variable, pass a reference to it.

Foo *foo;
[self changeFoo: &foo];

- (BOOL)changeFoo: (Foo **)foo
{
     // dereference the double pointer and assign a val = alloc init returns a *
     *foo = [[Foo alloc] init];
     return YES;
}

A common usage in objective-c / cocoa is NSError. It's essentially an out argument.

NSError *err;
BOOL success = [self doSomething:@"Foo" error:&err];

- (BOOL)doSomething:(NSString*)withData error:(NSError**)error
{
}
like image 135
bryanmac Avatar answered Oct 18 '22 20:10

bryanmac


As you might know, a pointer points to the address of an object and is the way you reference an object. A double pointer is sometimes used in Objective-C, mainly for returning NSErrors, where you want to get back the address, i.e. a pointer, to an error object (NSError) if an error occurred, thus you pass in a pointer assigned to null and the caller can change that pointer so that it points to the address of another pointer which in turn points to an NSError object.

The ampersand (&) is mostly used by the lower level C APIs, e.g. Core Graphics. They are used to reference things, like the current context. As long as most of your code uses square brackets around its method calls you won't see these very often.

Using a + or a - before a method declarations is used to differentiate between class (+) and instance (-) methods. A class methods is called on the class itself (such as alloc), while a instance method is called on an instance of that object (such as init).

like image 43
David Rönnqvist Avatar answered Oct 18 '22 21:10

David Rönnqvist