开发者

Help creating custom iPhone Classes

This should be a simple question, but I just can't seem to figure it out.

I'm trying to create my own class which will provide a simpler way of playing short sounds using the AudioToolbox framework as provided by Apple. When I import these files into my project and attempt to utilize them, they just don't seem to work. I was hoping someone would shed some light on what I may be doing wrong here.

simplesound.h

#import <Foundation/Foundation.h>


@interface simplesound : NSObject {

    IBOutlet UILabel *statusLabel;

}

@property(nonatomic, retain) UILabel *statusLabel;

- (void)playSimple:(NSString *)url;

@end

simplesound.m

#import "simplesound.h"


@implementation simplesound

@synthesize statusLabel;

- (void)playSimple:(NSString *)url {

    if (url = @"vibrate") {
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        statusLabel.text = @"VIBRATED!";
    } else {
        NSString *paths = [[NSBundle mainBundle] resourcePath];
        NSString *audioF1ile = [paths stringByAppendingPathComponent:url];
        NSURL *audioURL = [NSURL fileURLWithPath:audioFile isDirectory:NO];
        SystemSoundID mySSID;
        OSStatus error = AudioServicesCreateSystemSoundID ((CFURLRef)audioURL,&mySSID);
        AudioServicesAddSystemSoundCompletion(开发者_如何转开发mySSID,NULL,NULL,simpleSoundDone,NULL);

        if (error) {
            statusLabel.text = [NSString stringWithFormat:@"Error: %d",error];
        } else {
            AudioServicesPlaySystemSound(mySSID);
        }
    }


    static void simpleSoundDone (SystemSoundID mySSID, void *args) {

        AudioServicesDisposeSystemSoundID (mySSID);

    }
}

- (void)dealloc {
    [url release];
}

@end

Does anyone see what I'm trying to accomplish here? Does anyone know how to remedy this code that is supposedly wrong?


In C based languages, = is an assignment operator, and == is an equality operator.

So when you write this:

if (url = @"vibrate") {

That will always return true, since in C (and hence Obj-C), if statements are 'true' if what's in the brackets is not 0, and an = operation returns the assigned value, which in this case is a pointer to the NSString @"vibrate" (which is definitely not zero).

I don't know exactly why you're trying to compare a URL string to @"vibrate", but the correct way to compare NSString objects is to do something like:

if ([url isEqualToString:@"vibrate"])


if (url = @"vibrate") {

See also Tell The Program What To Do When No Save Data Is Found NSUserDefaults, iPhone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