Solved

Objective C - (id) type not accepting (int)

Posted on 2013-01-21
6
938 Views
Last Modified: 2016-02-29
Learning Objective C. Attempting to create an initialization method:

//Code from Fraction.m
#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(id) init
{
       return [self initWith: 0 over: 0];
}

-(Fraction *) initWith: (int) n over: (int) d
{
    self = [super init];
   
    if (self)
    {
        [self setTo: n over: d];
    }
   
    return self;    
}

Error Message before compiling:

//From main.m

Fraction *a, *b;

a = [[Fraction alloc] initWith: 7 over: 17];       //This line causes the Error Message below.

Error Message: "Implicit conversion of 'int' to 'id' is disallowed with ARC.
0
Comment
Question by:jbotts
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:binaryevo
Comment Utility
The issue is simple, init methods need to return self.  Your not doing that in your initwith method signature, your returning Fraction *.  The fraction * is a pointer in memory.  Change -(fraction *) to -(id) and see if that works for you.  Hope this helps!
0
 

Author Comment

by:jbotts
Comment Utility
I changed the return type to (id) but got the same result. The only way I have been able to make this work is by not using the ARC. Thanks for the response.

Jim
0
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
Comment Utility
Well not using ARC isnt an option, we will see if we can get your code to work... :-)  Is the initWith exposed in your interface / .h file?

I reproduced your issue and looked at where i was doing similar init methods and this is how im achieving the same thing:

Header / .h
@interface MenuModel : NSObject

@property (nonatomic, retain) NSMutableDictionary *menuItems;
@property (nonatomic, retain) NSMutableArray *keys;
@property (nonatomic, retain) NSMutableArray *menuSectionImages;

-(id)initWithIntegers:(int)n over:(int)d;    // Your Init Code

@end  

Open in new window



Implementation / .m
#import "MenuModel.h"

@interface MenuModel()



@end

Open in new window


@implementation MenuModel

-(id)init
{
    return self;
}

-(id)initWithIntegers:(int)n over:(int)d
{
    return self;
}

@end

Open in new window


Caller / Instantiator

MenuModel *model = [[MenuModel alloc] initWithIntegers:7 over:17];

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
I think, you forgot to add #import "Fraction.h" in main.m file.
0
 

Author Comment

by:jbotts
Comment Utility
Binaryevo, thanks for the response. Sorry I am so late in responding. I was traveling. I have in the Fraction.h file the following code:

-(id) init;
-(id) initWith (int) n: over (int) d;

I changed the implementation code but still get the same message as mentioned in the question.

pgnatyuk: I have the #import Fraction.h in the main file.


Thanks...
0
 

Author Comment

by:jbotts
Comment Utility
After double check of the code, I found the error:
In the header file:

-(id) initWith: (int) n over: d;

Should have been:

-(id) initWith: (int) n over: (int) d;
Thanks so much for the help.

JSB
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now