简体   繁体   中英

Execute C Code in iPhone Objective-C Project on Launch

I am just starting out in Objective-C programming, I have experience with C. I have some code (about 124 lines) that I am wanting to port to iOS (from OS X). The purpose of the code is to generate a URL and then open it in Mobile Safari. Seeing as the app does not have a user interface and it's sole purpose it to generate and open a URL, I want this code to be executed on launch of the application.

I have done some research and found out that if I want some code be executed on launch I have to put it in the "App_NameDelagate.m" file, in the

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ...  }

section, between the curly brases. The problem is, when I paste in my code and compile it (in Xcode), I get about 7 or so errors and warnings, saying, at every function declaration:

Nested functions are disabled, use -fnested-functions to re-enable

And at one point (when I start writing the code for my various functions, and the main "if" function ends)

Control reaches end of non-void function

The code it self is made up of 1 "if" function and about 5 other functions that get called during the "if" function.

I know the code it self is fine as I used it to build a Mac version of the app with the same purpose (to generate and open the URL) and it works fine.

I realise some of the Cocoa stuff changeswhen going from OS X to iOS, like, to open a URL on iOS use [UIApplication sharedApplication] rather than [NSWorkspace sharedWorkspace] , as you would on OSX. So I dont think that is the problem. I also removed the standard declaration of the "main" function at the start of my pasted code as I know it is also declared in the "main.m" file.

In the Mac version of my app I was able to put my code straight into the "main.m" file and the code would compile and the application would run just fine. On iOS I gather, this is not the case.

Any help with this would be greatly appreciated as it is driving me crazy!

Thanks.

UPDATE: Lines causing problems with 'nested functions' are:

int generateCrosswordNumber(){

int getDayNumber(){

bool checkIfSunday(){

void handleSunday(){

void normalDay(){

Below each of these lines are the code for the functions that get called. For each function there is another curly brace that closes the one that is opened on the above lines.

The return YES fixed the function reaches end of non-void function error, worked great, thanks!

So first of all, if this app is for the App Store, I think you do have to have some sort of UI. I don't think you can load and then bail to Safari immediately. But you'll have to check that with Apple.

But the issue you're having is that you normally can't define C functions within an Objective C method. [I guess you could enable nested functions in the compiler, but then I've never tried that with nested C functions inside an objective C method. Theoretically that should work, but it's not how we normally do things.]

But you can call C functions in your method. In fact, we do that all the time. It sounds like you're doing something like this:

- (int)someMethod:(int)x {

    int someFunction(int x) {

        return x + 2;
    }   

    x = someFunction(x);

    return x + 3;
}

Note you're defining a C function inside the definition of the method. But instead you can just do this:

int someFunction(int x) {

    return x + 2;
}

- (int)someMethod:(int)x {

    x = someFunction(x);

    return x + 3;
}

Now you're defining a C function and then also defining an objective C method that uses that function.

EDIT

Out of curiosity, I did test some simple cases of nesting C function definitions within ObjC method definitions, and that seems to work fine in GCC with -fnested-functions. Maybe for some simple case that might be more expressive than defining the function at the top level, but I think if you're doing this a lot or certainly for more than one function in a method, then that is probably not a great design. Also, Clang does not support this so it's probably not in your long term interest to write code like this on this platform at least.

Your Objective-C code is structurally incorrect. Your best approach to finding where the structure is broken is probably to Select All and then Edit->Format->Re-Indent .

All functions definitions should start in the leftmost column, and what's broken the parsing of your file should be obvious. It'll most likely be a missing ] or }.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM