Understanding forward declaration warning - iphone

I am writing my first lines in objective-c for an iPhone app.
This is the code:
/* ViewController.h */
#protocol ImageFlowScrollViewDelegate;
#interface ViewController : UIViewController<ImageFlowScrollViewDelegate> {
NSMutableArray *characters;
UILabel *actorName;
}
/* ViewController.m */
#import "ImageFlowScrollView.h"
#implementation IMDBViewController
/* methods here */
/* ImageFlowScrollView.h */
#protocol ImageFlowScrollViewDelegate;
#interface ImageFlowScrollView : UIScrollView<UIScrollViewDelegate> {
NSMutableArray *buttonsArray;
id<ImageFlowScrollViewDelegate> imageFlowScrollViewDelegate;
}
#property(nonatomic, assign)id<ImageFlowScrollViewDelegate> imageFlowScrollViewDelegate;
- (id)initWithFrame:(CGRect)frame imageArray:(NSArray *) anArray;
- (void)focusImageAtIndex:(NSInteger) index;
#end
#protocol ImageFlowScrollViewDelegate<NSObject>
#optional
- (void)imageFlow:(ImageFlowScrollView *)sender didFocusObjectAtIndex: (NSInteger) index;
- (void)imageFlow:(ImageFlowScrollView *)sender didSelectObjectAtIndex: (NSInteger) index;
#end
Doing this, I get a
warning: no definition of protocol
'ImageFlowScrollViewDelegate' is found
I can fix it using:
#import "ImageFlowScrollView.h"
#interface IMDBViewController : UIViewController<ImageFlowScrollViewDelegate> {
NSMutableArray *characters;
UILabel *actorName;
}
but I would like to know why the forward declaration approach is giving me a warning.

The forward declaration defines the symbol so the parser can accept it. But when you try to use the protocol (or class) - as you do by conforming to the protocol - the compiler needs it's definition to know the layout and size of the resulting object.
In addition, you can forward a class or protocol when you are only using it in the class (in an ivar for example). The compiler then only needs to know the existence of the symbol. But when making use of the class (in the implementation file), the methods need to be declared before use hence the need to include the declaration.
For example :
/* AViewController.h */
#class AnotherClass;
#interface AViewController : UIViewController {
AnotherClass* aClass; //only need the declaration of the name
}
#end
/* AViewController.m */
#import "AnotherClass.h"
#implementation AViewController
- (void) useAnotherClass {
[AnotherClass aMessage]; //aMessage needs to be declared somewhere, hence the import
}
#end
In addition, you already know that you must provide actual implementations in order to link your program.

Related

How to make a forward declaration for private method?

I'm arranging my methods into groups using #pragma mark in implementation. But sometimes, the method implementation code appears below the code that calls this method, and I'm getting "Instance method not found" warnings. It happens when I'm using private methods. How to fix that?
Simplest method is to use a anonymous category. Add something like this to the top of your .m file, before your #implementation:
#interface MyClass()
- (void)myPrivateMethod;
#end
In your Class.m implementation file, you can add an interface section at the beginning and declare private functions in there:
#interface YourClassName (private)
-(void)aPrivateMethod:(NSString*)aParameter;
...
#end
#implementation YourClassName
...
#end
In this case, you would use a class extension inside of your implementation file to define these methods. In this manner, your 'public' API is still defined in your header file, and your implementation file contains the definition of your pseudo-private methods.
YourClass.m
#interface MyClass()
- (void)myPrivateMethod;
#end
#implementation MyClass
- (void)myPublicMethod
{
// This will not throw an error or warning
[self myPrivateMethod];
}
- (void)myPrivateMethod
{
// Do something
}
#end

A question of objective-C protocol

