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
Related
How do you deal with subclassing collection attributes in JSONModel?
Let's say I have these two endpoints with different responses of the same "product object".
domain.com/api/1.0/getProductList
domain.com/api/1.0/getProductDetails/productId
I wrote some example code below to show you my issue:
// ProductListModel
#interface ProductListModel : JSONModel
#property (nonatomic, strong) NSNumber *productId;
#property (nonatomic, strong) NSNumber *userId;
#property (nonatomic, strong) NSArray<OrderListModel> *orders;
#end
// ProductDetailModel
#interface ProductDetailModel : ProductListModel
#property (nonatomic, strong) NSURL *productImageUrl;
#property (nonatomic, strong) NSArray<OrderDetailModel> *orders;
#end
// OrderListModel
#protocol OrderListModel <NSObject>
#end
#interface OrderListModel : JSONModel
#property (nonatomic, strong) NSNumber *orderId;
#property (nonatomic, strong) NSNumber *price;
#end
// OrderDetailModel
#protocol OrderDetailModel <NSObject>
#end
#interface OrderDetailModel : OrderListModel
#property (nonatomic, strong) NSURL *orderImageUrl;
#end
The ProductDetailModel wants the same inherited attributes as ProductListModel, but it wants the orders array in the subclassed type.
However this leads to a compiler warning:
Property type 'NSArray<OrderDetailModel> *' is incompatible with type
'NSArray<OrderListModel> *' inherited from 'ProductListModel'
I found this related SO post but I'd rather not monkey patch the JSONModel library.
Edit #1:
This has been discussed in the #574, and #229 github issues before but requires a "type" string to determine what class to instantiate. This requires a change on the backend API.
Is there a way to do this without changing the backend API?
You can't override the property in the subclass to have a different type as it will violate the Liskov substitution principle - #Paulw11
For future readers, here's how the updated example code would look like:
// ProductBaseModel
#interface ProductBaseModel : JSONModel
#property (nonatomic, strong) NSNumber *productId;
#property (nonatomic, strong) NSNumber *userId;
#end
// ProductListModel
#interface ProductListModel : ProductBaseModel
#property (nonatomic, strong) NSArray<OrderListModel> *orders;
#end
// ProductDetailModel
#interface ProductDetailModel : ProductBaseModel
#property (nonatomic, strong) NSURL *productImageUrl;
#property (nonatomic, strong) NSArray<OrderDetailModel> *orders;
#end
// OrderListModel
#protocol OrderListModel <NSObject>
#end
#interface OrderListModel : JSONModel
#property (nonatomic, strong) NSNumber *orderId;
#property (nonatomic, strong) NSNumber *price;
#end
// OrderDetailModel
#protocol OrderDetailModel <NSObject>
#end
#interface OrderDetailModel : OrderListModel
#property (nonatomic, strong) NSURL *orderImageUrl;
#end
I am receiving error like
: Unknown type name 'AppDelegate'
: Unknown type name 'AppDelegate'
: Property with 'weak' attribute must be of object type
I had refer many Question like this but all give answer like add
#class Appdelegate;
I tried with editing this code but after this it gives error like
: AppDelegate.m:23:18: Redefinition of 'ddLogLevel'
Because this log is define in both view controller. i have also tried to commented line of this log from above view controller but then it will give error
: /ChatViewController.m:440:13: Use of undeclared identifier 'ddLogLevel'
Now what is the solution for this?
EDIT: i have to declare like this. because i am working on xmpp. so i have to call [[self appDelegate] connect] method in another view controller.
- (AppDelegate *)appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
EDIT
//Appdelegate.h file //
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#import "ViewController.h"
#import "MBProgressHUD.h"
#import "FriendsViewController.h"
#import <CoreData/CoreData.h>
#import "XMPPFramework.h"
//#class FriendsViewController;
#class ViewController;
#interface AppDelegate : NSObject <UIApplicationDelegate, XMPPRosterDelegate>
{
XMPPStream *xmppStream;
XMPPReconnect *xmppReconnect;
XMPPRoster *xmppRoster;
XMPPRosterCoreDataStorage *xmppRosterStorage;
XMPPvCardCoreDataStorage *xmppvCardStorage;
XMPPvCardTempModule *xmppvCardTempModule;
XMPPvCardAvatarModule *xmppvCardAvatarModule;
XMPPCapabilities *xmppCapabilities;
XMPPCapabilitiesCoreDataStorage *xmppCapabilitiesStorage;
NSString *password;
BOOL customCertEvaluation;
BOOL isXmppConnected;
BOOL isauthenticate;
UIWindow *window;
UINavigationController *navigationController;
//SettingsViewController *loginViewController;
UIBarButtonItem *loginButton;
ViewController *viewController;
FriendsViewController *FriendsViewController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, strong, readonly) XMPPStream *xmppStream;
#property (nonatomic, strong, readonly) XMPPReconnect *xmppReconnect;
#property (nonatomic, strong, readonly) XMPPRoster *xmppRoster;
#property (nonatomic, strong, readonly) XMPPRosterCoreDataStorage *xmppRosterStorage;
#property (nonatomic, strong, readonly) XMPPvCardTempModule *xmppvCardTempModule;
#property (nonatomic, strong, readonly) XMPPvCardAvatarModule *xmppvCardAvatarModule;
#property (nonatomic, strong, readonly) XMPPCapabilities *xmppCapabilities;
#property (nonatomic, strong, readonly) XMPPCapabilitiesCoreDataStorage *xmppCapabilitiesStorage;
//#property (nonatomic, strong) IBOutlet UIWindow *window;
#property (nonatomic, strong) IBOutlet UINavigationController *navigationController;
//#property (nonatomic, strong) IBOutlet SettingsViewController *settingsViewController;
#property (nonatomic, strong) IBOutlet UIBarButtonItem *loginButton;
#property (nonatomic, strong) ViewController *viewController;
#property (nonatomic, strong) FriendsViewController *FriendsViewController;
- (NSManagedObjectContext *)managedObjectContext_roster;
- (NSManagedObjectContext *)managedObjectContext_capabilities;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (BOOL)connect;
- (BOOL) isXmppConnected;
- (void)disconnect;
-(BOOL) isauthenticate;
#end
You have two issues there, I will address them separately:
Unknown type name 'AppDelegate'
This occurs because of circular dependency: ChathistryViewController.h imports AppDelegate.h and vice-versa.
You can resolve this by importing AppDelegate.h only in ChathistryViewController.m and including #class AppDelegate in .h file.
AppDelegate.m:23:18: Redefinition of 'ddLogLevel'
When you are defining ddLogLevel in your *ViewController.h file, it's definition is "copied" to all files you import *ViewController.h.
You should import CocoaLumberjack and define ddLogLevel only in .m files, as they are not needed in .h anyway.
Appdelegate object should be created like this:
It is a singletone Class, where only one instance of the class exists for the current process.
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
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
I'm trying to declare another window in MyThing.m
#property (nonatomic, strong) UIWindow *window;
But get this error
Illegal redeclaration of property in class extension
"MyThing" (attribute must be 'readwrite', while its primary
must be 'readonly')
If I rename window to something else, it is OK. Why is that? Is window meant to be declared once in the AppDelegate.h ?
I figure out the problem, it has nothing to do with the window property declared in AppDelegate.h
The problem is MyThing conforms to UIApplicationDelegate, and UIApplicationDelegate protocol declare a property
#property (nonatomic, retain) UIWindow *window NS_AVAILABLE_IOS(5_0);
So we must do either of these
MyThing.h (like AppDelegate.h does)
#interface MyThing : NSObject <UIApplicationDelegate>
#property (nonatomic, strong) UIWindow *window;
#end
OR
MyThing.m (synthesize the window property declared in protocol)
#implementation WYNAppDelegate
#synthesize window;
#end
So I have this -
#import <UIKit/UIKit.h>
#import "RateView.h"
#interface BasicCardViewController : UIViewController<RateViewDelegate>
#property (weak, nonatomic) IBOutlet UILabel *label;
#property(copy, nonatomic)NSString *message;
#property(atomic)NSInteger rating;
#property (strong, nonatomic) IBOutlet RateView *rateView;
#property (weak, nonatomic) IBOutlet UILabel *ratingLabel;
#end
and this - in my RateView.h file.
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#class RateView;
#protocol RateViewDelegate
-(void)rateView:(RateView *)rateView ratingDidChange:(float)rating;
#end
#interface RateView : UIView
#property(strong,nonatomic)UIImage* fullSelectedStar;
#property(strong,nonatomic)UIImage* notSelectedStar;
#property(strong, nonatomic)UIImage* halfSelectedStar;
#property (assign, nonatomic)float rating;
#property(assign) BOOL editable;
#property (strong) NSMutableArray* imageViews;
#property(assign,nonatomic) int maxRating;
#property(assign) int midMargin;
#property(assign)int leftMargin;
#property (assign) CGSize minImageSize;
#property (assign) id <RateViewDelegate> delegate;
#end
But I get two errors -
1.Cannot find protocol declaration for 'TooviaRateViewDelegate'
2.Unknown type name "RateView"
I've tried to clean, and I've verified that the files are where they should be (their filepaths are to the project).
Why is this happening?
Edit - my AppDelegate.h
#import <UIKit/UIKit.h>
#import "SearchViewController.h"
#interface TooviaAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property NSOperationQueue *queue;
#property (strong, nonatomic) NSDictionary *userProfile;
#property (strong, nonatomic) SearchViewController *searchViewController;
- (NSOperationQueue*) getOperationQueue;
- (id)getSettings:(NSString *)keyPath;
- (void) saveCookies;
#end
There are also issues in my SearchViewController
#import <UIKit/UIKit.h>
#import "BasicCardViewController.h"
#interface SearchViewController : UIViewController
<UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, UITableViewDelegate>
- (IBAction)searchButtonClicked:(UIButton *)sender;
- (IBAction)backgroundTap:(id)sender;
#property (weak, nonatomic) IBOutlet UITableView *resultTable;
#property (weak, nonatomic) IBOutlet UITextField *searchBar;
#property (strong, nonatomic) NSArray *resultsTuples;
#property (weak, nonatomic) IBOutlet UIButton *searchButton;
#property (copy, nonatomic) NSArray *controllers;
#property(strong,nonatomic)BasicCardViewController *detailController;
Delegate and data source properties are usually marked as weak for the object graph management reasons described earlier, in “Avoid Strong Reference Cycles.”