Expected identifier or '(' before #interface - objective-c

First post and I am really hoping this is not a repetitive or solved question. I tried searching here and Google and while I have found similar Expected identifier or '(' errors none of the solutions work for me.
Basically I'm trying to learn Design patterns and as I used to know a bit of java I am trying to use it as an opportunity to learn objective-c so I have a java program that works and an xCode project that I get the error Expected identifier or '(' in my header file just before the #interface
this is my java solution (very simple I know):
public class Duck {
public void quack(){
System.out.print("Quack!");
}
public void swim(){
System.out.print("swimming duck!");
}
public void display(){
quack();
swim();
}
}
public class mainClass {
public static void main(String[] args){
Duck duck = new Duck();
duck.display();
}
}
and this is my objective-c version.
//duck.h
#include <CoreFoundation/CoreFoundation.h>
#interface Duck : NSObject{ //Expected identifier or '('
}
#end
// Duck.m
#include "Duck.h"
#implementation Duck
-(void)quack{
printf("Quack!");
}
-(void)swim{
printf("swimming duck!");
}
-(void)display{
[self quack];
[self swim];
}
#end
// main.c
#include <CoreFoundation/CoreFoundation.h>
#include "Duck.m"
int main(int argc, const char * argv[])
{
Duck *duck = [[Duck alloc] init];
[duck display];
return 0;
}
If any one can help I would greatly appreciate it, and again sorry if this is a duplicate post

The compiler doesn't know what NSObject is. If you look at the reference, you'll see that it's part of the Foundation framework, not CoreFoundation, so:
#import <Foundation/Foundation.h>
instead of:
#import <CoreFoundation/CoreFoundation.h>

//duck.h
//#include <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h> // or Cocoa/Cocoa.h
#interface Duck : NSObject//{ //Expected identifier or '('
//} not necessary if there are no instance fields
- (void)quack;
- (void)swim;
- (void)display;
#end
// Duck.m
//#include "Duck.h"
#import "Duck.h"
#implementation Duck
-(void)quack{
printf("Quack!");
}
-(void)swim{
printf("swimming duck!");
}
-(void)display{
[self quack];
[self swim];
}
#end
// main.c SHOULD BE ~main.m~ if using ObjC!!!
//#include <CoreFoundation/CoreFoundation.h>
//#include "Duck.m"
#import "Duck.h"
Additionally, get in to the habit of using NSString literals; #"example" for if/and when you decide to advance into Cocoa. Good luck with your studies.

It could be that you don't really need curly brackets on your empty interface:
#interface Duck : NSObject
#end

Try using import instead of include. Also, make sure that the CoreFoundation framework is actually part of your project.

Related

Expected identifier or '(' objective C

Just starting out with Objective C so please be gentle. I've got a class as follows:
Card.h
#import <Foundation/Foundation.h>
#interface Card : NSObject
#property (nonatomic) NSUInteger x;
-(NSUInteger) getNum;
#end
Card.m
#import "Card.h"
#implementation Card
-(NSUInteger) getNum {
return self.x;
}
#end
main.c
#include <CoreFoundation/CoreFoundation.h>
#include "Card.h"
int main(int argc, const char * argv[])
{
return 0;
}
When I compile, I get a load of errors, first one is:
NSObjCRuntime.h: Parse Issue: Expected identifier or '('.
I know this is something stupid - just hoping somebody here can spot what i'm doing wrong.
Are you actually compiling this with an Objective C compiler? The traditional extension to instruct GCC and clang to use ObjC is .m (not .c).

Is it possible to scope enums to a class in Objective-C?

I'm new to Objective-C and trying to figure out enums. Is there a way to scope an enum to a class so that the values could be used by another class? Something like this:
#interface ClassA {
typedef enum {
ACCEPTED,
REJECTED
} ClassAStatus;
}
#end
#interface ClassB {
typedef enum {
ACCEPTED,
REJECTED
} ClassBStatus;
}
#end
Though that does not work, obviously. Or is there a better way to do enums altogether?
Edit: I guess my wording wasn't clear, but I'm not asking how to declare enums. I'm aware that putting them at the top of the file works. I'm asking if there's a way to scope them so the values aren't global to the entire file.
You have to prefix your public enums. Simply put the enum definition in the header of your class.
// ClassA.h
typedef enum {
ClassAStatusAccepted,
ClassAStatusRejected
} ClassAStatus;
#interface ClassA {
ClassAStatus status;
}
#end
// ClassB.h
typedef enum {
ClassBStatusAccepted,
ClassBStatusRejected
} ClassBStatus;
#interface ClassB {
ClassBStatus status;
}
#end
This is how Apple does it.
Or you could use the new style:
// UIView.h
typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
UIViewAnimationCurveEaseInOut, // slow at beginning and end
UIViewAnimationCurveEaseIn, // slow at beginning
UIViewAnimationCurveEaseOut, // slow at end
UIViewAnimationCurveLinear
};
Just put the enum right at the top of your .h, like Apple does in UITableView.h, for example:
//
// UITableView.h
// UIKit
//
// Copyright (c) 2005-2012, Apple Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIScrollView.h>
#import <UIKit/UISwipeGestureRecognizer.h>
#import <UIKit/UITableViewCell.h>
#import <UIKit/UIKitDefines.h>
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped // preferences style table view
};
typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
}; // scroll so row of interest is completely visible at top/center/bottom of view
typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight, // slide in from right (or out to right)
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone, // available in iOS 3.0
UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = 100 // available in iOS 5.0. chooses an appropriate animation style for you
};
You probably recognize some of these name, but you may not have been aware they were actually apart of a public enum in Apple's header files.
Just want to add to #DrummerB's answer that I usually write like this. The namespace in camelCase, and then the constant in upper case.
typedef enum {
ClassAStatus_ACCEPTED,
ClassAStatus_REJECTED
} ClassAStatus;

