Solved

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

Posted on 2013-01-21
6
971 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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Title # Comments Views Activity
Web services working in xCode but not on phone for Enterprise distribution 1 494
app development 2 351
Can't Add UDID 9 100
Problem to scan all sheets 3 111
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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