Solved

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

Posted on 2013-01-21
6
984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:binaryevo
ID: 38805510
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
ID: 38806452
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
ID: 38806972
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 33

Expert Comment

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

Author Comment

by:jbotts
ID: 38815114
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
ID: 38815780
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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