user559142
user559142

Reputation: 12517

iPhone/Objective C - Executing Code without a view

I have a simple function which I wish to execute in objective c. I am using xcode and this is just a dummy application which requires no view. I have been told never to change the main method..so the question arises, how can I execute this function. the function is stored in a simple objective c class in the classes folder and I am using a window based project....

Thanks

Upvotes: 0

Views: 274

Answers (3)

Praveen S
Praveen S

Reputation: 10393

I would say you want to write the Model part of the MVC architecture first :D. Anyways you can always have that app without view and the entry point for any application is application:didFinishLaunchingWithOptions method of your delegate.

If you just want to write a objective C program and still want to use xcode you can create a empty project and write your classes and methods just like any other objective-c program. You can also specify build options in xcode.

Upvotes: 1

Jesse Naugher
Jesse Naugher

Reputation: 9820

Import the file (header if it has one) into the App Delegate; in the applicationDidFinishLoading: method: create an instance of the class the function is in (assuming its not a class method (starts with a + instead of a -)); and call the function on your created instance, you may want to NSLog the return value if there is one.

Something like this in your appdelegate.m

#import "Appdelegate.h"
#import "MyClass.h"

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MyClass *testClass = [[MyClass alloc] init];
NSString *result = [myClass testFunction]; //initialize and include paramaters if need be, etc etc. also whatever you return should be what result is, obviously
NSLog (@"My result: %@", result);

return YES;
}

Upvotes: 3

Gytis
Gytis

Reputation: 670

Call your function in the method application:didFinishLaunchingWithOptions: which is in your application delegate file.

Upvotes: 2

Related Questions