Objective-C: Use of undeclared identifier - objective-c

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

Related

Not known class method for selector OpenDevice

I'm developing an app in Objective-c for OSX.
I have write a first class and try to call this class in my MainThread.
The build succeed but the app crash
DeviceManager.h
#import <Foundation/Foundation.h>
#interface DeviceManager : NSObject
+(void)DeviceManager:OpenDevice;
#end
DeviceManager.m
#import "DeviceManager.h"
#implementation DeviceManager
- (id)init{
self = [super init];
if(self){
NSLog(#"Init");
}
return self;
}
+ (void)DeviceManager:OpenDevice {
NSLog(#"Opening Device");
}
#end
The main.m is calling it:
#import "DeviceManager.h"
int main(int argc, const char * argv[]) {
[DeviceManager OpenDevice];
return NSApplicationMain(argc, argv);
}
At build, I have Not known class method for selector OpenDevice
Thx
Seb
#import <Foundation/Foundation.h>
#interface DeviceManager : NSObject
+ (void) openDevice;
#end
DeviceManager.m
#import "DeviceManager.h"
#implementation DeviceManager
+ (void) openDevice {
NSLog(#"Opening Device");
}
#end
main.m
#import "DeviceManager.h"
int main(int argc, const char * argv[]) {
[DeviceManager openDevice];
return NSApplicationMain(argc, argv);
}
The above works for me. What does DeviceManager:OpenDevice mean? Because unless it's a new way to define methods it's gibberish. It looks like you want a class method for Device Manager called openDevice... so use + (void) openDevice {...}.

Property issue in Objective C, 'score' not found on object of type pointer

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.

Calling a Swift Method from Objective C class

I want to create an inheritance between a swift class and an objective-c class.
the swift class creates a person, the objective c class creates a student (who is also a person)
My swift person is:
import Foundation
class person {
var age: Int
var height: Float
init(){
self.age = 22
self.heigh =1.80
}
init(age:Int, height:Float){
self.age=age
self.height=height
}
}
the Objective-c student is:
student.h:
#import <Foundation/Foundation.h>
#import "Praktikum2a-Swift.h"
#interface student : person
#property (nonatomic) int studentID;
-(void)setStudentID:(int)stdID;
#end
student.m:
#import <Foundation/Foundation.h>
#import "Praktikum2a-Swift.h"
#import "student.h"
#implementation student
-(id)init{
return [self initWithStudentID:1337];
}
-(id)initWithStudentID:(int)stdID{
self=[super init];
if(self){
[self setStudentID:stdID];
}
}
#end
and the main:
#import <Foundation/Foundation.h>
#import "student.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
student *hugo = [[student alloc] init];
[hugo setAge: 22 setHeight: 1.80];
NSLog(#"Hello, World!");
}
return 0;
}
The Project and Package name is Praktikum2a
basically i can't figure out, why my student isn't a person... =/
It says on this page: https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_77
Note that you cannot subclass a Swift class in Objective-C

Use of Undeclared Identifier 'dateAge'?

I'm new to learning Objective C and I'm following some online tutorials, I seemed to be keeping up fine and then 26 videos in I'm a little confused. X-Code keeps throwing me an error for an undeclared identifier.
In Person.h I have written:
#import <Foundation/Foundation.h>
#interface Person : NSObject
-(void) dateAge:(int)a withIncome:(int)i;
#end
In Person.m I have written:
#import "Person.h"
#implementation Person
-(void) dateAge:(int)a withIncome:(int)i {NSLog(#"You can date girls %i years old and above", (dateAge/2+7) - (i/100000));}
#end
Person.m is where I am being thrown an error, I'm using the latest version of x-code and the tutorials are a year or so old, I don't know if that could be it?
main.m just says:
#import <Foundation/Foundation.h>
#import "Person.h
int main(int argc, const char * argv[])
{
#autoreleasepool {
Person *bucky = [[Person alloc]init];
[bucky dateAge:65 withIncome:300000];
}
return 0;
}
Use a instead of dateAge which is the declared variable name here,
-(void) dateAge:(int)a withIncome:(int)i {NSLog(#"You can date girls %i years old and above", (a/2+7) - (i/100000));}
{
NSLog(#"You can date girls %i years old and above", (dateAge/2+7) - (i/100000));
}
Instead of dateAge, use a which you are passing as a parameter like below :
{
NSLog(#"You can date girls %i years old and above", (a/2+7) - (i/100000));
}

expression error in Objective-C method call

I've dived into learning Objective-C and hit a little snag in when calling a method. Here's my simple code snippets:
Player.h code snippet:
#interface Player : NSObject{
}
-(void) performAction;
-(int) addNumber:(int) a toNumber:(int) b;
#end
Player.m code snippet:
#implementation Player
-(void)performAction{
NSLog(#"Here it is!");
}
-(int)addNumber:(int)a toNumber:(int)b{
return a+b;
}
#end
Calling method from main.m:
int val = [playerOne addNumber:(int)3 toNumber:(int)3];
In the above line of code, i keep getting an 'Expected expression' error.
Any ideas ?
This code works for me.
Player.h
#import <Foundation/Foundation.h>
#interface Player : NSObject
- (void)performAction;
- (int)addNumber:(int)a toNumber:(int)b;
#end
Player.m
#import "Player.h"
#implementation Player
- (void)performAction {
NSLog(#"Here it is!");
}
- (int)addNumber:(int)a toNumber:(int)b {
return a+b;
}
#end
main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "Player.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
Player *playerOne = [Player new];
int val = [playerOne addNumber:(int)3 toNumber:(int)3];
NSLog(#"%d", val);
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}