iPhone - Apple default sample code missing some vars? - iphone

In XCode 4, when you create a new View-base-application project, here is the .h of the AppDelegate :
#import <UIKit/UIKit.h>
#class TestAppleProjectViewController;
#interface TestAppleProjectAppDelegate : NSObject <UIApplicationDelegate> {
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet TestAppleProjectViewController *viewController;
#end
And some items on the .m :
#implementation TestAppleProjectAppDelegate
#synthesize window=_window;
#synthesize viewController=_viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I can see properties without vars...
I can see synthesize with var names that does not exist in the class...
I can see calls to those properties....
But... Where are the vars ?
Why such a code is working ?
Are there no more need to define vars into the class ? Are properties enough ?

In fact when you declare a var as a property, you don't have to declare it again as var. The previous comportment where we had to declare it again was a bug in the previous versions of Xcode. And now it's corrected.
It's just an habit to take, less code duplicate and it's good. (By default it's creating ah hidden var with the same name beginning by "_").

I'm not sure about this... I think when you write #synthesize prop=_var it will automatically create a private instance variable named _var for the property prop.

Related

Adding AppDelegate.h and AppDelegate.m info in Swift

Hi I am just starting in swift and taking an iPad app and creating a iphone version.
I am trying to add annotations to a map in Swift and need to add so info to AppDelegate.h and .m to get it to work but in Swift there are is no AppDelegate.h or .m . How do I get this to work?
Here is what has to go into the AppDelegates
.h
#import <UIKit/UIKit.h>
#class AnnotationViewController;
#interface AnnotationAppDelegate : NSObject <UIApplicationDelegate> {
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet AnnotationViewController *viewController;
#end
.m
#import "AnnotationAppDelegate.h"
#import "AnnotationViewController.h"
#implementation AnnotationAppDelegate
#synthesize window=_window;
#synthesize viewController=_viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Please Help..Under tight deadline Thanks.

Local declaration of "speed view" hides instance variable

So I have been searching in a few hours for why my iPhone app hates me. This is the error I get:
Warning: local declaration of 'speedView' hides instance variable.
Here is my .m file
#implementation MainViewController
#synthesize speedCount;
#synthesize speedView;
#synthesize popoverController;
- (void)setspeedView:(UILabel *)speedView
{
[speedView setText: [NSString stringWithFormat:#"%d",speedCount]];
speedCount = 0;
speedCount++;
}
.h file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MainViewController : UIViewController <LoginDelegate,WEPopoverParentView,PopoverControllerDelegate,MainMenuDelegate,MKMapViewDelegate,UIActionSheetDelegate,UIAccelerometerDelegate, CLLocationManagerDelegate>
{
AppDelegate *appDelegate;
IBOutlet MKMapView *userMap;
IBOutlet UILabel *speedView;
CLLocationManager *locationManager;
}
#property (strong, nonatomic) IBOutlet UILabel *speedView;
#property(nonatomic) int speedCount;
I really don't understand why it says that I am hiding the instance variable.
You have a ivar (an instance variable) called speedView.
In your method
- (void)setspeedView:(UILabel *)speedView
speedView is a local variable whose name clashes with the ivar.
If you are using a modern version of the compiler just remove the #synthesize directive.
It will be automatically added by the compiler in this form
#synthesize speedView = _speedView
which will create the ivar _speedView, whose name doesn't clash anymore with the local variable.
Also note that declaring both the instance variable and the property is redundant. The ivar will be automatically created by the (implicit) #synthesize directive.
Here's a "modern" version of your class:
.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface MainViewController : UIViewController <LoginDelegate,WEPopoverParentView,PopoverControllerDelegate,MainMenuDelegate,MKMapViewDelegate,UIActionSheetDelegate,UIAccelerometerDelegate, CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *speedView;
#property (strong, nonatomic) CLLocationManager *locationManager;
#property (strong, nonatomic) IBOutlet MKMapView *userMap;
#property (strong, nonatomic) AppDelegate *appDelegate;
#property (nonatomic) int speedCount;
.m
#implementation MainViewController
- (void)setspeedView:(UILabel *)speedView {
[speedView setText:[NSString stringWithFormat:#"%d", self.speedCount]];
self.speedCount = 0;
self.speedCount++;
}
Please note:
properties are nice: use them whenever you can
#synthesize is implicit
the implicit version of #sythesize declares a _ivar for the property ivar
always access variables through the getters/setters, i.e. self.ivar, a part from init methods. If you need to access the var directly use _ivar or self->_ivar
As a final remark, this looks a bit weird
self.speedCount = 0;
self.speedCount++;
and it could be replaced with
self.speedCount = 1;
Are you sure it's what you mean? Also, as noted in the comments by others, you are not using the method parameter speedView. That smells bad and you may want to double check your implementation.

Request for member not a structure or union - iOS

I wanted to change the default view that loads up in MainWindows.xib. So I added a new instance variable for the class I want to add the view of here is the code in .h
#class Table;
#interface GameUIAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
Table *viewController; //changed the class
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet Table *viewController;
and then in the MainWidow.xib I changed the class name and xib on the UIView
in the .m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
[self.window addSubview:viewController.view]; //error here
[self.window makeKeyAndVisible];
return YES;
}
what am I missing and please explain me why am I getting this error
You need to #import "Table.h" in your .m file.

error: #synthesize

In my AppDelegate.h file i written the following code
#interface iBountyHunterAppDelegate : NSObject <UIApplicationDelegate> {
UITabBarController *tabcontroller;
}
#property (nonatomic,retain) IBOutlet UITabBarController *tabcontroller;
and in AppDelegate.h file I synthesize it.
#synthesize tabcontroller;
but in #synthesize line i get an error and the msg is: "MISSING CONTEXT FOR PROPERTY IMPLEMENTATION DECLARATION"
can anyone tell me how to solve it?
I suspect you have put the #synthesize outside of your #implementation. It should look something like this
// iBountyHunterAppDelegate.m
#import "iBountyHunterAppDelegate.h"
#implementation iBountyHunterAppDelegate
#synthesize tabcontroller; // note that this is between #implementation and #end
// other stuff
#end
"and in AppDelegate.h file I synthesize it." Probably it´s just misspelled but it has to be in .m file :P
#interface iBountyHunterAppDelegate : NSObject <UIApplicationDelegate>
{
UITabBarController IBOutlet *tabcontroller;
}
#property (nonatomic,retain) IBOutlet UITabBarController *tabcontroller;
and in AppDelegate.h file I synthesize it.
#synthesize tabcontroller;
synthesize your property in controller(iBountyHunterAppDelegate.m) file instead of interface(.h) file

Adding UIViewController subclass with IBOutlet

I have seen this example on Apple's website before, but for some reason, I cannot find it and am brainfarting. I created a TestViewController.h and .m file that subclass from UIViewController and have a .xib. In the TestAppDelegate.h, I have:
#interface TestAppDelegate : NSObject <UIApplicationDelegate> {
TestViewController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet TestViewController *rootController;
in TestAppDelegate.m, I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window addSubview:rootController.view];
[self.window makeKeyAndVisible];
return YES;
}
Then in my MainWindow.xib, I drag a ViewController, change the class to TestViewController, control drag the outlet from TestAppDelegate to TestViewController. It builds fine, but when I run it I get:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<TestViewController 0x4d06570> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
I cannot remember what I'm missing in these steps. Any help would be appreciated. Thanks.
#interface TestAppDelegate : NSObject <UIApplicationDelegate> {
TestViewController *rootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet TestViewController *rootController;
in TestAppDelegate.m, I have:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window addSubview:rootController.view];
[self.window makeKeyAndVisible];
return YES;
}
IF IT IS THE CODE YOU ARE REALLY USING THEN ADD FOLLOWING LINE TO YOUR INTERFACE:
UIWindow *window;
AND CHECK IF IT HELPS.
For your window you need to setup you rootViewController. By the way your naming "rootController" is kind of misleading, b/c UIWindow has a property rootViewController.
So to get this to work instead line [self.window addSubview:rootController.view]; you should do this self.window.rootViewController = self.rootController;
If you want to compare your code with a working code just create new project from template. Choose View-Based Application it has the schema you are looking for.
Open the TestViewController.xib and check whether any false outlets are connected there. Select the FilesOwner and go to connection inspector. The false outlets will be shown faded.. In this case, it would be label
Check your outlets in Interface Builder, you have something named "label" that does not exist. Remove the reference to this and you should be good to go.
again checkout....
use the connection inspector and write nib name and class name....in main View Controller.Xib