Obj-C Setters returning null - ios

I am making a simple app and this piece of code has been giving me issues.
Here is my property.
In ConverisonCalculator.h
#property (strong, nonatomic)NSString *startingUnit;
In Viewcontroller.m I am using this code and everytime I NSLog it I am getting (null)
_calculator.startingUnit = #"FPS";
Also here is my lazy instantiation of the object.
- (ConversionCalculator *)calculator{
if (!_calculator) _calculator = [[ConversionCalculator alloc]init];
return _calculator; }
I hope this is enough for you to answer my question. I am not override the default setter either.
Here is my logging.
NSLog(#"%#", [_calculator startingUnit]);

_calculator.startingUnit = #"FPS";
This is not using your property. This is direct access to the instance variable, so your lazy loading code is never called.
If you define properties, always access them through the property:
self.calculator.startingUnit = #"FPS";
Otherwise, you might as well be using instance variables. The only exception is inside the accessor methods themselves, or in init or dealloc methods (in some cases).

Related

Why is instancetype used?

Can someone please explain to me (in simple terms) why an instancetype is used in Objective-C?
- (instancetype) init {
self = [super init];
if (self) {
// Custom initialization
}
return self;
}
It's to increase type safety.
Back in the old days, initialisers just returned an object of type id (any object).
With normal initialisers (those that begin with "init", "alloc" or "new"), this wasn't usually a problem. The compiler would automatically infer the type that it returned and therefore restrict any method calls on the object to the instance methods of that class.
However, this was a problem with static convenience initialisers or "factory methods" that didn't necessarily follow the same naming convention - therefore it was unable to apply the same type safety.
This means that with a class like this:
#interface Foo : NSObject
+(id) aConvenienceInit;
#end
The compiler would accept code like this:
NSArray* subviews = [Foo aConvenienceInit].subviews;
Why? Because the returned object could be any object, so if you try and access a UIView property - there's no type safety to stop you.
However, now with instancetype, the result you get back is of type of your given instance. Now with this code:
#interface Foo : NSObject
+(instancetype) aConvenienceInit;
#end
...
NSArray* subviews = [Foo aConvenienceInit].subviews;
You'll get a compiler warning saying that the property subviews is not a member of Foo*:
Although it's worth noting that the compiler will automatically convert the return type from id to instancetype if your method begins with "alloc", "init" or "new" - but nonetheless using instancetype wherever you can is a good habit to get into.
See the Apple docs on instancetype for more info.
Imagine two classes:
#interface A : NSObject
- (instancetype)init;
#end
#interface B : A
#end
The init method from A is inherited to B. However, in both classes the method has a different return type. In A the return type is A and in B the return type is B.
There is no other way to declare the return type for initializers correctly. Note that most programming languages with classes don't even have return types for constructors, therefore they completely avoid the issue.
This is the reason why Obj-C needs instancetype but of course it can be used outside initializers, too.
It is important to use instancetype instead of id in Objective-C if you are also using this code in Swift. Consider the following class declaration:
#interface MyObject : NSObject
+ (id)createMyObject;
- (void)f;
#end
If you want to create a MyObject instance in Swift 5.3 with createMyObject and then call f for this object, you will have to do the following:
let a = MyObject.createMyObject()
(a as? MyObject)?.f()
Now replace id with instancetype in MyObject to have the following Swift code:
let a = MyObject.create()
a?.f()
As you can see now, you can use MyObject.create() instead of MyObject.createMyObject(). And you don't need to use (a as? MyObject) since a is defined as MyObject? and not as Any.

Custom getter/setter not working

I have a property that sometimes needs to be a strong reference, and other times needs to be a weak reference. I thought I'd cracked it with the following custom getter/setter:
- (PropertyData *)property
{
return _strongProperty? _strongProperty : _weakProperty;
}
- (void)setProperty:(PropertyData *)newProperty
{
_strongProperty = newProperty;
_weakProperty = nil;
}
- (void)weaken
{
if (_strongProperty != nil) {
_weakProperty = _strongProperty;
_strongProperty = nil;
}
}
Previously there was an ivar called property and an #synthesize property. I removed both and replaced with:
PropertyData __weak *_weakProperty;
PropertyData *_strongProperty;
The class using this class calls weaken when it wants to convert to a weak reference to prevent a retain cycle.
This all works fine on iOS, but running the same code on OS X doesn't work at all. In the debugger, setProperty: has no effect on either of the new ivars right from the outset, and it crashes before weaken is ever called.
I'm confused about whether you need a #synthesize a=b statement when writing custom getter/setters (deployment target OS X 10.10). Not obvious how to do that in this case because there are two backing ivars.
Follow the conventions, or you confuse anyone including yourself.
Start your instance variables with an underscore. Don't use synthesise.
Apart from that, you have two problems. One, calling weaken twice sets the property to nil. Second, there's a good chance that your property has only one reference count and goes away when you call weaken.

Property declaration craziness iOS 7

I am at my wits end with a property declaration in a iOS class. In my .h file I have the following declaration :
#property (strong, nonatomic)NSString *sessionID;
In my .m file I have this code :
- (void)setSessionID:(NSString *)aSessionID
{
_sessionID = aSessionID;
// Custom code to set this in a global context
}
This is all fine and compiles with no issues. Now I need to have the sessionID return a default value if nothing is set, however the moment I add this line :
- (NSString *)sessionID
{
return _sessionID ? _sessionID : #"defaultSession";
}
then the first line in the setSessionID:
_sessionID = aSessionID;
causes an error with "Use of undeclared function _sessionID. Did you mean aSessionID", I am at my wits end to figure out what is causing it.. I have so many classes with variables and have never seen this before... what is causing this? I restarted Xcode, cleaned out the project and no luck.. If I remove the - (NSString *)sessionID method, then it stops complaining.. but the moment I add the method declaration the Xcode marks it as an error.
Anypointers accepted! :)
Edit: I also noticed, that in this class if I add any property accessor method it complains about the ivar.. e.g. I have another property declared
#property (strong, nonatomic) NSString *userEmail
The moment I add -(NSString *)userEmail, the ivar _userEmail usage above it all becomes undeclared.. :(
If you override both the setter and getter of a property, the compiler will not automatically synthesize the backing ivar for you. You need to do a manual synthesis,
#synthesize sessionID = _sessionID;

Method is not being called

I've linked a button to the following method:
- (IBAction)searchButton
{
NSString *searchText = _searchField.text;
NSLog(#"lol");
[_search testSearch:searchText];
}
The last line calls the method testSearch within an object named search, defined as follows:
#property (strong, nonatomic) Search *search;
Within Search, testSearch is defined as follows:
-(void)testSearch:(NSString *)testString
{
NSLog(#"HELLO");
}
My final output, when I click search, is only "lol" (each time I click the button). It does NOT print "HELLO", as testSearch should be doing. I have included testSearch in Search.h, so it should be accessible...why isn't this method being called?
You should start by initializing your _search ivar to an instance of Search in your designated initializer (or in viewDidLoad or some other "user gonna use this" method).
- init {
if ((self = [super init])) {
_search = [[Search alloc] init];
}
return self;
}
You should generally avoid lazy initialization in getter methods for a variety of reasons:
It adds unnecessary code; use #property and the default synthesized implementations. Leads to simpler code and less of it.
a getter that does lazy initialization yields a getter that causes mutation. That is inconsistent it is quite odd to see a KVO change notification when calling a getter (unless, of course, you don't fire the KVO notification... at which point, you have non-observeable mutation).
a getter that causes mutation is inherently not thread safe unless you add the code, tricky code, to make it so.
lazy initialization is generally a premature optimization. Unless you have an identifiable memory or CPU performance issue caused by initializing a resource "too soon", then adding the complexity of lazy initialization is wasted effort.
lazy initialization can lead to weird ordering dependencies and other complexities. Far better to have a known entry point for initializing a subsystem than to rely on subsystem X being initialized prior to Y, both by side effect.
The search object you are sending the message to has not been instantiated so you are sending a message to nil. In Obj-C this does not crash the program, instead it does nothing. Its a best practice in Objective-C programming to perform lazy instantiation in the getter method of the iVar. Additionally, you would have to couple this best practice with not accessing your iVars directly and use the setters and getters for whichever iVar you are trying to access. Below is an example of lazy instantiation in the getter method for your search iVar:
-(Search *)search
{
if(!_search){
_search = [[Search alloc]init];
}
return _search;
}
Here is your method call while NOT accessing the iVar directly:
[search testSearch:searchText];

EXC_BREAKPOINT: Message sent to deallocated instance

I get the above message in XCode 4.6. I've done a pretty thorough search and but nothing seems to match the exact circumstances surrounding my issue. Admittedly, I'm relatively new to iOS dev, and memory-management has never been my strong suit, but this just has me completely miffed.
I have an instance variable theLink which is defined in the class Game as follows:
#interface Game : NSObject
// Class objects
#property(nonatomic,retain) NSMutableArray *queryItems;
#property(nonatomic,retain) NSMutableArray *theArray;
#property(nonatomic,retain) NSString *theLink;
#property(nonatomic,retain) NSString *thePath;
theLink is set in the makeGame method which is called in the method initialiseGame in my view controller:
- (void) initialiseGame
{
bool gameCreated = FALSE;
while (!gameCreated)
{
gameCreated = [theGame makeGame:#"ptl"];
}
[loadingIndicator stopAnimating];
[loading setText:#"Tap to Start"];
[self performSelector:#selector(setLabels) withObject:nil afterDelay:0.0];
}
(Note: the performSelector afterDelay is used to allow the view to update before continuing. Bit of a hack but I couldn't work out a better way!)
The app then loads the game, and when the user taps the screen to start, the next method which is called from the view controller is:
- (void) setupLink
{
...
for(int i=0; i<[theGame.theLink length]; i++) {
...
}
}
It is on this reference to theGame.theLink where I'm am getting the crash.
What has me most confused is that if I call theGame.theLink from inside the initialiseGame method, it is displays correctly, and also calling any other variable from the Game class (such as thePath or theArray works perfectly, so theGame object has not been deallocated in it's entirety, only the variable theLink.
It seems to me that the variable is being deallocated somewhere as the view controller is being updated. I haven't released the variable, and can't work out why only this variable is being deallocated. As I said at the start, memory-management is not my strength!
Any help/ideas would be hugely appreciated. Let me know if you require any more details.
Thanks heaps,
Andrew
EDIT: Setting of theLink within makeGame
- (bool) makeGame:(NSString*)gameType
{
...
[self getLink];
}
- (void) getLink
{
...
if (... && ((arc4random() % 10) > 8))
{
theLink = #"Animals";
}
}
There are many different ways theLink may be set, depending on random numbers and other factors. This is the most basic form which simply sets it to a static string. It doesn't matter how theLink is set or what it is set to, the program always crashes at the same point.
If theLink is being set to the parameter being passed to it ,#"ptl" or some similar temporary string, it will give you a problem, because it is just a pointer pointing at the current location that is holding #"ptl". After the makeGame method is completed, your system will assume that it is all done with #"ptl" and just free it up.
When you make an #"stringwhatever" in your code, it is supposed to be the equivalent of making an NSObject that is an immutable literal instance of #"stringwhataver". It should, in theory handle all the reference counting in a nice way, but when you are doing your own memory management, there are so many ways to lose count of your references.
There's a pretty simple rule to follow. If you've declared properties, access them via the property. To do otherwise (as you are doing above, with theLink = ...) bypasses all of the memory management built into the property accessors.
self.theLink = ...
Would have solved this problem under MRC. Switching to ARC has "solved" your problem without you understanding the root cause.

Resources