I am new to objective C and trying to implement objection (dependency injector).
But its not working below is the code i am using
My Protocol File
#import <Foundation/Foundation.h>
#protocol InfoquestProtocolTest <NSObject>
-(void):nothing;
#end
My .h file is below
#import <Foundation/Foundation.h>
# import "InfoquestProtocolTest.h"
#interface InfoquestImplementation : NSObject<InfoquestProtocolTest>
#end
my .m file implementing protocol
#import "InfoquestImplementation.h"
#implementation InfoquestImplementation
-(void):nothing{}
#end
Code for module file of objection
#import "InfoquestTestConf.h"
#import <Objection/Objection.h>
#import "InfoquestViewController.h"
#import "InfoquestImplementation.h"
#implementation InfoquestTestConf
-(void)configure
{
[self bindClass:[InfoquestImplementation class] toProtocol:#protocol(InfoquestProtocolTest)];
}
#end
Code for getting object from objection
JSObjectionInjector *injector = [JSObjection createInjector];
[JSObjection setDefaultInjector:injector];
InfoquestTestConf *Module = [[InfoquestTestConf alloc] init];
[injector withModule: Module];
id<InfoquestProtocolTest> testing2 = [injector getObject:[#protocol(InfoquestProtocolTest)];
But when i try to call using [testing2 nothing]; i am getting error and autocomplete doesnt show up nothing.
Thanks
gaurav
You have a syntax error:
You should replace:
-(void):nothing;
with
-(void)nothing;
here is an error in your syntax.please change :
-(void):nothing;
to:
-(void)nothing;
Hence you are using custom delegates. So first you have to set the delegate to your class where you are implementing the method of your protocol.
Related
I was creating an NSObject class for doing web service. But while creating the protocol ,an error comes displaying"cannot find protocol declaration for NSObject" .In Xcode 4 I never came across such problems.Now I am using Xcode 6. Pls help me. Code is as below.
#protocol web <NSObject>
-(void)(NSArray *)urlArray;
#end
#import <Foundation/Foundation.h>
#interface Webclass : NSObject
#end
Write your protocol under #import and put name on your method
#import <Foundation/Foundation.h>
#protocol web <NSObject>
-(void)methodName:(NSArray *)urlArray;
#end
#interface Webclass : NSObject
#end
I am running an ARC 'refactor' on an old iOS app.
However, I am getting the following error
Receiver type 'WarningCallback' for instance message is a forward declaration
WarningCallback.h
#class WarningCallback;
#import <UIKit/UIKit.h>
#import "WebViewController.h"
#import "Constants.h"
#protocol WarningCallback
-(void) warningDismissedAndNavigateToCall:(BOOL) navigateToCall;
#end
#interface WarningViewController : WebViewController {
}
#property (nonatomic,retain) WarningCallback* parentVC;
#end
WarningCallback.m
#import "WarningViewController.h"
#implementation WarningViewController
#synthesize parentVC;
...
-(IBAction) done:(id) sender {
[[self parentVC] warningDismissedAndNavigateToCall:NO];
}
#end
The error occurs on the following line of WarningCallback.m
[[self parentVC] warningDismissedAndNavigateToCall:NO];
The error is because of the line #class WarningCallback;. The error message means that the compiler found a forward declaration of WarningCallback, but is not able to find the corresponding implementation. That's because WarningCallback is not a class, but a protocol.
If you want to forward declare a protocol you can do so as follows:
#protocol WarningCallback;
#interface WarningViewController : WebViewController
#property (nonatomic, weak) id<WarningCallback> parentVC;
#end
#protocol WarningCallback
- (void)warningDismissedAndNavigateToCall:(BOOL) navigateToCall;
#end
Note how I've declared parentVC.
Remove the line:
#class WarningCallback;
When declaring a #protocol, it is not necessary to also declare it as a #class unless you have a specific reason to do so. See:
What's with the declare a class and declare a protocol with the same name?
I want to use a custom method in a custom class in my viewcontroller
//Viewcontroller.h
#import "Class1.h"
//Class1.h
//#import "Class1+Category.h" // Deemed unnecessary in comments below.
#interface Class1: NSObject
-(void)doSomething;
#end
//Class1.m
#import "Class1.h"
#implementation Class1
-(void)doSomething{
NSLog("In doSomething");
}
#end
Now I want a category method of Class1.
//Class1+Category1.h
#import "Class1.h"
#interface Class1 (Category1) // ERROR : Cannot find interface declaration
-(void)doAnotherThing;
#end
//Class1+Category1.m
#import "Class1+Category.h"
#implementation Class1 (Category1)
-(void)doAnotherThing{
NSLog(#"Did Another thing");
}
#end
Finally - in my viewcontroller.m I see the doSomething method, but not the doAnother thing
//viewcontroller.m
Class1 *myClass1 = [[Class1 alloc]init];
[Class1 doSomething]; //Works great!
[Class1 doAnotherThing]; //Not recognized
I have added the -all_load to my target settings. I am out of ideas..do I use the #class? I get 'Cannot find interface declaration' error
Your class and category seems correct at first glance but your controller needs to import Class1+Category.h. Perhaps that's what you missed?
I am trying to call a method in my data controller object to load the data for my application, but for some reason it is not being called. Below is what I have done to initialize it. Any help would be greatly appreciated.
ViewController:
header file:
#import <UIKit/UIKit.h>
#class DetailViewController;
#class DataController;
#import <CoreData/CoreData.h>
#import "JointCAD.h"
#interface TableViewController : UITableViewController {
}
#property (nonatomic, assign) DataController *dataController;
#end
implementation file:
#import "TableViewController.h"
#import "DataController.h"
#implementation TableViewController
#synthesize dataController;
- (void)viewDidLoad {
[dataController refreshData];
}
#end
Data Controller:
header file:
#import <Foundation/Foundation.h>
#import "JointCAD.h"
#import "JointCADXMLParser.h"
#import "TFHpple.h"
#interface DataController : NSObject {
TFHpple *xpathParser;
}
- (void)refreshData;
- (void)initXMLParser;
- (void)noCallsMessage;
- (void)noInternetMessage;
#end
implementation file:
#import "DataController.h"
#implementation DataController
XMLParser *xmlParser;
- (void)refreshData {
NSLog("Some Method");
}
Is 'dataController' Object being set by some other class? - I believe that's why you have set it as a property? Right?
If No, then Remove the property,#synthesize of 'dataController' and try simple allocation of your 'dataController' object and then try calling your method.
Hope it helps.
You either need to initialize "DataController" prior to actually calling one of it's methods, or you need to make the method, "refreshData" a class by changing it's "-" to a "+".
If you need an instance callback instead. You need to rewrite "viewDidLoad" like this:
- (void)viewDidLoad {
DataController *dataController = [[DataController alloc] init];
[dataController refreshData];
}
And get rid of the property declaration of dataController because you haven't initialized it. If you would prefer a property declaration instead, simply allocate the viewcontroller prior to calling a function from it.
- (void)viewDidLoad {
dataController = [[DataController alloc] init];
[dataController refreshData];
}
One last thing to note is that I (and probably Ray) assume that you're using a storyboard configuration. If you are using a xib configuration, you need to add initWithNibName: to each initialization of the view controller.
I hope that's helpful!
I have created a math based application in Xcode 4.4. I am using tabbar based app with the help of storyboard.
I have written all my math functions in a separate class called CalculationMethods which is the subclass of NSObject.
My ViewController:
// FirstViewController.h
#import <UIKit/UIKit.h>
#import "CalculationMethods.h"
#interface FirstViewController : UIViewController
#end
// FirstViewController.m
#import "FirstViewController.h"
#import "CalculationMethods.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%f",[self julianDateFinder: [self currentDayMonthAndYearFinder]]);
}
#end
As you can see I have included my CalculationMethod.h file in both FirstViewController.h and the FirstViewController.m file, but when I use the methods of that class such as julianDateFinder and currentDayMonthAndYearFinder, Xcode errors, saying:
"No Visible #interface for 'FirstViewController' declares the selector 'CurrentDayMonthAndYearFinder'"
I am new to iOS and XCode. Can anyone help me solve the error?
In the FirstViewController, to use any of the methods in the CalculationMethods class, you need to create an instance of CalculationMethods. And then access a method using this syntax: [instanceOfCalculationMethods aMethodInCalculationMethods];
For example in your case, try this:
In the FirstViewController.h file, before the #end:
CalculationMethods *_calculationMethods;
And in the viewDidLoad method:
_calculationMethods = [CalculationMethods alloc] init];
NSLog(#"%f",[_calculationMethods julianDateFinder: [_calculationMethods currentDayMonthAndYearFinder]]);
I think you're misunderstanding how Objective C works slightly.
Without adding details of the CalculationMethods.h header file I can't help you much, but that compiler warning is telling you that FirstViewController doesn't have the method currentDayMonthAndYearFinder.
The reason that this is the case is because you're calling performing the selector CurrentDayMonthAndYearFinder on self which in the context of your FirstViewController instance is in fact the instance of FirstViewController
You said it yourself that your method CurrentDayMonthAndYearFinder is on your CalculatorMethods class so I suggest you either create an instance of your CalculatorMethods class or call the class method named CurrentDayMonthAndYearFinder on your CalculatorMethods class.
The problem here is weather or not you've defined instance methods or class methods.
Do yourself a favour and update your question with the contents of CalculationMethods.h