Proper declaration of outlets in Objective-C - objective-c

I am a little bit confuse about the this:
Why do some programmers use this at their .h file
{
IBOutlet UIScrollView* scrollView;
IBOutlet UIPageControl* pageControl;
}
#property (nonatomic, retain) UIView *scrollView;
#property (nonatomic, retain) UIPageControl* pageControl;
Instead just this...
#property (nonatomic, retain) UIView *scrollView;
#property (nonatomic, retain) UIPageControl* pageControl;
I am new to XCode and Objective C , I will be glad if there is anyone who can explain me this. Thanks.

In recents versions of the toolchain, it's enough just doing this:
#property (nonatomic, retain) IBOutlet UIView *scrollView;
(If you dont put the IBOutlet you wont be able to connect it to the interface builder)
Some people still do it in the old way, some of them just tradition, some dont know it can be skipped, or it's because they are just old projects.

It was only relatively recently that you could declare properties without a corresponding instance variable (the compiler will create the backing instance variable for you now if necessary, but that wasn't always the case).

Outlets are properties
properties need an ivar (unless some very specific case)
Since Xcode 4.3 or 4.4, ivars are automatically added and synthesized by the compiler if absent.

Related

Why declare the variables of public properties on the interface with the property declarations?

I'm looking at this code below and trying to figure out which of the two of us... (me, or the person who wrote it) doesn't know what they're doing.
Why did he/she declare properties, then declare matching variables as well?
I can't ask the person because this project is inherited from a group long gone.
#interface LayerList : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *tvList;
ArcGisViewController *mapController;
NSArray *fileList;
}
#property (nonatomic, strong) IBOutlet UITableView *tvList;
#property (nonatomic, strong) ArcGisViewController *mapController;
#property (nonatomic, strong) NSArray *fileList;
-(void)visibleSwitchValueChanged:(id)sender;
-(IBAction) Cancel;
#end
This was the usual way in a day when you had to synthesize the properties manually before auto-synthesizing was introduced.

How to silent warning incompatible property type

I am using RTSPagedView from github in my project. Which gives me this warning
Property type 'id<RTSPagedViewDelegate>' is incompatible with type 'id<UIScrollViewDelegate>' unherited from 'UIScrollView'
in
#property (nonatomic, assign) IBOutlet id <RTSPagedViewDelegate> delegate;
in RTSPagedView.h
App is working fine with this warning. Anyone came across this before or knows the solution please help.
Link for this is RTSPagedView
// RTSPagedView.h
// PagedView
// http://github.com/rplasman/RTSPagedView
#import <UIKit/UIKit.h>
#protocol RTSPagedViewDelegate;
#interface RTSPagedView : UIScrollView
#property (nonatomic, assign) IBOutlet id <RTSPagedViewDelegate> delegate;
#property (nonatomic, assign) NSUInteger currentPage;
#property (nonatomic, readonly) NSUInteger numberOfPages;
#property (nonatomic, assign) BOOL continuous;
- (void)resizeBounds:(CGRect)bounds;
- (UIView *)dequeueReusableViewWithTag:(NSInteger)tag;
- (UIView *)viewForPageAtIndex:(NSUInteger)index;
- (void)scrollToPageAtIndex:(NSUInteger)index animated:(BOOL)animated;
- (void)reloadData;
- (NSUInteger)indexForView:(UIView *)view;
#end
#protocol RTSPagedViewDelegate <UIScrollViewDelegate>
- (NSUInteger)numberOfPagesInPagedView:(RTSPagedView *)pagedView;
- (UIView *)pagedView:(RTSPagedView *)pagedView viewForPageAtIndex:(NSUInteger)index;
#optional
- (void)pagedView:(RTSPagedView *)pagedView didScrollToPageAtIndex:(NSUInteger)index;
- (void)pagedView:(RTSPagedView *)pagedView didRecycleView:(UIView *)view;
#end
Try it this way:
#property (nonatomic, assign) IBOutlet id <UIScrollViewDelegate, RTSPagedViewDelegate> delegate;
Works for me.
Issue occurred because
#property (nonatomic, assign) IBOutlet id <RTSPagedViewDelegate> delegate;
In this delagate is poniting towards UIScrollViewDelagate because RTSPagedView is inherited from UIScrollView.
Modify the complete delegate name with any other name like
#property (nonatomic, assign) IBOutlet id <RTSPagedViewDelegate> rtspDelegate;
It will remove the warning.
I think (I cannot be sure without more code) that you are haven't declared your delegate class as conforming to the RTSPagedViewDelegate protocol.
try this:
YourClass.h:
#interface YourClass : NSObject <UIScrollViewDelegate, RTSPagedViewDelegate>
...
#end
And now when you do:
rtsPageView.delegate = self;
The warning should be gone.
This could be complete rubbish, in which case I'll delete it.

UITextfield is not showing the assigned NSString

