Solved

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

Posted on 2013-01-21
6
988 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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

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