Calling a Swift Method from Objective C class - objective-c

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

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.

Objective-C: Use of undeclared identifier

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

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]));
}
}

Objective-c: Now allowing me to create an instance of a class

I just started programming in objective-c(today) and I'm just doing some simple command line examples but I've already run into something that has stumped me. I am getting a compiler error on lines 5 & 6 in main. Here is my code:
Fraction Interface:
#import <Foundation/Foundation.h>
#interface Fraction : NSObject
{
int numerator, denominator;
}
-(void)print;
-(void)setDenominator;
-(void)setNumerator;
#end
Fraction implementation:
#import "Fraction.h"
#implementation Fraction
-(void)print {
NSLog(#"%i/%i",numerator,denominator);
}
-(void)setNumerator: (int) n {
numerator = n;
}
-(void)setDenominator: (int) d {
denominator = d;
}
#end
Main:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
#autoreleasepool {
Fraction *f1 = [[Fraction alloc] init];
Fraction *f2 = [[Fraction alloc] init];
}
return 0;
}
All 3 files are in the same folder. I'm using Xcode; if this is relevant information.
Looks like you forgot to add #import "Fraction.h" to the top of main.m, In the future adding the error you receive would be helpful.