Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-20-2008, 04:52 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default Calling function in a different class file

Basically I'm wondering how to call a function which is in another class file.
I've got
Code:
#import "myClassFile.h"
in the top of the class I'm calling from.

I'm calling it like this
Code:
[myClassFile myFunction];
Now in myClassFile.h I have
Code:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


@interface myClassFile : NSObject {

}

-(void)myFunction;
@end
Then in myClassFile.m I have this
Code:
#import "myClassFile.h"


@implementation myClassFile

-(void)myFunction
{
	NSLog(@"myFunction");
}
@end
When I come to running it I get
Quote:
warning 'myClassFile' may not respond to '+myFunction'
Why can't a call my function from a different class file?
Thanks,
Tom
harrytheshark is offline   Reply With Quote
Old 12-20-2008, 05:05 PM   #2 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 88
Default

Code:
-(void)myFunction;
The "-" indicates an instance method. You need to create an instance ([[myClassFile alloc] init]) to call it.

You can also replace the "-" with a "+" to make it class-level, this is similar to declaring a function "static" in Java.
nacht is offline   Reply With Quote
Old 12-20-2008, 05:12 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Forget this post I just realised what you meant, I'm very tired lol.
Thanks again,
Tom

Last edited by harrytheshark; 12-20-2008 at 05:18 PM.
harrytheshark is offline   Reply With Quote
Old 12-20-2008, 05:21 PM   #4 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 88
Default

I would recommend starting by learning the basics of programming before messing with the intricacies of iPhone programming. While it is certainly possible to use Objective-C as a learning language, the resources out there for learning it mostly assume knowledge of another language, and iPhone development in particular has a variety of quirks inherent to a (relatively) new SDK and working on an external device or in a simulator.

How to think like a computer scientist: Learning With Python

The most relevant sections come later (e.g., Chapter 13), but it covers most of the basics. Python is a good language to start from for learning those basics.

If you prefer a C-derived language, there's always Java or C++, the last of which is closest to Objective-C.

If you really insist on getting started immediately with Objective-C:

Code:
[[myClassFile alloc] init] myFunction];
alloc allocates the memory.

init acts as a constructor.

myFunction is your function and can then be called after the object has been created.

Last edited by nacht; 12-20-2008 at 05:24 PM.
nacht is offline   Reply With Quote
Old 12-20-2008, 05:36 PM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Thanks for the link.

Tom

Last edited by harrytheshark; 05-15-2010 at 09:41 AM.
harrytheshark is offline   Reply With Quote
Old 04-12-2010, 08:41 AM   #6 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 67
Default

Why dont we first discuss how Apple makes their money off of marketing before we get upset at the people trying to learn system programming?
scleland is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 258
17 members and 241 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, Grinarn, HemiMG, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:50 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0