Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-01-21
6
Medium Priority
?
1,031 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
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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

963 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