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).
Related
I've been tinkering all day, and I can't seem to fix this error.
Here's the code:
//
// main.m
// Learning ObjC
//
// Created by Nickirv on 8/9/15.
// Copyright (c) 2015 Nickirv. All rights reserved.
//
#import <Foundation/Foundation.h>
#interface Person: NSObject{
int age;
int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) SetWeight: (int) w;
#end
And it outputs this issue:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I would appreciate any help! Thank you very much!
You can't delete the main boilerplate code:
int main(int argc, const char * argv[]) {
#autoreleasepool {
}
return 0;
}
In the end Objective-C is "C" and the program starts execution by calling main. Additionally Objective-C code needs to execute in an autoreleasepool.
You define class #interfaces and #implemtations outside of (generally above) the boilerplate but the first line of code to run must be within the autoreleasepool scope {}.
Here is an example Objective-C program similar to what the OP seems to want using #properties for simplicity and demonstration.
It is important to study Objective-C documentation until the following code is fully understood, td;dr does not work for this.
#import <Foundation/Foundation.h>
#interface Person: NSObject
#property int age;
#property int weight;
- (void)print;
#end
#implementation Person : NSObject
- (void)print {
printf("Age: %i, weight: %i", self.age, self.weight);
}
#end
int main(int argc, const char * argv[]) {
#autoreleasepool {
Person *don = [[Person alloc] init];
don.weight = 130;
don.age = 23;
[don print];
}
return 0;
}
Output:
Age: 23, weight: 130
I just started out programming in Objective C. This is very simple stuff but im wondering what im missing on my knowledge of properties, as far as I understand this should work but it gives me the error:
Property 'score' not found on object of type 'Player *'
I have a Player class and the code as follows below, each bolded is a separate file
Player.h has:
#import <Foundation/Foundation.h>
#interface Player : NSObject
#property int score;
#end
Player.m has:
#import "Player.h"
#implementation Player
- (id)init {
self = [super init];
if (self){
_score = 5000;
}
return self;
}
#end
main.m has
#import <Foundation/Foundation.h>
#import "Player.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
Player *firstPlayer = [[Player alloc] init];
NSLog(#"The default score is %i", [firstPlayer score]);
}
return 0;
}
Player.h is missing the following line which should go between the #import and #property lines:
#interface Player : NSObject
Where NSObject may be a different class, but needs to be whatever you intend to be subclassing.
I know I'm probably making a stupid mistake but I'm working my way through the book Programming in Objective-C and I'm getting a couple of errors but I cant seem to find the mistake.
person.m
#import "person.h"
#implementation person{
int age;
}
-(void) print{
NSLog(#"the person is %i years old", age);
}
-(void) setAge:(int)a{
age = a;
}
#end
person.h
#import <Foundation/Foundation.h>
#interface person : NSObject
-(void) print;
-(void) setAge: (int)a;
#end
main
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
person *newPerson; //error is on this line use of undeclared identifier:person
}
return 0;
}
You need to import person.h header in all files you want to use Person class, so add
#import "person.h"
line to your main file
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.
I'm trying to compile my Objective-C application but it fails saying "Unknown type name 'OpenGLView'". Now I know what this means but the weird thing is I'm convinced I'm importing it just fine.
#import <Foundation/Foundation.h>
#import "OpenGLView.h"
#import "Frame.h"
#import "Mesh2.h"
#import "Vector2.h"
#import "StaticRigidBody.h"
#import "DynamicRigidBody.h"
#interface PhysicsApplicationController : NSObject {
Frame* frame;
OpenGLView* view;
}
It fails on line 11 of this sample. You can see I import it on line 2. The thing which I can't get my head around though is the same import works completely fine in this segment.
#import <UIKit/UIKit.h>
#import "OpenGLView.h"
#interface PhysicsAppDelegate : UIResponder <UIApplicationDelegate> {
OpenGLView* _glView;
}
Both source files for the two segments are in the same directory. I have no idea what is going on.
[EDIT]
Content of OpenGLView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#import "PhysicsApplicationController.h"
#interface OpenGLView : UIView {
CAEAGLLayer* _eaglLayer;
EAGLContext* _context;
GLuint _colorRenderBuffer;
GLuint _positionSlot;
GLuint _colorSlot;
PhysicsApplicationController* _physicsController;
}
typedef struct {
float position[3];
float color[4];
} Vertex;
- (void)renderFrame:(Frame*)frame;
- (void)drawObjectsInFrame:(Frame*)frame;
- (void)setupVBOsWithVertices:(Vertex*)vertices Indices:(GLubyte*)indices;
- (void)setPhysicsController:(PhysicsApplicationController*)controller;
#end