In Xcode how to suppress "incomplete implementation"?(inheritance) - iphone

I have a method which is implemented in its super method. And when I run my program. It works fine. How do I suppress this warning? By the way, both methods are in '.m' file.
The code is like this:
ClassA.m
#interface ClassA()
- (void)method;
#end
ClassA.h
#interface ClassA : ClassB
#end
ClassB.m
#interface ClassB()
#end
#implementation ClassB
- (void)method
{
}
And it works OK when I call method in ClassA

You could always implement the method anyway, and call the super method -
-(void)doMyThing {
[super doMyThing];
// do nothing
}

You've got it completly wrong. Popular construction is to have:
ClassB.h
#interface ClassB
- (void)method;
#end
ClassB.m
#interface ClassB()
#end
#implementation ClassB
- (void)method
{
//do nothing or even assert false
}
#end
ClassA.h
#interface ClassA : ClassB
#end
ClassA.m
#interface ClassA()
#end
#implementation ClassA
- (void)method
{
//do your stuff here
}
#end
And that's called method overriding. But if I correctly understand what you are trying to achieve you should have:
ClassB.h
#interface ClassB
- (void)method;
#end
ClassB.m
#interface ClassB()
#end
#implementation ClassB
- (void)method
{
//do your stuff here
}
#end
ClassA.h
#interface ClassA : ClassB
#end
And there is no need to mess up with ClassA, and you can call
ClassA *o=[[ClassA alloc ] init];
[o method];
Then the method is available in classA thanks to inheritance from ClassB.