UITextfield is showing null eventhough nsstring has value...
I've taken uitextfield in IB. Connection is correct in IB.
This is .h file
{
IBOutlet UITextField *dobTextField;
}
#property (nonatomic, retain) UITextField *dobTextField;
In .m file
#synthesize dobTextField;
-(void)displayDOB:(NSString*)str
{
NSLog(#"%#", str);
dobTextField.text = str;
}
In the log it is showing some value... but dobTextField is showing null...i tried
dobTextField.text = [NSString StringWithValue:#"%#", str];
but no use....
Please help me out guys.
Try doing...
NSLog(#"%#", self.dobTextField);
is it nil? (it shouldn't be)
Sounds like you possibly haven't connected up the outlet in IB.
EDIT
Hmm... second thoughts I think you're confusing things.
Remove the ivar from the .h file. Just use the #property.
#property (nonatomic, retain) IBOutlet UITextField *dobTextField;
Remove the #synthesize from the .m file.
In displayDOB function use this...
self.dobTextField.text = str;
This will ensure that everything is referring to the same thing and also uses best practise methods when it comes to properties.
It seems that you have not set your outlet of UITextField in xib
Check that in your interface builder
Also,
change this line
#property (nonatomic, retain) UITextField *dobTextField;
to
#property (nonatomic, retain) IBOutlet UITextField *dobTextField;
Also, change this line
dobTextField.text = str;
to
self.dobTextField.text = str;
Hope this helps you..

Static Instance releasing properties prematurely with ARC

I have a static instance variable that is being used throughout my application. It has properties. These properties are used through my application and seem to work pretty well. However, sometimes the properties are released prematurely. What is odd is that the object that is pulling these properties keeps some and releases others. What would be a good way to insure that the properties of my object are not released prematurely.
Edit: It turns out that the issue was not premature releasing at all. It was a conversion issue. Thanks all for help.
#interface Game : NSObject
#property (nonatomic, strong) PFObject *gameObject;
//#property (nonatomic, strong) Concept *concept; // Will need to add Concept Object to GameObject once it's wrapper is done
#property (nonatomic, strong) User *initialPlayer;
#property (nonatomic, strong) User *invitedPlayer;
#property (nonatomic, strong) User *lastPlayedPlayer;
#property (nonatomic, strong) NSDate *lastPlayedDate;
#property (nonatomic, strong) NSDate *timeOutDate;
#property (nonatomic, assign) int timerTicks;
#property (nonatomic, assign) int currentRoundNumber;
#property (nonatomic, strong) User *winnerPlayer;
#property (nonatomic, assign) int initialPlayerPoints;
#property (nonatomic, assign) int invitedPlayerPoints;
#property (nonatomic, assign) int currentPlayerPoints;
#property (nonatomic, assign) GameStatus status;
#property (nonatomic, assign) int initialPlayerTimeouts;
#property (nonatomic, assign) int invitedPlayerTimeouts;
#property (nonatomic, assign) BOOL isInitialPlayer;
#property (nonatomic, strong) NSMutableDictionary *rounds;
#property (nonatomic, strong, readonly) Round *currentRound;
+(void)getActiveUserGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getYourTurnGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getTheirTurnGameObjects:(PFUser *)user target:(id)target selector:(SEL)selector;
+(void)getGameObjects:(PFUser *)user yourTurn:(id)yourTurn target:(id)target selector:(SEL)selector;
+(Game*)currentGame;
+(void)setCurrentGame:(Game*)currentGame;
..
//.m #implementation
..
static Game *sharedInstance = nil;
..
+(Game*)currentGame
{
return sharedInstance;
}
+(void)setCurrentGame:(Game*)currentGame
{
sharedInstance = currentGame;
}
...
#pragma mark - Player Setters and Getters
-(void)setInvitedPlayer:(User *)invitedPlayer
{
if (nil != invitedPlayer.userObject)
{
[self.gameObject setObject:invitedPlayer.userObject forKey:GAME_INVITED_PLAYER];
}
}
-(User*)invitedPlayer
{
NSObject *value = [self.gameObject objectForKey:GAME_INVITED_PLAYER];
if ([value isKindOfClass:[PFUser class]])
{
return [User userFromPFUser:(PFUser*)value];
}
return nil;
}
What would be a good way to insure that the properties of my object are not released prematurely.
Holding onto them by maintaining a strong reference. It is extremely unlikely that ARC is randomly releasing your data. Much more likely is that you are letting go of it when you don't mean to.
The first place I'd look is at your use of setCurrentGame:, making sure that you're not accidentally working on different Game objects at the same time in different parts of the program. First, make sure t
Your +get... methods are awkwardly named (a get prefix means a very specific thing in ObjC, and it's not what you're doing here). Havings class methods that take targets and actions like this seems a likely place to have trouble. It makes me wonder what's going on inside there.
Your conversion between two kinds of User objects is a little suspicious, and I'd make sure you're not accidentally dropping User or PFUser objects when you don't mean to.
Generally speaking, though, this question is over-vague. Are you winding up with dangling strong pointers? Are your strong pointers seeming to become nil? Is your game object itself becoming nil? How do you know when things are being "released?" Or do you mean that they're deallocating? Have you put a breakpoint in dealloc to see who had the last reference to the object?
Make sure your properties are (retain) type (same as strong, I think), and keep an instance refereed to in the app delegate (so that ARC sees the reference as valid for the entire execution of your program).

Is declaration of variable necessary in objective-c

In the following code:
#interface UnitConverterViewController : UIViewController {
UITextField *tempText;
}
#property (strong, nonatomic) IBOutlet UILabel *resultLabel;
#end
I've seen the same result achieved with out having
{
UITextField *tempText;
}
So, is this really necessary?
No it's not necessary as of Objective-c 2.0.
see: Do declared properties require a corresponding instance variable?