Just learning Objective-C coming from a Java background. I am trying to write a program that has no purpose other then to teach me how to write functions in this language and I am getting errors everywhere. The problem is since I just started with this language yesterday the errors make no sense to me. Here is what I have so far.
Main Method:
int main (int argc, const char * argv[])
{
@autoreleasepool
{
NSString * prompt = @"Hello World";
prompt = writePromptMessage(prompt);
NSLog(@"%@", prompt);
}
return 0;
}
Special Method:
NSString *writePromptMessage(NSString * prompt)
{
return prompt;
}
My Errors:
- Implicit declaration of function writePromptMessage is invalid in C99
- Implicit conversion of ‘int’ to ‘NSString *’ is disallowed with ARC
- Incomplete integer to pointer conversion assigning NSString strong from int
- Conflicting Types for writePromptMessage
Unlike Java, you need to add a function declaration (aka a function prototype) before the point where it’s used. A function declaration is just like the function definition, but without the body (i.e. the code) of the function, and ending with a semicolon:
In order to be able to call a function, you need to have written a declaration before the usage site:
Alternatively, you can place the whole definition before the usage site, but this won’t always be possible (e.g. if you have two functions which can call each other).
C and Objective-C do actually allow you to call functions that don’t have visible declarations, but this is considered a deprecated feature because it’s easy to misuse and cause subtle errors:
If a function is called without a visible declaration, the compiler creates an implicit
declaration:
intSo, what happened is that the compiler is assuming that
writePromptMessagereturnsintwhen it first sees it, which is wrong, and that causes a cascade of other errors. For functions which don’t returnint, you must never use implicit function declarations, and for functions which do returnint, you should never use implicit function declarations.