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] .
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 a new object, one of the property is (readonly).
Here is my code:
1/ My class (.h + .m)
#import <Foundation/Foundation.h>
#interface MyClass : NSObject
#property (readonly) NSString* myProperty;
#end
#import "MyClass.h"
#implementation MyClass
#end
2/ My class extension (.h)
#import "MyClass.h"
#interface MyClass ()
#property (readwrite) NSString* myProperty;
#end
3/ MyClassCategory (.h + .m)
#import "MyClass.h"
#interface MyClass (MyClassCategory)
+ (MyClass*) creatNewObject:(NSString*) mypro;
#end
#import "MyClass+MyClassCategory.h"
#import "MyClass_MyClassExtension.h"
#implementation MyClass (MyClassCategory)
+ (MyClass*) creatNewObject:(NSString*) mypro {
MyClass* newObject = [[MyClass alloc] init];
newObject.myProperty = mypro;
NSLog(#"New object have been created%#", newObject);
return newObject;
}
#end
#import <Foundation/Foundation.h>
#import "MyClass.h"
#import "MyClass+MyClassCategory.h"
int main(int argc, const char * argv[]) {
#autoreleasepool {
// insert code here...
NSLog(#"Hello, World!");
MyClass* newObject1 = [MyClass creatNewObject:(#"I am the new object!")];
NSLog(#"New object have been created%#", newObject1);
}
return 0;
}
after running the code in the main method, I got
2014-09-25 21:53:52.204 MyClass[2811:303] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyClass setMyProperty:]: unrecognized selector sent to instance 0x100109e10'
014-09-25 22:36:05.746 MyClass[2989:303] -[MyClass setMyProperty:]: unrecognized selector sent to instance 0x100200130
I want to be enables edits to the properties of an existing class instance from category.
Can you help me please?
Thank you.
In MyClass.m, you also need to include the extension header.
#import "MyClass_MyClassExtension.h"
Otherwise the compiler won't see myProperty is redeclared as a read-write property while generating the implementation of MyClass.
I am really new to Objective-C and when I was practicing the book exercises, I really am stuck here. Please help me solve this and I have been thinking what could cause this error for more than three hours. Still I didn't get it!
Best regards,
Raj.
Thanks in advance !
main.m
#import <Foundation/Foundation.h>
#import "XYZPerson.h"
#import "XYZShout.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
//XYZPerson *some = [[XYZPerson alloc]init];
XYZShout *some = [[XYZShout alloc]init];
[some sayHello];
// insert code here...
// NSLog(#"Hello, World!");
}
return 0;
}
XYZPerson.h
#import <Foundation/Foundation.h>
#interface XYZPerson : NSObject
#property NSString *firstName;
#property NSString *secondName;
#property NSDate *dob;
-(void) saySomething;
-(void) sayHello;
#end
XYZPerson.m
#import "XYZPerson.h"
#implementation XYZPerson
-(void) sayHello {
[self saySomething:#"Hello all"];
}
-(void) saySomething:(NSString *)greet {
NSLog(#"%#", greet);
}
#end
XYZShout.h
#import "XYZPerson.h"
#interface XYZShout : XYZPerson
// -(void) saySomething;
#end
XYZShout.m
#import "XYZShout.h"
#implementation XYZShout
-(void) saySomething:(NSString *)greet {
NSString *upperGreet = [greet uppercaseString];
[super saySomething:upperGreet]; // this is where I get the error mentioned above
}
#end
Got it working ! Thanks to #MatthewD , #trojanfoe , #JFS for your big help :)
It looks like you are testing inheritance so I will assume that XYZShout is supposed to be derived from XYZPerson. If so follow the suggestion from #JFS and make sure it does actually derive:
XYZShout.h:
#import <Foundation/Foundation.h>
#import "XYZPerson.h"
#interface XYZShout : XYZPerson
- (void)saySomething:(NSString *)greet;
#end
And also correct the definition of saySomething in XYZPerson (you missed off the parameter):
XYZPerson.h:
#import <Foundation/Foundation.h>
#interface XYZPerson : NSObject
#property NSString *firstName;
#property NSString *secondName;
#property NSDate *dob;
- (void)saySomething:(NSString *)greet;
// ^^^^^^^^^^^^^^^^^
- (void)sayHello;
#end
(Moved from comments into an answer...)
MatthewD: What happens if you change - (void) saySomething; in XYZPerson.h to - (void) saySomething:greet;?
Raj0689: Why does it run when I change it to saySomething:greet and not saySomething ? Since greet is defined only along with saySomething !!
When you call a method, the compiler needs to locate the signature of that method so it can verify that the method is being called correctly. The signature includes the method name and the number and types of parameters. The usual way of providing method signatures is by importing the header file that defines those signatures.
So, in XYZShout.m where you call:
[super saySomething:upperGreet];
The compiler searches XYZShout.h, which is imported by XYZShout.m, and XYZPerson.h, which is imported by XYZShout.h. In XYZShout.h, the following method was being found:
-(void) saySomething;
This matches the called method in name, but not in parameters, so the compiler does not consider this a match. No other definitions of saySomething are found anywhere, so it gives an error instead.
Please make sure to set the XYZShout.h interface to #interface XYZShout : XYZPerson?
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]));
}
}