Let's say i have a class definition header file like this :
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (readonly, strong, nonatomic) SomeObject *managedObject;
#end
instead of defining the #synthesize on managedObject to create the getters/setters a friend of mine told me i can do the following header definition using a class extension to do the synthesis more cleanly:
#import "TSPAppDelegate.h"
#interface TSPAppDelegate () //notice the class extension here
#property (strong, nonatomic) SomeObject *managedObject; //this will already be synthesized since its an extension
#end
Could some one explain how this works using the extensions ?
I think your friend is incorrect. You have to #synthesize to have the getters/setters implemented for you
Related
I'm using React Native to make an iOS Application.
I need to add some code to AppDelegate.h file.
However, after searching about this, I realized that it's impossible to declare two interfaces at the same time.
How can I integrate these two interfaces?
// here's the code in AppDelegate.h
#interface AppDelegate : UMAppDelegateWrapper <RCTBridgeDelegate>
#property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
#property (nonatomic, strong) UIWindow *window;
#end
#interface AppDelegate : UIResponder <UIApplicationDelegate, AppsFlyerTrackerDelegate>
#end
Suposing that UMAppDelegateWrapper is a subclass of UIResponder, you can merge both as follows:
#interface AppDelegate : UMAppDelegateWrapper <UIApplicationDelegate, AppsFlyerTrackerDelegate,RCTBridgeDelegate>
#property (nonatomic, strong) UMModuleRegistryAdapter *moduleRegistryAdapter;
#property (nonatomic, strong) UIWindow *window;
#end
I have a very simple problem, yet because it's so simple, I can't find anything wrong with it to fix.
ListSelectorUtility.h
#import <UIKit/UIKit.h>
#protocol ListSelectorDelegate <NSObject>
- (void) returnValueString:(NSString *)value requestID:(long)requestID;
#end
#interface ListSelectorUtility : UITableViewController
#property (strong, nonatomic) NSString *data;
#property (weak, nonatomic) id<ListSelectorDelegate> delegate;
#property (nonatomic) long requestID;
#end
UpdateProperty.h
#import <UIKit/UIKit.h>
#import "ListSelectorUtility.h"
#interface UpdateProperty : UITableViewController <ListSelectorDelegate>
#property (nonatomic, strong) NSDictionary *data;
#end
There's an error on the #interface UpdateProperty : UITableViewController <ListSelectorDelegate> saying that No type or protocol named 'ListSelectorDelegate'. Why is this happened? How to solve this? Thanks.
EDIT: oh, and when I do CMD+click on the <ListSelectorDelegate>, it brings me straight up to the ListSelectorDelegate declaration. That means the IDE (supposedly) can find it.
set property of delegate (strong) on ListSelectorUtility.h
#property (strong, nonatomic) id<ListSelectorDelegate> delegate;
may bey problem of that because if it is week then dealloc by ARC and call the delegate method on ListSelectorUtility.h on your pass the data and values
[self.delegate returnValueString:obj_StringValue requestID:obj_requestID];
And finally this delegate method declare in UpdateProperty.h,
- (void) returnValueString:(NSString *)value requestID:(long)requestID
{
//---------
//Your Code
//---------
}
At the last line, I got the error: Duplicate interface definition for class "ViewController". I want to do an IBAction. What is the fault? What can I do? Please help me.
//
// ViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate>
- (IBAction)showEmail:(id)sender;
#end
#interface ViewController : UIViewController <MFMessageComposeViewControllerDelegate> {
}
-(IBAction)sendMessage:(id)sender;
- (IBAction)showEmail:(id)sender;
#property (nonatomic, strong) IBOutlet UIWebView *site ;
- (IBAction)call:(id)sender;
#property (retain, nonatomic) IBOutlet UIButton *myBotton;
#end
#interface ViewController : UIViewController**i**
The problem is exactly what the error states. You defined #interface viewController twice. Change the name of one to something else. As a side note, it is a terrible idea to name something with a name apple has already used. You should change both viewControllers to something else, more descriptive of what it does, like mailViewController or setupViewController. Weird stuff can happen when you use apple defined names.
#interface FSMainiPadViewController : UIViewController
Why don't you just make it simple like this?
// ViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
// Actions
- (IBAction)showEmail:(id)sender;
- (IBAction)sendMessage:(id)sender;
- (IBAction)call:(id)sender;
// Properties
#property (nonatomic, strong) IBOutlet UIWebView *site;
#property (retain, nonatomic) IBOutlet UIButton *myBotton;
#end
I have a subclassed UIView called TargetView that is contained in a UIViewController called MainViewController. I want to set MainViewController as the delegate for TargetView so that MainViewController can receive messages from the child view (TargetView).
In my MainViewController (UIViewController) header I have the following:
#import <UIKit/UIKit.h>
#import "TargetView.h"
#class TargetView;
#interface MainViewController : UIViewController <TargetViewDelegate>
#property (strong, nonatomic) IBOutlet TargetView *target;
#property (strong, nonatomic) IBOutlet UILabel *lblResults;
#end
When I set the TargetViewDelegate in the interface declaration, it shows up in code completion so it knows that it's there, but then the build fails with the message: can't find protocol declaration..."
In my TargetView (UIView) class I have the following:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "MainViewController.h"
#protocol TargetViewDelegate
#required
-(void)receivedTargetTap;
#end
#interface TargetView : UIView{
id<TargetViewDelegate> delegate;
}
#property (nonatomic,strong) NSString *lblResults;
#property (nonatomic,weak) id<TargetViewDelegate> delegate;
#end
Creating custom delegates is uncharted territory for me. Can anyone tell me what I'm doing wrong? Thanks!
I believe your TargetView.h should be :
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#protocol TargetViewDelegate <NSObject>
#required
-(void)receivedTargetTap;
#end
#interface TargetView : UIView
#property (nonatomic, strong) NSString *lblResults;
#property (nonatomic, weak) id<TargetViewDelegate> delegate;
#end
MainViewController.h :
#import <UIKit/UIKit.h>
#import "TargetView.h"
#interface MainViewController : UIViewController <TargetViewDelegate>
#property (strong, nonatomic) IBOutlet TargetView *target;
#property (strong, nonatomic) IBOutlet UILabel *lblResults;
#end
From your code, you must add <NSObject> after your protocol definition, and remove the MainViewController.h import in your TargetView class.
I think there might be a problem with your import statements in both .h files.
Why do you reference MainViewController.h from TargetView.h? It seems like you don't need it. On the other hand, you should remove the forward declaration of #class TargetView in MainViewController.h, and the simple #import "TargetView.h" should be enough.
After that, you'll also need to implement the required - (void)receivedTargetTap;, otherwise the compiler will complain again that the TargetViewDelegate is not fully implemented.
This question already has answers here:
iOS how to implement a protocol's #property
(3 answers)
Closed 9 years ago.
I read some code, I founded the #protocol have defined a #property's protocol.
For example
protocol1.h
#protocol protocol2;
#protocol protocol1
-(void)p1_method1;
-(void)p1_method2;
#property (readonly, nonatomic) id<protocol2>p2;
#end
protocol2.h
#protocol protocol2
-(void)p2_method1;
-(void)p2_method2;
#end
I don't know the protocol have a #property protocol mean.
Have a simple example? Thanks.
You have to add protocol above the interface you will be using.
#protocol MyViewControllerDelegate;
#interface MyViewController : UIViewController
#property (weak, nonatomic) id <MyViewControllerDelegate> delegate;
#property (copy, nonatomic) NSArray *viewControllers;
#end
#protocol MyViewControllerDelegate <NSObject>
#optional
//sth
#end
All you need to do is #synthesize p2 in a class that conforms to protocol1. Properties from protocols don't get automatically synthesised.
#interface Class1 : NSObject <protocol1>
#end
#implementation Class1
#synthesize p2; // Synthesize p2, the property from protocol1
- (void)p1_method1 {
// Do something
}
- (void)p1_method2 {
// Do something else
}
This will create the correct getter/setter for the property (in your example the property is readonly so only a getter). The #synthesize will also create the ivar, in this case p2.