Documentation Archive Developer
Search

Reviewing the Code

If you have trouble getting your app to work correctly, compare your code with the listings shown at the end of this chapter and review your action and outlet connections.

Code and Compiler Warnings

Your code should compile without any warnings. If you do receive warnings, it’s recommended that you treat them as likely to be errors. Because Objective-C is a flexible language, sometimes the most you get from the compiler is a warning.

Code Listings

This section provides listings for the interface and implementation files of the AppDelegate and Track classes. These listings don’t show comments and other method implementations that are provided by the Xcode template.

The Interface File: AppDelegate.h

#import <Cocoa/Cocoa.h>
 
@class Track;
 
@interface AppDelegate : NSObject <NSApplicationDelegate>
 
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *textField;
@property (weak) IBOutlet NSSlider *slider;
@property (strong) Track *track;
 
- (IBAction)mute:(id)sender;
- (IBAction)takeFloatValueForVolumeFrom:(id)sender;
- (void)updateUserInterface;
 
@end

The Implementation File: AppDelegate.m

#import "AppDelegate.h"
#import "Track.h"
 
@implementation AppDelegate
 
@synthesize textField = _textField;
@synthesize slider = _slider;
 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    Track *aTrack = [[Track alloc] init];
    [self setTrack:aTrack];
    [self updateUserInterface];
}
 
- (IBAction)mute:(id)sender {
    [self.track setVolume:0.0];
    [self updateUserInterface];
}
 
- (IBAction)takeFloatValueForVolumeFrom:(id)sender {
    float newValue = [sender floatValue];
    [self.track setVolume:newValue];
    [self updateUserInterface];
}
 
- (void)updateUserInterface {
    float volume = [self.track volume];
    [self.textField setFloatValue:volume];
    [self.slider setFloatValue:volume];
}
 
@end

The Interface File: Track.h

#import <Foundation/Foundation.h>
 
@interface Track : NSObject
@property (assign) float volume;
 
@end

The Implementation File: Track.m

#import "Track.h"
 
@implementation Track
 
@end

Back to Jump Right In