No known class method for selector 'initWith...' - objective-c

I found similar questions, but I couldn't solve my error. My error is:
No known class method for selector 'initWithUrl:sub:cont:cat:dat'
I've tried
#Synthesize,
self.variableName instead of _variableName,
adding [[MyClass init] alloc],
changing - to +
How can I fix it?
MyClass.h:
#import <Foundation/Foundation.h>
#interface MyClass : NSObject
-(id)init;
-(id)initWithURL:(NSURL *)url_ sub:(NSString *)subject_ cont:(NSString *)content_ cat:(NSString *)category_ dat:(NSString *)date_;
#property NSURL *bannerImageURL;
#property NSString *subject;
#property NSString *content;
#property NSString *category;
#property NSString *date;
#end
MyClass.m:
#import "MyClass.h"
#implementation MyClass
-(id)init {
self = [super init];
if (self) {
_bannerImageURL = [NSURL URLWithString:#"url0"];
_subject = #"sub0";
_content = #"cont0";
_category = #"cat0";
_date = #"dat0";
}
return self;
}
-(id)initWithURL:(NSURL *)url_ sub:(NSString *)subject_ cont:(NSString *)content_ cat:(NSString *)category_ dat:(NSString *)date_ {
self = [super init];
if (self) {
_bannerImageURL = url_;
_subject = subject_;
_content = content_;
_category = category_;
_date = date_;
}
return self;
}
#end
myViewController.m:
#import "myViewController.h"
#import "MyClass.h"
#implementation SimpleTableViewController
MyClass *news1;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *aUrl = [NSURL URLWithString:#"aUrl"];
// Error occurs here.
news1 = [MyClass initWithURL:aUrl sub:#"aSub" cont:#"aCont" cat:#"aCat" dat:#"aDat"];
}

I think you mean:
news1 = [[MyClass alloc] initWithURL:aUrl sub:#"aSub" cont:#"aCont" cat:#"aCat" dat:#"aDat"];
This is a standard pattern in Objective-C: call [SomeClass alloc] to create a new instance, then immediately call some initializer method on it. Initializers are instance methods which must be called on an instance of a class, whereas alloc is a class method that is called on the class itself (and which returns a newly allocated instance of that class).

Related

Objective-c : cannot init a class object

I am getting an issue cannot init a class object even after adding lines to the constructor such as
self = [super init];
or
self = [[super init] alloc];
And I am not sure what to do.
This is the specific error:
file:///%3Cunknown%3E: test failure: -[LinkedListTest testAdd] failed: *** +[NList<0x8e14> init]: cannot init a class object.
.m
#interface NList()
#property (weak, nonatomic, readwrite) NSObject *head;
#property (nonatomic,readwrite) NSInteger *size;
#end
#implementation NList
#synthesize size = _size;
- (id) init:(NSInteger *)size {
//is this even necessary? I don't want object methods.. or do I ?
if (self){
_head = nil;
_size = size;
}
return self;
}
.h
#interface NList : NSObject
#property (nonatomic,readonly) NSInteger *size;
#property (weak, readonly, nonatomic) NSObject *head;
- (void)add:(NSObject *)node;
#end
test class
- (void)testAdd
{
NList *testList = [[NList init] alloc];
// Card *testCardOne = [[Card init] alloc];
// [testList add:(testCardOne)];
XCTAssertNotNil(testList.head);
}
I have tried adding the line
self = [[super init] alloc];
to the constructor to no avail.
No visible interfacce for nlist declares
or self = [super init]
complains cannot init a class object!
EDIT
I realized that it is not asking me for the size! the constructor requires a size parameter...how do I do this! Ahh [looks up docs]
A few things.
You need a default constructor
- (id)init {
self = [super init];
if (self) {
self.head = nil;
}
return self;
}
Now that you have a default constructor (that calls the superclasses constructor), you need a more specific one for your purposes.
- (id)initWithSize:(int)size {
self = [self init]; // sets head, calls super constructor.
if (self) {
self.size = size;
}
return self;
}
Edit: Note, the last one had to be in your .h file so it is visible.
And also, when instantiating this class, call
NList *list = [[NList alloc] initWithSize:mySize];
You're a little backwards.
How about:
NList *testList = [[NList alloc] init:SIZE];
where size is the SIZE init you want to use.
Alloc comes before init when you're instantiating Objective-C objects.

Retrieving custom class object properties from NSMutableArray

//controller header file.
#interface MasterVaultDataViewController : UITableViewController
#property(nonatomic,strong) NSMutableArray *sectionTittle;
#end
//controller m class
#import "MasterVaultDataViewController.h"
#import "PasswordKeeper.h"
#interface MasterVaultDataViewController (){
PasswordKeeper *cathegory1,*cathegory2,*cathegory3;
UIImage *image2;
}
#end
#implementation MasterVaultDataViewController
-(NSMutableArray *) sectionTittle{
if(!_sectionTittle){
_sectionTittle = [[NSMutableArray alloc] init];
}
return _sectionTittle;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
cathegory1 = [[PasswordKeeper alloc]init];
[cathegory1 setTitle:#"Concepcion"];
[cathegory1 setSubtitle:#"Parte Del Sur"];
cathegory2 = [[PasswordKeeper alloc]init];
[cathegory2 setTitle:#"Santiago"];
[cathegory2 setSubtitle:#"Norte"];
cathegory3 = [[PasswordKeeper alloc]init];
[cathegory3 setTitle:#"Bariloche"];
[cathegory3 setSubtitle:#"Argentine"];
[_sectionTittle addObject:[cathegory1 class]];
//Custom Class h file
#interface PasswordKeeper : NSObject
#property(nonatomic,strong) NSString *title;
#property(nonatomic,strong) NSString *subtitle;
#end
//custom class m file
#import "PasswordKeeper.h"
#implementation PasswordKeeper
#end
I have created 3 objects from my PasswordKeeper class and added them to a
NSMutableArray property in my controller. i have first set up the properties of each object.
Now i want to retrieve the properties of the objects. Here is some code.

How to create a property in objective c that's also a custom made class?

Does anyone know how to add a property in objective c - but that property is also a custom made class?
For instance, I made this class:
#interface Person : NSObject
#property NSString *personName;
#property NSNumber *personAge;
-(id)init;
#end
Where...
#implementation Person
#synthesize personAge, personName;
-(id)init{
self = [super init];
if(self){
self.personAge = [NSNumber numberWithInt:26];
self.personName = #"Jamie";
}
return self;
}
#end
So basically whenever I init & alloc the Person class, it gets setup with personAge as 26 and personName as Jamie.
Now I want to create a bank account class which contains a person property:
#interface BankAccount : NSObject
#property NSNumber *bankAccNumber;
#property (nonatomic) Person *thePerson;
-(id)init;
#end
Where...
#implementation BankAccount
#synthesize thePerson = _thePerson;
#synthesize bankAccNumber;
-(id)init{
self = [super init];
if(self){
bankAccNumber = [NSNumber numberWithInt:999];
}
return self;
}
#end
Now - my issue is this:
1) In the BankAccount class, where do I alloc & init the Person class?
To stay with your example
#implementation BankAccount
#synthesize thePerson = _thePerson;
#synthesize bankAccNumber;
-(id)init{
self = [super init];
if(self){
bankAccNumber = [NSNumber numberWithInt:999];
thePerson = [[Person alloc] init]; // <- **** HERE
}
return self;
}
#end
Of course, in general, you do not want everybody to be 26yo Jamies and have 999$, but I guess you plan to improve on these details later :)

How do I Call a method from other Class

I'm having some trouble figuring out to call methods that I have in other classes
#import "myNewClass.h"
#import "MainViewController.h"
#implementation MainViewController
#synthesize txtUsername;
#synthesize txtPassword;
#synthesize lblUserMessage;
- (IBAction)calculateSecret {
NSString *usec = [self calculateSecretForUser:txtUsername.text
withPassword:txtPassword.text];
[lblUserMessage setText:usec];
[usec release];
}
...
myNewClass.h
#import <Foundation/Foundation.h>
#interface myNewClass : NSObject {
}
- (NSString*)CalculateSecretForUser:(NSString *)user withPassword:(NSString *)pwd;
#end
myNewClass.m
#import "myNewClass.h"
#implementation myNewClass
- (NSString*)CalculateSecretForUser:(NSString *)user withPassword:(NSString *)pwd
{
NSString *a = [[NSString alloc] initWithFormat:#"%# -> %#", user, pwd];
return a;
}
#end
the method CalculateSecretForUser always says
'MainViewController' may not respond to '-calculateSecretForUser:withPassword:'
what am I doing wrong here?
The keyword "self" means the instance of your current class. So you are sending the message calculateSecretForUser:withPassword to MainViewController which does not implements it. You should instantiate myNewClass and call it :
- (IBAction)calculateSecret {
myNewClass *calculator = [[myNewClass alloc] init];
NSString *usec = [calculator calculateSecretForUser:txtUsername.text
withPassword:txtPassword.text];
[lblUserMessage setText:usec];
[usec release];
[calculator release];
}

Objective C one class not seeing the other

I have these two classes MobRec & MobDef and I want to reference an array pointer of (MobDef) *mobInfo from MobRec. #import the mobdefs.h file in MobRec.h or .m but no luck any ideas?
MobRec.h
// Basic class unit
#interface MobRec : NSObject {
NSString *mName;
int speed;
}
#end
MobDef.h
// Master Class holding an array of units
#interface MobDef : NSObject {
NSMutableArray *mobInfo;
}
#property(retain) NSMutableArray *mobInfo;
#end
MobDef.m
#synthesize MobInfo;
- (id)init { // to add a new node and initialize it
mobInfo = [[NSMutableArray alloc] init];
MobRec *aNewMobRec = [[MobRec alloc] init];
[mobInfo addObject:aNewMobRec];
[aNewMobRec release];
}
The problem is that individual files have no knowledge of other files in your project, since Objective-C is a derivative of C. You need to #import the header files of any other classes that you need to use:
// ModDef.m
#import "MobDef.h"
#import "ModRec.h"
#implementation MobDef
#synthesize mobInfo; // case matters here
- (id)init
{
mobInfo = [[NSMutableArray alloc] init];
MobRec* aNewMobRec = [[MobRec alloc] init];
[mobInfo addObject:aNewMobRec];
[aNewMobRec release];
}
#end
Well, your #synthesize doesn't match #property declaration - you're declaring properties for mobInfo but generating synthesized accessors for MobInfo.
Of are you getting some other error?