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.
Related
I have read question after question about people getting the same error as me, but I simply do not understand them, so before you go searching for duplicate questions, maybe someone can explain to me what I am doing wrong with this subclassing deal.
I have a subclass of UIImageView called swapView that I want to subclass to override the method -(void)count for special cases. I went to subclass this as I have any pre-existing UIKit class, but when I tried to build and run the project, I get this error:
Attempting to use the forward class 'swapView' as superclass of 'coinView'
I have tried putting both the #import statement of swapView and #class swapView in coinView.h and I've tried putting the import statement in coinView.m, but it refuses to build because of this continued error. If I move the import statement into the .m file, all references to the superclass's methods and properties, such as #property (nonatomic) int max; cause errors as well.
What am I doing wrong?
swapView.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#class ViewController;
#interface swapView : UIImageView
{
NSTimer* tmr;
}
#property (nonatomic) int current;
#property (nonatomic) int max;
#property (nonatomic, retain) UIImage* firstImage;
#property (nonatomic, retain) UIImage* secondImage;
#property (nonatomic) BOOL smallMax;
#property (nonatomic, retain) ViewController* pvc;
- (BOOL)testCollision:(CGPoint)point;
- (float)randomFloatBetween:(float)smallNumber bigNumber:(float)bigNumber;
#end
coinView.h
#import "swapView.h"
#class swapView;
#interface coinView : swapView
- (void)count;
- (void)move;
#end
For inheritance, the superclass MUST be inherited.
coinView.h
#import "swapView.h"
#interface coinView : swapView
- (void)count;
- (void)move;
#end
You're both forward declaring and importing ViewController.h in your swapView, which may cause compiler to complain.
swapView.h
#import <UIKit/UIKit.h>
#class ViewController
#interface swapView : UIImageView
.
.
.
#end
I have made a very simple NSObject:
GameSetUpData.h
#interface GameSetUpData : NSObject
#property (readwrite, nonatomic) NSUInteger numberOfPlayers;
#property (strong, nonatomic) NSMutableArray *playerNames;
#property (strong, nonatomic) NSString *gameType;
#property (readwrite, nonatomic) NSUInteger numberOfMinutes;
#property (readwrite, nonatomic) NSUInteger numberOfTurns;
#property (readwrite, nonatomic) CGSize boardSize;
#end
GameSetUpData.m
#import "GameSetUpData.h"
#implementation GameSetUpData
#synthesize numberOfPlayers = _numberOfPlayers;
#synthesize playerNames = _playerNames;
#synthesize gameType = _gameType;
#synthesize numberOfMinutes = _numberOfMinutes;
#synthesize numberOfTurns = _numberOfTurns;
#synthesize boardSize = _boardSize;
#end
This class basically just holds data. I then try to use this object in my viewcontroller:
MainMenu.h
#import <UIKit/UIKit.h>
#class GameSetUpData;
#interface MainMenu : UIViewController
#property (strong, nonatomic) GameSetUpData *gameSetUp;
-(IBAction)tappedNewGame:(id)sender;
-(IBAction)tappedTwoPlayers:(id)sender;
...
MainMenu.m
#import "MainMenu.h"
#import "MJViewController.h"
#import "GameSetUpData.h"
#implementation MainMenu
#synthesize gameSetUp = _gameSetUp;
...
-(IBAction)tappedTwoPlayers:(id)sender {
_gameSetUp.numberOfPlayers = 2;
NSLog(#"number of Players: %d", _gameSetUp.numberOfPlayers);
}
Unfortunately, my NSLog says that numberOfPlayers is equal to 0. What is wrong with my GameSetUpData? I was told that in iOS5 we do not need to call alloc/init or make a dealloc method. Do I still need a -(void)init method in GameSetUpData. Thank you all for your time!
Edit: Please alloc/init your objects -- ARC ONLY deals with release/retain/autorelease. You still need to make an instance of an Object! I apologize for the mis-information. I will make sure to RTFM next time...
Of course you have to alloc/init your object. How should the compiler know when to do that? With ARC you just don't need to retain or release.
Add _gameSetUp = [[GameSetUpData alloc] init]; somewhere.
I have a superclass of UIViewController - MasterViewController which declares a property called itemsViewController. This declares a method called from the MasterViewController, and is wired up via a storyboard in IB.
I have a subclass of MasterViewController which redeclares this property as a specific iPad version, but I can't access the redeclared property from the parent class.
MasterViewController
#interface MasterViewController : UIViewController {
}
#property (nonatomic, strong) IBOutlet ItemsViewController *itemsViewController;
#end
#implementation MasterViewController
#synthesize itemsViewController;
-(void)viewDidLoad {
// I can access itemsViewController in viewDidLoad.
}
#end
MasterViewController_iPad
#interface MasterViewController_iPad : MasterViewController {
IBOutlet ItemsViewController_iPad *_itemsViewController;
}
#property (nonatomic, strong) IBOutlet ItemsViewController_iPad *itemsViewController;
#end
#implementation MasterViewController_iPad
#synthesize itemsViewController = _itemsViewController;
-(void)viewDidLoad {
[super viewDidLoad];
// when I call super viewDidLoad, itemsViewController is nil, as though the property hasn't been overriden
// _itemsViewController is not nil in viewDidLoad.
}
#end
Am I misunderstanding the way property inheritance works in Objective-C?
You can't change the type signature of a method when you override a superclass method.
MasterViewController has these methods:
(void)setItemsViewController:(ItemsViewController *)foo
(ItemsViewController *)itemsViewController
But you're trying to give MasterViewController_iPad these methods:
(void)setItemsViewController:(ItemsViewController_iPad *)foo
(ItemsViewController_iPad *)itemsViewController
Which you can't do: you can't overload the same method name but have different types for the arguments.
If ItemsViewController_iPad is a subclass of ItemsViewController, a quick solution would be to keep the same signature as in MasterViewController but simply use an ItemsViewController_iPad when you set the property.
You can use category if you'd like to override property. Here is example:
I have PDFFileChooserViewController with PDFFileModel and PDFFilesDataSource and some logic related to this properties.
#class PDFFileModel, PDFFilesDataSource;
#interface PDFFileChooserViewController : UIViewController
#property (nonatomic, strong) PDFFileModel* selectedModel;
#property (nonatomic, strong) PDFFilesDataSource*dataSource;
#end
Then I'd like to add specific ViewController for choosing files from Dropbox but my model have some additional fields for example dropboxPath and my DropboxDataSource gets files using another way. So I decided to create category and override this properties:
#import "PDFFileChooserViewController.h"
#class DropboxFileModel,DropboxDataSource;
#interface DropboxViewController : PDFFileChooserViewController
#end
#interface DropboxViewController (ModelCategory)
#property(nonatomic, strong) DropboxFileModel* selectedModel;
#property(nonatomic, strong) DropboxDataSource* dataSource;
#end
Notice that this category will be visible inside DropboxViewController only where I can manipulate with that properties but another classes see only super class interface
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
One of my app worked and crashed at thread0, step 35, mem address 0x00002c60
35 MyAppName 0x00002c60 0x1000 + 7264
So I called command atos to locate the crash point as below:
atos -arch armv6 -o MyAppName.app/MyAppName 0x00002c60
it returns
-[AppDelegate setGPictureArray1:] (in MyAppName) (AppDelegate.m:9)
I show all codes before the line
//AppDelegate.h
#import <UIKit/UIKit.h>
#import "RootViewController.h"
#class RootViewController;
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet RootViewController *rootViewController;
NSMutableArray * gPictureArray1;
NSMutableArray * gPictureArray2;
}
#property (nonatomic, retain) NSMutableArray * gPictureArray1;
#property (nonatomic, retain) NSMutableArray * gPictureArray2;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) RootViewController *rootViewController;
#end
//AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
#implementation AppDelegate
#synthesize window;
#synthesize rootViewController;
#synthesize gPictureArray1;//**it looks like the crash happens here**
#synthesize gPictureArray2;
I can find there is any problem.
Welcome any comment.
I would guess that either the value you are trying to set that property as is an invalid pointer (pointing to a deallocated instance) or that the existing value is invalid.
Create the following function in the implementation file and put a breakpoint in it so you can track who is calling the setter:
-(void)setGPictureArray1:(NSMutableArray*)array
{
[gPictureArray1 release];
gPictureArray1 = array;
[gPictureArray1 retain];
}
Each time it is called check the value of "array" and "gPictureArray1" to make sure it is pointing to something valid. If it is called too many times you can also use NSZombies:
http://www.cocoadev.com/index.pl?NSZombieEnabled