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 {...}.
Related
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
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
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]));
}
}
I subclassed NSFontManager and overrode "modifyFont:(id)sender)
Then I changed the NSFontManager class in my xib files to the new class.
I can see, that the class is initialized, but the overwritten method is never called. Though the NSFontManager method works normal.
What do I wrong?
#import "GFFontManager.h"
#implementation GFFontManager
-(id)init{
if (self = [super init]) {
//this is called
NSLog(#"GFFontManager init");
}
return self;
}
-(void)modifyFont:(id)sender{
//this is never called
NSLog(#"Do something");
[super modifyFont:sender];
}
#end
OK - here is how it works:
I added the following to the main.c and it worked like a charm!
#import <Cocoa/Cocoa.h>
#import "GFFontManager.h"
int main(int argc, char *argv[])
{
[NSFontManager setFontManagerFactory: [GFFontManager class]];
return NSApplicationMain(argc, (const char **) argv);
}
Best regards - Gerald