I try to learn protocol of objective C.
I write two files, the first one is FirstViewController.h, and in which there is a protocol "print". I declare FirstViewController class in successViewController with the delegate method "print".
The question is why the console output is "C". Why I can not get the "B" output? Why the protocol method did not perform?
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#interface successViewController : UIViewController <FirstViewControllerDelegate> {
}
#end
#import "successViewController.h"
#import "FirstViewController.h"
#implementation successViewController
- (void)viewDidLoad {
FirstViewController *firstViewController= [[FirstViewController alloc] init];
firstViewController.delegate=self;
NSLog(#"C");
[super viewDidLoad];
}
-(void) print{
NSLog(#"B");
}
#end
#import <Foundation/Foundation.h>
#class FirstViewController;
#protocol FirstViewControllerDelegate <NSObject>
- (void) print;
#end
#interface FirstViewController : NSObject {
id <FirstViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <FirstViewControllerDelegate> delegate;
#end
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize delegate;
#end
Because you never call the print method. Where were you expecting it to be called?
Objective-C protocols allow you to specify that a class is capable of performing certain actions. In your example, the successViewController is declared FirstViewControllerDelegate, meaning it is capable of handing the duties required by FirstViewController of its delegate. It is more of a programming contract between classes, one that can be verified by the compiler.
As a side note, classes in Objective-C should always start with a capital letter, methods should always start lowercase. Your FirstViewController follows this rule, but the successViewController does not.
You need to call the method you want to use.
[successViewController print];
You never call the delegates print method. A delegate can not read your mind and automagically call stuff. Lets take a small example how how delegates are supposed to work.
Assume we have a class called Delay, the only thing it do is to wait for a time when start is called, and then tell it's delegate that it has waited. Optionally the delegate can tell the Delay how long to wait, if the client do not care, a 1 second delay is assumed.
Some rules:
First argument of all delegate methods should be the sender itself, never have delegate methods with no arguments.
Delegate method name should include one of the words:
will - if method is called before something unavoidable occurs. Example applicationWillTerminate:
did - if method is called after something has occurred. Example scrollViewDidScroll:
should - if the method return a BOOL to signal if something should occur. Example textFieldShouldClear:
Name the method to tell what has occurred, not what you expect the delegate to do.
Only exception is if the client is expected to return something, then that something should be part of the name. Example: tableView:editingStyleForRowAtIndexPath:
Here is the simple definition and implementation. Notice that I do not even check if the delegate has been set, since calling methods on nil is just ignored anyway.
// Delay.h
#protocol DelayDelegate;
#interface Delay : NSObject {
#private
id<DelayDelegate> _delegate;
}
#property(nonatomic, assign) id<DelayDelegate> delegate;
-(void)start;
#end
#protocol DelayDelegate <NSObject>
#required
-(void)delayDidComplete:(Delay*)delay;
#optional
-(NSTimeInterval)timeIntervalForDelay:(Delay*)delay;
#end
// Delay.m
#interface Delay
#synthesize = delegate = _delegate;
-(void)start {
NSTimeInterval delay = 1.0;
if ([self.delegate respondsToSelector:#selector(timeIntervalForDelay:)]) {
delay = [self.delegate timeIntervalForDelay:self];
}
[self performSelector:#selector(fireDelay) withObject:nil afterDelay:delay];
}
-(void)fireDelay {
[self.delegate delayDidComplete:self];
}
#end

Objective C: Class Extensions and Protocol Conformation Warnings

I have a large class, which I have divided into several different class extension files for readability.
#protocol MyProtocol
#required
-(void)required;
#end
#interface MyClass : NSObject <MyProtocol>
#end
#interface MyClass (RequiredExtension)
-(void)required;
#end
Is there a better way to do this, without the compiler warning?
warning: class 'MyClass' does not fully implement the 'MyProtocol' protocol
Use a category for each protocol implementation. I use this when I have complex viewControllers.
For example, I have a category that implements NSTextDelegate protocol.
So, MyComplexViewController+NSTextDelegate.h:
#import "MyComplexViewController.h"
#interface MyComplexViewController (NSTextDelegate) <NSTextDelegate>
#end
and MyComplexViewController+NSTextDelegate.m:
#import "MyComplexViewController+NSTextDelegate.h"
#implementation MyComplexViewController (NSTextDelegate)
- (BOOL)textShouldBeginEditing:(NSText *)textObject{
...
}
- (BOOL)textShouldEndEditing:(NSText *)textObject{
...
}
- (void)textDidBeginEditing:(NSNotification *)notification{
...
}
- (void)textDidEndEditing:(NSNotification *)notification{
...
}
- (void)textDidChange:(NSNotification *)notification{
....
}
#end
Then I take all the headers for the main class definition and the categories and combine them into one header which I then import where I need to use the class.
#interface MyClass : NSObject
#end
#interface MyClass (RequiredExtension) <MyProtocol>
-(void)required;
#end
Adopt the protocol in the category.
You don't need to change your style of coding. To get around the warning, it only need to implement "required" method of the protocol, not "optional"
If that's only for readability, you should use categories only. A protocol is not needed in such a case.

Adding C++ Object to Objective-C Class

I'm trying to mix C++ and Objective-C, I've made it most of the way but would like to have a single interface class between the Objective-C and C++ code. Therefore I would like to have a persistent C++ object in the ViewController interface.
This fails by forbidding the declaration of 'myCppFile' with no type:
#import <UIKit/UIKit.h>
#import "GLView.h"
#import "myCppFile.h"
#interface GLViewController : UIViewController <GLViewDelegate>
{
myCppFile cppobject;
}
#end
However this works just fine in the .mm implementation file (It doesn't work because I want cppobject to persist between calls)
#import "myCppFile.h"
#implementation GLViewController
- (void)drawView:(UIView *)theView
{
myCppFile cppobject;
cppobject.draw();
}
You should use opaque pointers and only include C++ headers in the file that implements your Objective-C class. That way you don't force other files that include the header to use Objective-C++:
// header:
#import <UIKit/UIKit.h>
#import "GLView.h"
struct Opaque;
#interface GLViewController : UIViewController <GLViewDelegate>
{
struct Opaque* opaque;
}
// ...
#end
// source file:
#import "myCppFile.h"
struct Opaque {
myCppFile cppobject;
};
#implementation GLViewController
// ... create opaque member on initialization
- (void)foo
{
opaque->cppobject.doSomething();
}
#end
Make sure that all files that include GLViewController.h are Objective-C++ sources (*.mm).
When you include C++ code in the header of your view controller, all sources that import this header must be able to understand it, so they must be in Objective-C++
You need to declare the C++ objects in an interface block in your .mm file.
In .mm:
#include "SomeCPPclass.h"
#interface SomeDetailViewController () {
SomeCPPclass* _ipcamera;
}
#property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)blabla;
#end
I think you need to set the following flag to true in your project settings:
GCC_OBJC_CALL_CXX_CDTORS = YES
This should allow you to instantiate C++ objects in your Objective-C classes.

warnings in iphone sdk

i'm getting this two messages:
warning: 'MainView' may not respond to '-switchToNoGridView'
Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments
1st here:
//GridView.m
#import "GridView.h"
#import "MainView.h"
#implementation GridView
-(IBAction)switchToNoGridView {
[mainView switchToNoGridView];
}
#end
2nd here:
warning: 'MainView' may not respond to '-goBack'
Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments
in this:
//NoGridView.m
#import "NoGridView.h"
#import "MainView.h"
#implementation NoGridView
-(IBAction)goBack {
[mainView goBack];
}
#end
how to avoid these warnings?
Have you declared switchToNoGridView and goBack in the class interface for MainView?
This warning means that the method signatures could not be found in the class of the instance that you are calling the method on; since message dispatch in Objective-C is done at runtime this is allowed, however a warning is shown.
I'm not quite sure from your code, but I think you're encountering one of two errors. Either:
You haven't declared the methods switchToNoGridView or goBack in the MainView class declaration. For Xcode to know that an object responds to a method, you have to include its definition in the class header file, like this:
#interface MainView : ParentObject {
// instance variables
}
// properties
- (void)switchToNoGridView;
- (void)goBack;
#end
This assumes you actually want to declare them as (void), of course - they can have return values, but since you don't do anything with the result of the call in your code, I'm assuming they're void. Or:
You meant to call MainView the class, not mainView the object. Since we can't see your property or instance variable definitions for GridView.h, nor can we see the current method declarations in MainView.h, it's possible that you have a static method +(void)switchToNoGridView and +(void)goBack declared on MainView, but you're calling it on an instance mainView of MainView. For example:
#interface AClass : NSObject { }
+ (void)doSomething;
#end
#implementation AClass
+ (void)doSomething {
NSLog(#"Doing something");
}
#end
#import "AClass.h"
#interface AnotherClass : NSObject {
AClass *aClass;
}
#property(nonatomic,retain) AClass *aClass;
- (void)doSomethingElse;
#end
#implementation AnotherClass
- (void)doSomethingElse {
[aClass doSomething]; // This will break at runtime
[AClass doSomething]; // but this won't
}
#end
Basically, it's possible you've confused class methods with object methods. Either way, you should check your MainView header file for the appropriate method definitions.