I have a very strange problem, I have two classes the first one is a sub class of NSObject class it contains a method that add an object to its array. See the code below:
#import "portfolio.h"
#implementation portfolio
-(void) addStockObject:(stockHolding *)stock
{
[self.stocks addObject:stock ];
}
+(portfolio *) alloc
{
return [self.superclass alloc];
}
-(portfolio *) init
{
self.stocks=[[NSMutableArray alloc]init];
return self;
}
-(NSString *)getCurrentValue
{
stockHolding *stockInArray;
float currentValue=0.0;
for (NSInteger *i=0; i<[self.stocks count]; i++) {
stockInArray = [self.stocks objectAtIndex:i];
currentValue+=stockInArray.currentValue;
}
return [NSString stringWithFormat:#"Current Value: %f",currentValue];
}
#end
so when i call the method -(void) addStockObject:(stockHolding *)stock, i get the following error(during runtime):
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSObject addStockObject:]: unrecognized selector
sent to instance 0x8b48d90'
The calling code is:
p=[[portfolio alloc]init];
[p addStockObject:s];
portfolio *p;
anyone can tell me what is wrong?
the other class has a property and it seems that it can not access it during compile time.
I'm really confused.
Thank you,
missa
First, never override +(portfolio *) alloc.
Second, init methods must call another init method and you must always check self for nil before setting ivars. Apple recommends against using properties to set ivars in init methods and init methods should always return instancetype in compilers that support it or id in those that don't.
-(instancetype) init
{
self = [super init];
if (self)
{
_stocks = [[NSMutableArray alloc] init];
}
return self;
}
Related
I am a complete noob when it comes to Objective C (or even for OOP for that matter). Here is what I am trying to do
AInterface.m
#implementation AInterface
- (BOOL)getParam:(NSData **)a param1:(NSData**)param1 param2:(NSData**)param2
{
//Do a bunch of things
return bool;
}
#end
AInterface.h
#interface AInterface : NSObject
- (BOOL)getParam:(NSData **)a param1:(NSData**)param1 param2:(NSData**)param2;
+ (instancetype) inst;
#end
testMain.m()
int main()
{
Bool result = NO;
NSData *a = Nil;
NSData *b = Nil;
NSData *c = Nil;
result = [[AInterface inst] getParam:(NSData **)&a param1:(NSData**)&a param2:(NSData**)&b];
return result
}
When I run this though, I get an error saying failed:
caught "NSInvalidArgumentException", "+[AInterface inst]: unrecognized selector sent to class
Although you declared +inst method in #interface section your class does not have it implemented and that leads to runtime error. You need to add implementation to make it work, e.g.
#implementation AInterface
...
+ (instancetype)inst {
return [self new];
}
Your problem is that you don't have implementation of +inst in AInterface.m.
In your case inst would be something like:
[[AInterface alloc] init] but I'd just use [[AInterface alloc] init] instead of calling inst in the first place. Or [AInterface new], which stands for the same.
In general, the rest of your code is not idiomatic Objective-C.
I am newbie to Objective-C. I have a 'XYZPerson' Class with attributes {firstName, lastName, dateOfBirth} and I want when I write "XYZPerson *person=[[XYZPerson alloc] init]" in main, it should call my overridden 'init' method which should in-turn call my designated initializer and initializes my object with the defined values.
My Code snippets. http://pastebin.com/FfxNDDhf
#import <Foundation/Foundation.h>
#import "XYZShoutingPerson.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
XYZPerson *person=[[XYZPerson alloc] init];
if(person) {
[person sayHello];
}
else {
NSLog(#"Person Object is NULL");
}
}
return 0;
}
-(id)init
{
self=[super init];
return [self initWithFirstName:#"Ankit" LastName:#"Sehra" DOB:01/01/2000];
}
-(id)initWithFirstName:(NSString *)aFirstName LastName:(NSString *)aLastName DOB:(NSDate *)aDateOfBirth
{
_firstName=aFirstName;
_lastName=aLastName;
_dateOfBirth=aDateOfBirth;
}
-(void)sayHello;
{
NSLog(#"%# %# %#",self.firstName,self.lastName,self.dateOfBirth);
}
Write now the output of the program is "Person Object is NULL", I want it to print the firstName, lastName and DOB.
If a class has several init methods, one of them is the "designated initializer".
This is the one that calls [super initXXX]. In your example, initWithFirstName:LastName:DOB: would be the designated initializer and should look like
this:
-(id)initWithFirstName:(NSString *)aFirstName LastName:(NSString *)aLastName DOB:(NSString *)aDateOfBirth
{
self = [super init];
if (self) {
_firstName=aFirstName;
_lastName=aLastName;
_dateOfBirth=aDateOfBirth;
}
return self;
}
(I have changed the type of the dateOfBirth property and the DOB argument to NSString and will explain that later.)
All other init methods just call the designated initializer (and not [super initXXX]), in your example init:
-(id)init
{
return [self initWithFirstName:#"Ankit" LastName:#"Sehra" DOB:#"01/01/2000"];
}
Note that (among several other errors), "01/01/2000" is not a NSDate, so
return [self initWithFirstName:#"Ankit" LastName:#"Sehra" DOB:01/01/2000];
does not make any sense. Therefore, to get a first working example for you, I have
changed the argument type to NSString.
I'd bet the compiler is giving you plenty of warnings on that code. You should fix them. If it isn't, your compiler isn't tuned correctly.
First, your designated initializer doesn't return anything.
Secondly, there is no reason to override init and call your designated initializer. Simply do:
foo = [[Person alloc] initWithFirstName:#"bob" lastName:#"dobbs"];
Hi i want to implement my own Objects to manage my data, i was trying to make a two classes.
Class Continents that contains a Continent Objects
Here is my implementation:
#implementation OsContinents
#synthesize continentes;
-(id)init{
return [super init];
}
-(NSUInteger)count{
NSLog(#"%u",[continentes count]);
return [continentes count];
}
-(void)add:(OsContinent *)continente{
[continentes addObject:continente];
}
-(OsContinent *)getElementByIndex:(NSUInteger)index{
return [continentes objectAtIndex:index];
}
-(void)deleteContinentByIndex:(NSUInteger)index{
return [continentes removeObjectAtIndex:index];
}
-(void)deleteContinent:(OsContinent *)objContinent{
return [continentes removeObject:objContinent];
}
-(NSMutableArray *)getAll{
return continentes;
}
#end
Next i want to populate *continents Property with "Continent" Objects like this.
OsContinents *continentesCollection = [[OsContinents alloc] init];
for (NSString *strContinente in [data allKeys]) {
OsContinent *con = [[OsContinent alloc] init];
[con setContinente:strContinente];
NSLog(#"%#",[con getContinente]);
[continentesCollection add:con];
}
NSLog(#"%u",[continentesCollection count]);
But allways got ZERO in de count Method.
Note: NSLog(#"%#",[con getContinente]) print de data OK, the Continent Object is OK, the problem is in the "*continentes" inside the Continents Object-
Any Clue?
Your initializer does nothing but initialize the superclass. Use it to set up your own class:
- (id)init
{
self = [super init];
if (self)
{
_continentes = [[NSMutableArray alloc] init];
}
return self;
}
Otherwise, continentes will remain nil. Messaging nil is valid: methods simply don't do anything, and return 0.
If you want to completely hide the underlying mutable array (which is perfectly fine) then don't advertise it in your .h file as a property. Instead, at the beginning of your #implementation, declare a semi-private instance variable:
#implementation OsContinents
{
NSMutableArray *_continentes;
}
I say "semi-private" because you can always use the runtime engine to introspect objects. But it'll be hidden from normal use. If you ever subclass your object, you can always move the instance variable declaration from your #implementation to your #interface so that subclasses can get at it.
I have a class called "CardSet", containing an NSMutableArray* cardSet to hold "cards", which I extend to make "DeckCards". I'd like "CardSet" to have a method called "(void)addCard:(Card*)" (and similarly a method "removeCard"). I'd like "addCard" to some how have access to and set cardSet. Even better I'd like to use the "addCard" method to initialise cardSet. The class file "CardSet.h" reads:
#import < Cocoa/Cocoa.h >
#import < Card.h >
#interface CardSet : NSObject {
NSMutableArray* cardSet;
}
-(id)init;
-(NSMutableArray*)getCardSet;
-(void)setCardSet:(NSMutableArray *)new_cardset;
-(Card*)getCard:(NSInteger) index;
**-(void)addCard:(Card*) new_card;**
-(void)removeCard:(Card*) old_card;
-(void)dealloc;
#property (readwrite, retain, getter=getCardSet, setter=setCardSet) NSMutableArray* cardSet;
#end
and the method file reads:
#import "CardSet.h"
#implementation CardSet
-(id)init{
if( self = [super init] ){} //will add initialisations here later
return self;
}
-(NSMutableArray*)getCardSet{
return cardSet;
}
-(void)setCardSet:(NSMutableArray *)new_cardSet{
cardSet = new_cardSet;
}
-(Card*)getCard:(NSInteger)index{
return [cardSet objectAtIndex:index];
}
**-(void)addCard:(Card *)new_card{
[cardSet addObject:new_card];
}**
-(void)removeCard:(Card *)old_card{
[cardSet removeObject:old_card];
}
-(void)dealloc{
[cardSet release];
[super dealloc];
}
#synthesize cardSet;
#end
This compiles just fine. I'd like to initialise a "DeckCards" instance using its "addCard" method 52 times. When I call addCard 52 times in a DeckCards setter method, and ask for the size of its "cardSet", I'm returned 0.
This appears to be a scope or privileges problem? Can the "addCard" method have any setter privileges? Must a setter argument be the same as the return and respective member type?
[I can work around the above by creating an NSMutableArray object "deck_cards_temp" outside of "DeckCard", add 52 cards to this, and pass it to set the member of my "DeckCards" instance via the setter inherited from "CardSet". This is not very satisfactory!]
What do you advise? Many thanks in advance for your help and patience.
You are never actually creating the cardSet object. You should be creating it in your -init method:
-(id)init
{
if( self = [super init] )
{
cardSet = [[NSMutableArray alloc] init];
}
return self;
}
Because you never actually create the array, all the calls to -addCard: are being sent to a nil object.
When you pass in an array to -setCardSet:, you are passing in an initialized array so the array is no longer nil and the -addCard: calls work fine.
CardSet.h
#import <Cocoa/Cocoa.h>
// For know we just need to know there is a class named "Card" being used but implemented later
#class Card;
#interface CardSet : NSObject {
NSMutableArray *cardSet;
}
// Here are the methods according to "correct" naming conventions
- (Card *)cardAtIndex:(NSInteger)index;
- (void)addCard:(Card *)card;
- (void)removeCard:(Card *)card;
// This will help us and forget about writing the setter/getter
#property (nonatomic, retain) NSMutableArray *cardSet;
#end
CardSet.m
#import "CardSet.h"
// Now we tell the compiler what "Card" is and what methods etc. it has
#import "Card.h"
#implementation CardSet
#synthesize cardSet;
- (id)init {
if (self = [super init]) {
// If we don't create the cardSet, how are we able to work with it!?
NSMutableArray *anArray = [[NSMutableArray alloc] init];
self.cardSet = anArray;
[anArray release];
}
return self;
}
- (Card *)cardAtIndex:(NSInteger)index {
return [cardSet objectAtIndex:index];
}
- (void)addCard:(Card *)card {
[cardSet addObject:card];
}
- (void)removeCard:(Card *)card {
[cardSet removeObject:card];
}
- (void)dealloc {
[cardSet release];
[super dealloc];
}
#end
As Abizern already noted: Naming the array the same as your class is a bad thing.
I would shorten that init method:
- (id)init {
if (self = [super init]) {
// If we don't create the cardSet, how are we able to work with it!?
self.cardSet = [NSMutableArray array];
}
return self;
}
I've got this code:
Entry.h
#import <Foundation/Foundation.h>
#interface Entry : NSObject {
id object;
SEL function;
}
#property (retain) id object;
#property (assign) SEL function;
-(Entry*) initWithObject:(id)object selector:(SEL)function;
#end
Entry.m
#import "Entry.h"
#implementation Entry
#synthesize object;
#synthesize function;
-(Entry*) initWithObject:(id)obj selector:(SEL)sel {
self = [super init];
[self setObject:obj];
[self setFunction:sel];
return self;
}
-(void) dealloc {
[super dealloc];
if ([self object] != nil)
[[self object] release];
}
#end
And when I do this:
Entry *hej = [Entry alloc];
[hej release];
I get:
objc[2504]: FREED(id): message object sent to freed object=0xf5ecd0
Program received signal: “EXC_BAD_INSTRUCTION”.
What am I doing wrong?
(And this insert code thing at stack overflow doesnt work, unless I'm doing something wrong and you're not supposed to click "code sample" and then paste.)
+alloc only allocates memory. You need -init to actually create the object in that memory space. Since you are only allocating memory and not creating an object there, calling -release on a chunk of memory is giving you an error. Further, you want your [super dealloc] call to appear at the end of you -dealloc method. Change those two things and the following should work:
Entry *hej = [[Entry alloc] init];
[hej release];
there are two problems here:
1) you need to check that self = [super init] does not return nil. Typical usage would be to follow wrap your initialization code with the conditional:
if ((self = [super init]) != nil) {
// initialize the object...
}
2) but where you are getting stuck is on instantiating your object: you should do it like this:
Entry *hej = [[Entry alloc] initWithObject:myObj selector:mySelector];
(assuming that you want to go through the custom initializer you just defined...
else just use the default init method.) but 'alloc' must be followed by an init.
Entry *hej = [[Entry alloc] init]; // will do the trick...
Firstly, you need an init to go with your alloc. Second, in dealloc, you send a message to self after calling [super dealloc]. You can't do that. The final deallocation should go at the end.
I would also recommend changing:
if ([self object] != nil)
[[self object] release];
to:
[self setObject:nil];
It's less code and functionally equivalent. =)
There are many things wrong with your code. I'll try to go through them.
First, its better to use a different ivar name to your property name so its clear where you are using each. Apple normally uses an underscore prefix, but any prefix will do.
#interface Entry : NSObject {
id _object;
SEL _function;
}
#property (retain) id object;
#property (assign) SEL function;
#synthesize object = _object;
#synthesize function = _function;
Next, you aren't using the standard init template (although this probably wont make any difference normally).
-(Entry*) initWithObject:(id)obj selector:(SEL)sel {
self = [super init];
if (self != nil) {
// initializations
}
return self;
}
Next, Apple (for good reasons) recommends against using getters/setters in your init/dealloc. So your init would be:
-(Entry*) initWithObject:(id)obj selector:(SEL)sel {
self = [super init];
if (self != nil) {
_object = [obj retain];
_object = sel;
}
return self;
}
Next, after [super dealloc] your object is destroyed, so you cannot reference self (and hence your ivars) after that, so your dealloc should look like:
-(void) dealloc {
// your deallocations
[super dealloc];
}
Further, as above, Apple recommends you should not use setters or getters in your dealloc routine, so your deallocation would initially look like:
if (_object != nil)
[_object release];
But further still, Objective C allows (and Cocoa encourages) that sending a method to nil does nothing. This is in stark contast to most other languages where messaging nil would cause a crash, but it is how Objective C/Cocoa work and you need to get used to it. So your deallocation is actually just:
[_object release];
And finally, alloc only allocates the memory for your object, you have to initialize it, so the initialization would be something like:
Entry *hej = [[Entry alloc] initWithObject:myobj selector:#selector(mymethod)];