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.
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 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
The following code give me an error of "no visible #interface for BankAccount declares the selector getAccountNumber"
Why? Please help
// BankAccount.h
#import <Foundation/Foundation.h>
#interface BankAccount: NSObject
#property long accountNumber;
#end
// BankAccount.m
#import "BankAccount.h"
#implementation BankAccount
#end
//main.m
#import <Foundation/Foundation.h>
#import "BankAccount.h"
int main (int argc, const char * argv[]) {
BankAccount *account1 = [[BankAccount alloc] init];
[account1 setAccountNumber: 34543212];
NSLog(#" Account No = %li", [account1 getAccountNumber]);
return 0;
}
The generated getter is accountNumber, not getAccountNumber.
Change [account1 getAccountNumber] to [account1 accountNumber] .
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]));
}
}