Annette
Annette

Reputation: 127

How do include and call sample code methods in Objective-C?

iPhone/Mac "play a sound class": link text

I find an awful lot of great objective-c classes and code samples here... and elsewhere.

I successfully create the .h and .m files, but how do I call them from my existing code?

Upvotes: 1

Views: 302

Answers (2)

Kurbz
Kurbz

Reputation: 11003

Wherever you want to call a method from one of the classes, put #import "SomeClass.h" at the top of the .h file.

Then you can do [SomeClass someMethod] or SomeClass *object = [[SomeClass alloc] init], or whatever you want to do.

This is pretty basic, you should read through The Objective-C Programming Language Guide

Upvotes: 0

Kyle Browning
Kyle Browning

Reputation: 5382

Usually Annette, you can tell what needs to be done by looking at the objects superclass

in this case, if you look at the .h file you can see @interface Sound : NSObject

Sound is the name of this Class, NSObject is our superclass

the initWithPath method is returning itself and does a [super init] meaning that it calls the parents init method.

In order for you to call this methods theres one of two ways.

You can have a property that you manage lets say, in your delegate.

@class Sound;
@interface ScanViewController : UIViewController  {
    Sound *aSound;
}
@property (nonatomic, retain) Sound *aSound;

then somwhere in your delegate

- (void) someFunction() {
   aSound = [[Sound alloc] initWithPath:@"pathtoSound"];
}

If you didnt want it to be a property you can easily create a new Sound object anywhere in a .m file like so.

Sound *mySound = [[Sound alloc] initWithPath:@"pathtoSound"];

If you wanted multiple sounds, Store them in a Sound Array

P.S. dont forget to release these objects since you alloc'ed them.

Upvotes: 1

Related Questions