Can't declare another window - ios

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

Related

How can I declare interface in AppDelegate.h in Xcode?

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

IOS unknown typename appdelegate

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];

iOS - shortcut to synthesize properties using extension

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

How to share a variable AND it's value in Objective-C?

I have surfed quite a lot about this. But the results that I've got tells me that either I should use appDelegate or singleton etc.
All of which produces the same result. i.e I am able to share a variable in different ViewControllers but as soon as the ViewController gets changed , the variable looses it's value.
For example I used a variable named myVar of type int. I declared it in AppDelegate and then I'm able to use it in all the ViewControllers with the help of AppDelegate. But as soon as I move from A ViewController to B ViewController the value of the myVar variable gets "0" again. I don't want that . I want the variable to hold it's value. And I don't want to pass this data with the help of pushViewController etc. Please suggest me a good solution.
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, assign) int myVar;
#end
FirstViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate* app = (AppDelegate*)[UIApplication sharedApplication].delegate;
app.myVar = 1;
NSLog(#"%d",app.myVar); //Shows "1" in Log
}
SecondViewController.m
- (IBAction)pressButton:(id)sender{
AppDelegate * app = (AppDelegate*)[UIApplication sharedApplication].delegate;
NSLog(#"%d",app.myVar); // Shows "0" in Log (But I want it to show "1" as I have already set "myVar" value as "1" in my FirstViewController)
}
Your #property (strong, assign) int myVar; is declaring both strong and assign. As an int, it should only be assign, not strong. strong would be used for Objective-c objects, assign for primitive C properties.
I think what you want is the following:
#property (assign, nonatomic) int myVar;
First of all check your code there are some error
AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, assign) int myVar;
And check it gives same value in FirstViewController.m and SecondViewController.m
if your var is a NSString, you can simple use the [copy] protocol. e.g.
NSString *eString = [cString copy];
If it is an object, you need to implement the [copy] protocol for that class.

Why do default Storyboard apps have a second interface declaration

Sorry if this is stupid... but it confuses me?...
I'm trying a new storyboard app with Xcode and just asked myself why there is a second declaration of the #interface in my implementation file?
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
}
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
....
#end
See Apple's documentation: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html
It's a class extension, subtly different from a category, since it has no name inside the parentheses. You use it for declaring properties and methods that are intended to be kept private (out of the header), and redeclaring publicly read-only properties and methods as privately read-write. This allows for cleaner encapsulation.
By request, a friendly example:
JYDuckPondManager.h
#interface JYDuckPondManager : NSObject
#property (nonatomic, assign, readonly) NSUInteger duckCount;
#property (nonatomic, assign, readonly) CGFloat waterLevel;
- (JYDuckReaction *)feedDucks:(JYDuckFood *)food;
- (JYDuckReaction *)harassDucks:(JYDuckTaunt *)taunt;
#end
JYDuckPondManager.m (extension, imaginary implementation omitted)
#interface JYDuckPondManager ()
//// Redefined for internal modification
#property (nonatomic, assign, readwrite) NSUInteger duckCount;
#property (nonatomic, assign, readwrite) CGFloat waterLevel;
//// Internally exclusive properties
#property (nonatomic, strong) NSSet *duckPersonalitySet;
#property (nonatomic, assign) CGFloat flockAnxietyLevel;
//// Private messages
- (void)recalculatePondState;
#end
Other objects should be able to interact with the pond, but they're not supposed to know certain things going on in the pond or redefine the pond's statistics. Keeping nuts-and-bolts stuff in the .m extension ensures that the .h is concise and appropriately limited.
The second #interface block in the .m file is an extension. You could add declarations for methods and instance variables you want to use internally within your class.
The second interface #interface ViewController () is a class extension which is like an anonymous category. A class extension is declared like a category only without a name. Declarations found in these extensions directly extend the declarations found in the class’s primary #interface and can sometimes (in some situations) override declarations found in the primary interface.

Resources