iOS - how do I create a "utility" class that all my controllers can call

I am new to iOS and obective-c so I am not too sure how to best accomplish this seemingly simple task.
What I want is to make a class that looks like this in pseudocode:
class UtilityClass
{
// Have a method that I can pass parameters to
String doCalculation ( String art1 , String arg2 )
{
return arg1 + arg2;
}
}
My uncertainty is:
1) xCode seems to be inclined to lay out my file structure in a relatively flat way. So should I make a utils directory and have this file be under utils/fileName ? Usually I am kind of used to having at least some src directory, but so far I have not been prompted by anything to create one.
2) How do I import and call this class/function from my controllers?
Thanks,
Alex
Just create a new group called Utilities, and then create your class inside it. Like,
utils.h
utils.m
Later in your ViewController's header file just add.
#import "utils.h"
if this utils class is used by many controllers in very fat project then, find a file called, Should be inside supporting files group.
YourAppName-Prefix.pch
In that file you have a code block like this,
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#endif
Just edit this block and add your utils.h reference here, In this way your entire project can create utils object without explicitly importing into their own header.
Edit like this.,
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "utils.h"
#endif
First of all create a new File in Xcode and uncheck xib file. Name the Project as you like . and extend it from NSObject .
for creating static method you have to replace function starting - to + like
interface. class
#interface Utility : NSObject
+ (int)getNumber;
+ (void)setNumber:(int)number;
#end
.m class
#import "Utility.h"
#implementation Utility
static int number = 1;
+ (int)getNumber {
return number;
}
+ (void)setNumber:(int)newNumber {
number = newNumber;
}
+ (id)alloc {
[NSException raise:#"Cannot be instantiated!" format:#"Static class 'ClassName' cannot be instantiated!"];
return nil;
}
#end
call it in any other ViewController like
NSLog(#"number = %d", [Utility getNumber]);
[Utility setNumber:3];
NSLog(#"number = %d", [Utility getNumber]);
for details..
Where you store the files is up to you, just make sure XCode knows where to find them. The class itself should be made like any other Objective C class, just make it inherit from NSObject instead of one of the graphical classes:
// MyClass.h
#interface MyClass : NSObject {
int instanceVar;
}
#property (nonatomic, assign) int property;
#end
// MyClass.m
#import "MyClass.h"
#implementation MyClass
#synthesize property;
-(id) init {
...
}
-(int) function {
...
}
#end
To use the class in another file, just import the header like any other class
#import "MyClass.h"

typedef in header causes error "Expected specifier-qualifier-list before 'typedef'"

Dear wisdom of the internet,
in the header-file (Objective-C)
myTestClass.h
#import <Foundation/Foundation.h>
#interface myTestClass : NSObject {
typedef int pixel;
}
- (id) initWithPic: (NSString*) picFileName;
- (void) dealloc;
- (void) doSomething;
#end
At the line typedef int pixel;xCode complains like
( ! ) "Expected specifier-qualifier-list before
'typedef'" ( 3 )
This err-msg seems pretty popular but given solutions (missing #import) do not work for me.
Also the hints I found do not explain what is going wrong here.
I do not understand this err-msg
Can someone explain it to me?
I do appreciate any tips.
Not sure what you are trying to do, put your should just put the typedef before your interface.
Inside the braces is the place for iVars.
If you want an integer variable, then you do not need the typedef:
#interface MyClass
{
int myPixel;
}
#end
Typedefs are used to create a new type, based on another. For instance:
typedef int pixel;
#interface MyClass
{
pixel myPixel;
}
#end
So when you use the pixelpseudo-type, the int type will be used.

an someone please explain to me what #ifdef does here?

Can someone please explain what #ifdef..#else..#endif does in this piece of code? It's from an open-source iphone twitter client.
#ifdef ENABLE_OAUTH
#interface NTLNTwitterClient : NTLNOAuthHttpClient {
#else
#interface NTLNTwitterClient : NTLNHttpClient {
#endif
int requestPage;
NSString *screenNameForUserTimeline;
BOOL parseResultXML;
NSObject<NTLNTwitterClientDelegate> *delegate;
BOOL requestForTimeline;
BOOL requestForDirectMessage;
NTLNTwitterXMLParser *xmlParser;
}
If ENABLE_OAUTH is defined somewhere else, then the class NTLNTwitterClient will be a subclass of NTLNOAuthHttpClient.
If ENABLE_OAUTH is not defined, then the class NTLNTwitterClient will be a subclass of NTLNHttpClient.