Sounds like you've declared the method in the .h file when you shouldn't have? Or perhaps the #end is missing from the .h file.
You aren't giving us much to go on. Can we see your full code from the .h and .m?
Having read your code sample, try the following:
ClassA.h
#interface ClassA : ClassB
//ClassB inherits from Class A, so we define 'method' in Class B
//ClassA knows about 'method' by inheritance from class B,
//so 'method' doesn't need to be defined here.
//doStuff will be the method that calls 'method'
- (void)doStuff;
#end
ClassA.m
#interface ClassA
#end
#implementation ClassA
- (void)doStuff
{
//Call 'Method'
[self method];
}
#end
ClassB.h
#interface ClassB
//Define 'method' which will be inherited by ClassA
- (void)method;
#end
ClassB.m
#interface ClassB
#end
#implementation ClassB
- (void)method
{
//'method' does it's thing
NSLog(#"Method has been Called");
}
#end

Related

wrong delegate declaration

Lets assume I have a class ClassA
header:
#interface ClassA : NSObject
- (id)initWithDelegate:(id)delegate;
#end
implementation:
#interface ClassA ()
{
NSObject *_delegate;
}
#end
#implementation
- (id)initWithDelegate:(id)delegate
{
self = [super init];
if( self )
{
_delegate = delegate;
}
return self;
}
#end
Since I skipped the #property definition my delegate declaration defaults to strong? So this code is wrong because it will cause a leak, or does my delagate declaration default to weak?
You do have to qualify that ivar as __weak. The default for an ivar is strong.
Otherwise, as you already know, you risk a retain cycle with delegates.
BTW, the convention here is 'id' rather than NSObject *.
It’s best to write the code in a way that makes this explicit:
#interface ClassA : NSObject
#property(weak) id delegate; // or #property(weak, readonly)
#end
#implementation ClassA
- (id)initWithDelegate: (id) delegate
{
self = [super init];
_delegate = delegate;
return self;
}
#end
By default, you instance variable would be strong.
You are correct, this will cause ARC to retain the _delegate, ie make a "strong" reference. If you want to create a weak reference without declaring a #property, you can use
#interface ClassA ()
{
__weak id _delegate;
}
#end

How to access first class method from second class in objective c

I have written two classes which contains same method (print). I want to access first class print method using second class object. How i can achieve this?
Code:
#interface classA : NSObject
-(void) print;
#end
#implementation classA
-(void) print
{
NSLog(#"hello");
}
#end
#interface classB : classA
-(void) print;
#end
#implementation classB
-(void) print{
NSLog(#"hey");
}
#end
Now i created second class object like
classB *B = [classB alloc]init];
use delegates to access other classes
#protocol
you can do like this way also
#implementation view1
(void)someMethod
{
......code of method...
}
#implementation view2
(void)fistMethod
{
view1 *abc = [[view1 alloc]init];
[abc someMethod];
[abc release];
}
also check this Objective-C call function on another class?

confused category in objective-c concept

hi i'm wondering category in objective-c
i have 3 files
A_ViewController
A_ViewController+Category
B_ViewController
here's example code
1-1. A_ViewController.h
#interface A_ViewController {
// some instance Variables
}
//some public methods
#end
1-2 A_ViewController.m
#import "A_ViewController.h."
#implementation A_ViewController
// implementation public methods and private methods
#end
2-1. A_ViewController+Category.h
#interface A_ViewControler(Category)
-(void) categoryMethod;
#end
2-2. A_ViewController+Category.m
#import "A_ViewController.h"
#import "A_ViewController+Category.h"
#implementation A_ViewController(Category)
-(void) categoryMethod {
NSLog(#"it's A_ViewController+Category");
}
#end
3-1. B_ViewController.h
#interface B_ViewController {
// some instance variables
}
-(void) myMethod;
3-2. B_ViewController.m
#import "B_ViewController.h"
#import "A_ViewController.h"
#interface A_ViewController() // i think it's A_ViewController extension, not A_ViewController+Category, am i right?
-(void) categoryMethod;
#end
#implementation B_ViewController
-(void) myMethod
{
A_ViewController *obj = [[A_ViewController alloc] init];
[obj categoryMethod];
}
#end
i thought it's crashed because i'm not import A_ViewController+Category.h
and i'm not implement -(void) categoryMethod in B_ViewController.m
but it works fine, and no warning.
how [obj categoryMethod] can be linked??
if that's perfectly fine syntax, i have extension question.
if i have another category called A_ViewController+Category2
here's A_ViewController+Category.h
#interface A_ViewController(Category2)
-(void) categoryMethod;
#end
and A_ViewController+Category2.m
#import "A_ViewController.h"
#import "A_ViewController+Category2.h"
#implementation A_ViewController(Category2)
-(void) categoryMethod
{
NSLog(#"it's A_ViewController+Category2");
}
#end
and this situation, if i write a code like 3-2,
then [obj categoryMethod] can' be guaranteed
that comes from A_ViewController+Category or A_ViewController+Category2, right?
I'm struggling slightly to work out what you're trying to do but in regard to standard category behaviour.
If you want to call categoryMethod on an instance of A_ViewController from within B_BiewController you need to import the header that contains the category method #interface.
You can't declare the private category () on A_ViewController from within B_ViewController.m, as the private category or class extension is a special category.
B_ViewController.m should look like this
#import "B_ViewController.h"
#import "A_ViewController+Category.h"
#implementation B_ViewController
- (void) myMethod {
A_ViewController *obj = [[A_ViewController alloc] init];
[obj categoryMethod];
}
#end
Edit
I've just noticed your Category2 method name is the same as your Category method name. This is incorrect and undefined behaviour in Objective C.

Method is not calling from another class

I have a "class-A" which contains a method
-(void)methodA
{
//Logic
}
I have another "Class-B" which is a method
-(void)methodB
{
//Logic
}
Now i am trying to call methodA from Class B
So what i do
In Class B
Create an object of "Class-A"
ClassA *a;
#property(nonatomic,retain)ClassA *a;
#synthesize a;
-(void)methodB
{
[self.a methodA];
}
But the method is not called.
So what am i doing wrong or any other approach for doing this ?
//In class A
//classA.h
#interface classA : NSObject
-(void)methodA;
#end
//classA.m
#implementation classA
-(void)methodA
{
//Logic
}
#end
//In class B
//classB.h
#import classA.h
#interface classB : NSObject
#property(nonatomic,retain)classA *a;
#end
//classB.m
#implementation classB
#synthesize a;
-(void)methodB
{
if(!self.a) self.a = [[classA alloc]init];
[self.a methodA];
//Logic
}
#end

iPhone - class stop seeing superclass' reference

I have this class where I have something like this on .h
#interface myClass : UIImageView <UIGestureRecognizerDelegate>{
#public id referenceOne;
#public id referenceTwo;
}
#property (nonatomic,retain) id referenceOne;
#property (nonatomic,retain) id referenceTwo;
on .m I have
#synthesize referenceOne, referenceTwo;
This class has no delegate protocol.
I have other classes that are based on this one. For one of these classes I have defined a delegate protocol and have my implementation file like this:
#protocol MyBasedClassDelegate <NSObject>
#optional
- (void) doStuff;
#end
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> _delegate;
}
#property(nonatomic,assign) id<MyBasedClassDelegate> delegate;
and on .m I have
#synthesize delegate;
as soon as I have defined this MyBasedClassDelegate the class stopped seeing the referenceOne and referenceTwo ids inherited from myClass. Now Xcode says these are not declared. If I disable the protocol, it sees the references again.
Why is that and how do I solve that?
thanks.
You have an error here:
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> _delegate;
}
rename to
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> delegate;
}
OR
do #synthesize delegate = _delegate; instead
EDIT:
Works well for me
myClass.h
#import <Foundation/Foundation.h>
#interface myClass : UIImageView <UIGestureRecognizerDelegate> {
id referenceOne;
id referenceTwo;
}
#property (nonatomic,retain) id referenceOne;
#property (nonatomic,retain) id referenceTwo;
#end
myClass.m
#import "myClass.h"
#implementation myClass
#synthesize referenceOne, referenceTwo;
#end
MyBasedClass.h
#import <Foundation/Foundation.h>
#import "myClass.h"
#protocol MyBasedClassDelegate <NSObject>
#optional
- (void) doStuff;
#end
#interface MyBasedClass : myClass {
id<MyBasedClassDelegate> delegate;
}
#property(nonatomic,assign) id<MyBasedClassDelegate> delegate;
#end
MyBasedClass.m
#import "MyBasedClass.h"
#implementation MyBasedClass
#synthesize delegate;
-(void) dosmth {
referenceOne = nil;
}
#end
If you change the variable name "_delegate" to "delegate", I think it might work. (Alternatively, comment out the synthesize line and it should also work).
I ran into a similar thing earlier today -- if you synthesize properties for instance variables that do not exist, the compiler doesn't complain but for some reason you can no longer see the superclass's variables.
For me it was finding http://www.iphonedevsdk.com/forum/iphone-sdk-development/53261-unable-access-superclass-member-variables-subclass-implementation.html that helped.