Expected method body - objective-c

*.m file*
#import "BNREmployee.h"
#interface BNREmployee ()
#property (weak) IBOutlet NSWindow *window;
#end
#implementation BNREmployee
- (double)yearsOfEmployment
// **Error expected method body**-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Do I have a non-nil hireDate?
if (self.hireDate) {
// NSTimeInterval is the same as double
NSDate *now = [NSDate date];
NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate];
return secs / 31557600.0; // Seconds per year
} else {
return 0;
}
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
*.h file*
#import "BNRPerson.h"
#interface BNREmployee : BNRPerson
#property (nonatomic) unsigned int employeeID;
#property (nonatomic) unsigned int officeAlarmCode;
#property (nonatomic) NSDate *hireDate;
- (double)yearsOfEmployment;
#end
How can i declare "- (double)yearsOfEmploymentsince" since it is declared in another file but while still keeping the "-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {" as the main header???

Your question is a little unclear especially without your .h file to see what this class is a subclass of, but this line: - (double)yearsOfEmployment needs an opening {, that's the "expected method body" problem.

Related

Error in Obj-C : Expected identifier or '('

I'm trying to make a simple app with a button a score counter and a timer but I'm getting some errors
#import <UIKit/UIKit.h>
#interface xyzViewController : UIViewController
{
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;
NSInteger count;
NSInteger seconds;
NSTimer *timer;
}
- (IBAction)buttonPressed //Expected ';' after method prototype
{
count++
scoreLabel.text = [NSString stringWithFormat:#"Score \n %i", count]
}
#end
If I add ';' I get this instead:
- (IBAction)buttonPressed;
{ //Expected identifier or '('
count++
scoreLabel.text = [NSString stringWithFormat:#"Score \n %i", count]
}
#end
What I must to do?
You are mixing up interface and implementation. The interface contains
the (globally visible) instance variables, properties and method declarations, i.e.
the prototypes:
#interface xyzViewController : UIViewController
{
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;
NSInteger count;
NSInteger seconds;
NSTimer *timer;
}
- (IBAction)buttonPressed;
#end
The method itself goes into the implementation:
#implementation xyzViewController
- (IBAction)buttonPressed
{
count++;
scoreLabel.text = [NSString stringWithFormat:#"Score \n %i", count];
}
#end
Remarks:
The convention is to start class names with a capital letter: XyzViewController.
Create properties for the outlets (if you don't have them already):
#property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
The compiler synthesizes the instance variable _scoreLabel automatically, so you don't need it in the interface. And then access the property via
self.scoreLabel.text = ....;
Define your interface in the .h iff they are to be public, then create your implementation in the .m. You can't combine them in the .h
You want semicolons inside the function like so:
- (IBAction)buttonPressed {
count++;
scoreLabel.text = [NSString stringWithFormat:#"Score \n %i", count];
}
This is the correct syntax to use.

Objective-C unable to call a function in Game Center

I'm having a bit of a nightmare trying to implement the ability to have two players playing against each other on my iPhone app through Game Center.
I am simply trying to access a function within GameCenterManager.m from a different .m file that I have. In my racetohundredViewController.h file I have:
#import <UIKit/UIKit.h>
#import "startPage.h"
#import "GameCenterManager.h"
#import <GameKit/GameKit.h>
#interface racetohundredViewController : UIViewController <UIActionSheetDelegate, GKLeaderboardViewControllerDelegate, GameCenterManagerDelegate>
{
GameCenterManager *gcManager;
BOOL gameIsMultiplayer;
double randomHostNumber;
}
#property (retain, nonatomic) GameCenterManager *gcManager;
in my corresponding .m file I have:
#interface racetohundredViewController ()
#end
#implementation racetohundredViewController
#synthesize gcManager;
- (void)generateAndSendHostNumber;
{
NSLog(#"Generate and send host number");
randomHostNumber = arc4random();
NSString *randomNumberString = [NSString stringWithFormat: #"$Host:%f", randomHostNumber];
NSLog(#"the random number string is: %#", randomNumberString);
[self.gcManager testString];
[self.gcManager sendStringToAllPeers:randomNumberString reliable: YES];
}
- (void)viewDidLoad
{
[self generateAndSendHostNumber];
}
It's the [self.gcManager testString]; and [self.gcManager sendStringToAllPeers:randomNumberString reliable: YES]; that are not being called. They were before, I have clearly messed it up somehow. I can see the NSLog for the random number string.
In my GameCenterManager.h file I have (including some bits unrelated to this problem):
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>
#class GKLeaderboard, GKAchievement, GKPlayer;
#protocol GameCenterManagerDelegate <NSObject>
#optional
- (void) processGameCenterAuthentication: (NSError*) error;
- (void) scoreReported: (NSError*) error;
- (void) reloadScoresComplete: (GKLeaderboard*) leaderBoard error: (NSError*) error;
- (void) achievementSubmitted: (GKAchievement*) ach error:(NSError*) error;
- (void) achievementResetResult: (NSError*) error;
- (void) mappedPlayerIDToPlayer: (GKPlayer*) player error: (NSError*) error;
- (void) receivedData:(NSDictionary *)dataDictionary;
#end
#interface GameCenterManager : NSObject <GameCenterManagerDelegate>
{
id <GameCenterManagerDelegate, NSObject> delegate;
NSMutableDictionary* earnedAchievementCache;
id matchOrSession;
}
//This property must be atomic to ensure that the cache is always in a viable state...
#property (retain) NSMutableDictionary* earnedAchievementCache;
#property (nonatomic, strong) id <GameCenterManagerDelegate, NSObject> delegate;
#property(nonatomic, strong) id matchOrSession;
+ (BOOL) isGameCenterAvailable;
- (void) authenticateLocalUser;
- (void)sendStringToAllPeers:(NSString *)dataString reliable:(BOOL)reliable;
- (void)sendString:(NSString *)dataString toPeers:(id)peers reliable: (BOOL)reliable;
-(void)testString;
and finally a cut down version of the .m file for GameCenterManager:
#import "GameCenterManager.h"
#import <GameKit/GameKit.h>
#implementation GameCenterManager
#synthesize earnedAchievementCache;
#synthesize delegate;
#synthesize matchOrSession;
-(void)testString
{
NSLog(#"THIS IS A TEST");
}
I could post code about sendString too but the principle is that even with the basic testString function it is not calling it.
Am I missing something very obvious here? It seems when I step through the code it will highlight the function in the .h file of GameCenterManager, but it won't then display the NSLog as I request.
My best guess would be that self.gcManager is nil. Where do you instantiate your GameCenterManager?

Objective C - My code doesn't see declared #property - Property not found in object of type

I'm new to ObjectiveC and Xcode and I'm having a strange behavior:
This is the Packet class header: Packet.h
#import <Foundation/Foundation.h>
#interface Packet : NSObject
{
short index;
NSData *packetData;
short totalChunks;
}
#property (nonatomic) short index;
#property (strong, nonatomic) NSData *packetData;
#property (nonatomic) short totalChunks;
#end
And this is Packet.m
#import "Packet.h"
#implementation Packet
#synthesize index;
#synthesize packetData;
#synthesize totalChunks;
-(id)init
{
self = [super init];
return self;
}
#end
The problem is i can't use getter and setter although it seems to me that properties are declared and synthesized correctly!
for example:
#import "TransferManager.h"
#implementation TransferManager
-(void)sendAckPacket
{
Packet *packet = [[Packet alloc] init];
packet.index; //ERROR HERE: Property not found on object of type Packet *
}
#end
I know there is no TransferManager.h attached becaues I don't want to write useless stuff, but it imports "Packet.h". Otherwise i should have error during the initialization of *packet.
I'm stuck... Any Help? :)

Passing over Object between Classes not working

hey guys,
i'm new to objective-c and i'm having trouble with the memory management. i declared 3 classes, Table, Dataset and my Main class. In my mainclass, i created an Dataset Object and now im trying to pass this Object over to a Tableobject, where i want to store it permanently. but it seems to me that the garbage collector kills the reference before i can use it.
heres some code:
Dataset:
//Dataset.h
#interface Dataset : NSObject {
NSMutableArray* daten;
}
#end
//Dataset.m
#import "Dataset.h"
#import "Datensatz.h"
#implementation Dataset
- (id) init
{
self=[super init];
daten=[[NSMutableArray alloc] init];
return self;
}
Table:
//Table.h
#class Dataset;
#interface Table : NSObject {
Dataset* daten;
}
-(id)init:(NSTableView *)aTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView;
#property (retain) Dataset* daten;
#end
//Table.m
#import "Table.h"
#import "Dataset.h"
#impl
ementation Table
#synthesize daten;
-(id)init:(NSTableView*)aTableView
{
self=[super init];
[self setDaten:[Datenmenge alloc]];
return self;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
NSLog(#"anzahl: %d %#",[self.daten anzahl], self.daten);//This is always 0 null
return [daten anzahl];
}
-(void)setDaten:(Dataset *)a
{
NSLog(#"setter: anzahl: %d %#",[a anzahl], a);
[daten release];
daten=[a retain];
NSLog(#"setter: anzahl: %d %#",[daten anzahl], daten);
}
#end
In my mainclass i do the following:
//init method
[self setDaten:[[[Dataset alloc]init]autorelease]];
tabelle=[[Table alloc] init:tableview];
[tabelle setDaten:[self daten]];
Mainclass:
//code.h
//
// MalWiederWasNeuesAppDelegate.h
// MalWiederWasNeues
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#class Datenmenge,Graph,Tabelle;
#interface MalWiederWasNeuesAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSToolbarItem *datenKnopf;
NSToolbarItem *speichernKnopf;
NSSlider *scaleSlider;
NSScroller *moveSlider;
NSTableView* tableview;
Graph* graph;
Tabelle* tabelle;
Datenmenge* daten;
}
-(void)tuWas;
- (IBAction)datenHinzufuegen:(id)sender;
- (IBAction)speichern:(id)sender;
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSToolbarItem *datenKnopf;
#property (assign) IBOutlet NSToolbarItem *speichernKnopf;
#property (assign) IBOutlet NSSlider *scaleSlider;
#property (assign) IBOutlet NSScroller *moveSlider;
#property (assign) IBOutlet Graph *graph;
#property (assign) IBOutlet Tabelle *tabelle;
#property (assign) IBOutlet NSTableView* tableview;
#property (retain) Datenmenge* daten;
#end
//code.m
//
// MalWiederWasNeuesAppDelegate.m
// MalWiederWasNeues
//
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "MalWiederWasNeuesAppDelegate.h"
#import "Datenmenge.h"
#import "Graph.h"
#import "Tabelle.h"
#implementation MalWiederWasNeuesAppDelegate
#synthesize window;
#synthesize daten;
-(id) init
{
self.daten=[[Datenmenge alloc]init];
[self.daten datenHinzufuegen:nil];
tabelle=[[Tabelle alloc] init:tableview];
tabelle.daten=daten;
NSLog(#"konstruktor: %f %d",[daten maximum],[daten anzahl]);
//graph.daten=daten;
return self;
}
-(void)tuWas{
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (IBAction)datenHinzufuegen:(id)sender
{
NSLog(#"%f %d",[daten maximum],[daten anzahl]);
NSLog(#"daten hinzufügen");
}
- (IBAction) speichern:(id)sender
{
NSLog(#"%# %#",daten,[tabelle daten]);
NSLog(#"speichern");
}
#end
I hope this wasnt too much code for you.
when i call a method of "tabelle", my Table object, "daten" does not refer to an Dataset Object. But the NSLogs in "setDaten" show me valid references.
so, what am i doing wrong?
have a good evening,
lukas
You define Daten as a retain type
#property (retain) Dataset* daten; and #synthesize daten;
theres no need to then implement the method
-(void)setDaten:(Dataset *)a thats what #synthesize daten; does
I think theres a lost in translation moment here so ill assume Table == Tabelle and Dataset == Datmenge and I dont see the implementation for your main class.
cast your eye over this too.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html
Maybe, the daten property of your main class is declared as assign? In that case daten is correct when you call setDaten:, but might have been already autoreleased when you try to access it afterwards.
Also,
-(void)setDaten:(Dataset *)a
{
NSLog(#"setter: anzahl: %d %#",[a anzahl], a);
[daten release];
daten=[a retain];
NSLog(#"setter: anzahl: %d %#",[daten anzahl], daten);
}
is not a good implementation of a setter. If a == daten, then this object will be released (and maybe dealloc'd). You need to check identity of objects when implementing your own setter.

iOS – NSMutableArray and Block Copy

Currently I’m trying to get the hang of a block copy with my current project. The structure of the copy is an NSMutableArray that contains NSIntegers, NSStrings another NSMutableArray and two Objects… Those objects in turn hold NSStrings. The Array contains Objects which hold an NSInteger and Two Objects which contain strings…
I believe I am supposed to use the Block Copy method for coping objects… Code is below…
I am aware the code is not releasing properly… I tried to make the code smaller for your benefit.
Any insight you could shed would be awesome.
//Main controller Excerpt
//Insert Position Information into temporary node point... Node Points can have multiple Positions (or rather you can face multiple directions at the node. Each Node has 3-4 of these.
[newNode.positionArray insertObject:[newPosition copy] atIndex:currentPosition];
Insert the temporary node into the Node Array.
[nodeArray insertObject:[newNode copy] atIndex:count];
//Main Controller Excerpt
//
// Node.h
//
#import <Foundation/Foundation.h>
#class Sequence;
#class Position;
#interface Node : NSObject {
NSInteger Id;
NSInteger currentPosition;
NSString *title;
NSMutableArray *positionArray;
Sequence *forwardSequence;
Sequence *backSequence;
}
-(id) copyWithZone: (NSZone *) zone;
#property (nonatomic, assign) NSInteger Id;
#property (nonatomic, assign) NSInteger currentPosition;
#property (nonatomic, assign) NSString *title;
#property (nonatomic, retain) NSMutableArray *positionArray;
#property (nonatomic, retain) Sequence *forwardSequence;
#property (nonatomic, retain) Sequence *backSequence;
#end
//
// Node.m
//
#import "Sequence.h"
#import "Position.h"
#import "Node.h"
#implementation Node
#synthesize Id;
#synthesize currentPosition;
#synthesize positionArray;
#synthesize title;
#synthesize forwardSequence;
#synthesize backSequence;
-(id) copyWithZone: (NSZone *) zone {
Node *nodeCopy = [[Node allocWithZone: zone] init];
nodeCopy.Id = Id;
nodeCopy.currentPosition = currentPosition;
nodeCopy.positionArray = [positionArray copy];
nodeCopy.title = title;
nodeCopy.forwardSequence = [forwardSequence copy];
nodeCopy.backSequence = [backSequence copy];
return nodeCopy;
}
#end
//
// Position.h
//
#import <Foundation/Foundation.h>
#class Sequence;
#interface Position : NSObject <NSCopying> {
NSInteger Id;
Sequence *leftSequence;
Sequence *rightSequence;
}
#property (nonatomic, assign) NSInteger Id;
#property (nonatomic, retain) Sequence *leftSequence;
#property (nonatomic, retain) Sequence *rightSequence;
-(id) copyWithZone: (NSZone *) zone;
#end
//
// Position.m
//
#import "Sequence.h"
#import "Position.h"
#implementation Position
#synthesize Id;
#synthesize leftSequence;
#synthesize rightSequence;
-(id) copyWithZone: (NSZone *) zone {
Position *positionCopy = [[Position allocWithZone: zone] init];
positionCopy.Id = Id;
positionCopy.leftSequence = [leftSequence copy];
positionCopy.rightSequence = [rightSequence copy];
return positionCopy;
}
#end
//
// Sequence.h
//
#import <Foundation/Foundation.h>
#interface Sequence : NSObject <NSCopying> {
NSInteger numberOfFrames;
NSString *imageNameScheme;
NSString *endFrame;
}
-(id) copyWithZone: (NSZone *) zone;
#property (nonatomic, assign) NSInteger numberOfFrames;
#property (nonatomic, copy) NSString *imageNameScheme;
#property (nonatomic, copy) NSString *endFrame;
#end
//
// Sequence.m
// MCIT
//
#import "Sequence.h"
#implementation Sequence
#synthesize numberOfFrames;
#synthesize imageNameScheme;
#synthesize endFrame;
-(id) copyWithZone: (NSZone *) zone {
Sequence *sequenceCopy = [[Sequence allocWithZone: zone] init];
sequenceCopy.numberOfFrames = numberOfFrames;
sequenceCopy.imageNameScheme = imageNameScheme;
sequenceCopy.endFrame = endFrame;
return sequenceCopy;
}
#end
Works like a charm now thanks all. :D
If your intent is to make this a copyable class, then you need to declare that it conforms to the NSCopying protocol like so:
#interface Node: NSObject <NSCopying> {
Falling to declare the protocol can cause other objects to believe that the class is uncopyable even if it has a copyWithZone: method.