Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol while adding overlays to MKMapView - objective-c

I got this error when I'm trying to add overlays to map, from one of SO answers How to prevent from "_OBJC_IVAR_$_UIPickerView._backgroundView symbol not found" error? I understood that we shouldn't use private ivar, but i didn't use any of them in my code, still I got the following error.
Error [IRForTarget]: Couldn't find Objective-C indirect ivar symbol OBJC_IVAR_$_MKPolygon._interiorPolygons
Please show me the way to solve this problem

As of XCode 4.5 the 3 variable declaration statements can be collapsed into 1
An iVar
A Property
A Synthesize
Now:
1. Synthesize
So, if old code was:
NSString* name; //in .h file
#property (nonatomic, strong) NSString* name; // in .h file
#synthesize name; // in .m file
Since 4.5 this is all that is needed is:
#property (nonatomic, strong) NSString* name; // in .h file
We can now address this iVar with either of the following:
self.name = #"john"; // used to reference the full property setter/getting - usually from outside the object
_name = #"john"; // used to reference an internal variable
This error message happened to me when the I used the iVar (notice no leading underscore):
name = #"john";
To fix change to either:
self.name = #"john"; // or
_name = #"john";
Note: I did not get this error on xcode version 4.5 (4G182) but I did on 4.5.2 (4G2008a).

Related

Objective C: Should I assign the variable AND create a property or is just one of them enough?

I have got a header file (.h) and I want to declare name but all these ways work the same I think because I haven't seen any difference with functionality. Could you tell me what the difference is between:
This with both declarations:
#interface someClass : UIViewController
{
NSString *name;
}
#property (nonatomic, copy) NSString *name;
#end
Without variable:
#interface someClass : UIViewController
{
}
#property (nonatomic, copy) NSString *name;
#end
Or Without property:
#interface someClass : UIViewController
{
NSString *name;
}
#end
#interface someClass : UIViewController
{
NSString *name;
}
#property (nonatomic, copy) NSString *name;
#end
Doing this you will explicitly declare both a property and an ivar.
A property is just a set of methods:
- (void)setName:(NSString*)name;
- (NSString*)name;
An ivar is the memory store holding the value that the property methods manage. This allows you to do:
self.name = ... // access through setter method
name = ... // direct access
The advantage of using properties is that they deal with memory management for you. E.g., in your case, the property is of type copy: this means that with the first syntax (self.name = ...) a copy of the object will be done. If not using properties, you would explicitly need to do: name = [originalString copy]; to obtain the same effect.
Other options you can specify for properties (but not ivars) are: strong and weak ownerships.
Furthermore, a property also represents a public interface to access the variable from outside your class.
Using direct access you are on your own as to memory management (if you are not using ARC).
If you are using ARC and don't define properties, you will not be able to control how the memory is managed by specifying the ownership: strong, weak, retain).
#interface someClass : UIViewController
{
}
#property (nonatomic, copy) NSString *name;
#end
Here you only declare the properties; the ivar is "inferred" by the #synthesize directive in your implementation file. This is only possible in Objective C 2.0 and later (previously, the ivar declaration as above was mandatory).
The same considerations as above applies, with a minor nuance: with older versions of LLVM (ObjC compiler) you will not be able to reference directly the auto-synthesized ivar; with current version of LLVM, if you omit the #synthesize directive, then an automatic ivar named after your property would also be declared (in your case it would be _name).
This last paragraph may seem a bit "advanced", or contrived, but you can safely ignore it.
#interface someClass : UIViewController
{
NSString *name;
}
#end
In this case you are only declaring the ivar. No accessor methods. You will need to handle memory management on your own (if not using ARC), futhermore you will not be able to access the variable from outside the class. For that you need accessors.
Hope this helps.
Case 1:
The is the old method, here the #property and variable are not related until you #synthesize name = name;
Access methods :
variable : name = #"hello"; //direct access to viariable
setter/getter : self.name = #"hello" // set value to name using setName: selector
With the latest xcode just the property is enough.
Case 2:
the new xcode style. Here the synthesize and variable creation is taken care by the compiler. (so less 2 lines of code and this also helps with memory management)
Access methods :
variable : _name = #"hello"; //direct access to viariable
setter/getter : self.name = #"hello" // set value to name using setName: selector
Case 3:
Here the name is just a variable and it dose not have a setter or a getter.
with out property (or) setter & getter this is as good as a local variable and it cannot be accessed from other objects.

Property should be #synthesize with inner var or not

I'm little confuse because in some tutorials is just
in h
#property (readwrite,nonatomic) NSUInteger DirectProp;
in m
#synthesize DirectProp;
But in other is like this
in h
#interface MyClass : CCNode {
NSUInteger throuVarProp;
}
#property (readwrite,nonatomic) NSUInteger ThrouVarProp;
in m
#synthesize ThrouVarProp = throuVarProp;
Which way is the right way?
Both are correct...
The difference is in older and newer style of writing.
In latest versions of Xcode you will see even synthesize is not required and it will take automatically as _yourIvarName
With a single argument:
#synthesize directProp;
The synthesized getter/setter methods are called the same as the instance variable used to store the value. This can get confusing. For example:
self.directProp = YES;
[self setDirectProp:YES];
directProp = YES;
Are all valid.
With the additional = ivar, you are able to name the instance variable (the convention being to use a leading underscore), which is a good idea, so you don't get confused:
#synthesize directProp = _directProp;
self.directProp = YES;
[self setDirectProp:YES];
_directProp = YES;
As also mentioned, with newer runtimes you don't need to declare the instance variable before use, which is also a bad idea and seems to be there to promote laziness. You will regret using this feature some day...
They are both right.
With:
#interface MyClass : CCNode {
NSUInteger throuVarProp;
}
You are saying to the compiler that that object has an instance variable of NSUInteger kind.
This if formally correct, but now this code is auto generated by Xcode and the compiler when you use a #property.
#syntesize, used in the implementation, creates the getter and setters: special instance methods used to access your instance variable from "outside".
In the last XCode (AFAIK from 4.4) even the #syntesize is auto generated, in the format
#syntesize var = _var;
Newer runtimes don't require you to define the iVar. In fact, they even auto-synthesize the iVar for you.
// .h
#property (…) Type name;
This will automatically synthesize the property as if you had written:
// .m
#synthesize name = _name;
Thus you're even able to access the underlying variable, which I would not recommend though because I consider it an underlying implementation detail.
Note that if you override the getter the auto-synthesis will no longer happen:
// .m
#synthesize lazyProperty = _lazyProperty;
- (NSString *)lazyProperty {
if (!_lazyProperty)
_lazyProperty = #"Foobar";
return _lazyProperty;
}

Why can't I set this to a string?

I've just starting out with obj-c and I created 2 files, a .h and a .m file. The .h file is..
#import <Foundation/Foundation.h>
#interface CardUnit : NSObject
{
#property (assign) NSString *name;
#property (assign) NSString *gold;
#end
and the .m file is
#import "CardUnit.h"
#implementation CardUnit
#synthesize gold = #"gold";
#synthesize name = _name;
#end
But it's giving me an error on
#synthesize gold = #"gold";
Saying "expected ; after synthesize"
Why can't I set that to a string?
#synthesize is not used for giving variables a value, but is rather a shorthand for defining basic getters and setters for the variable. The
#synthesize var = _var
syntax is used to say, "I want you to use the instance variable _var as the internal variable for the property var".
If you want to assign a default string to a property, put it in your init method:
-(id)initWithName:(NSString*)name
{
self = [super init];
if(self)
{
self.gold = #"gold";
self.name = name
}
return self;
}
Or you can set the default value in the getter (per #Mario's comment bellow):
-(NSString*)gold
{
_gold ? return _gold : return #"gold";
}
You got synthesize wrong. It is not for assigning values, it is for generating default setter/getter methods in case you do not provide them. You can use property = ivar to specify which ivar should be used for the property, but a constant value is NOT ivar. So you can't assign string value in this way. Please check The Obj-C Programming Language (Property Implementation Directives) for the details.
You can not assign a value in the #synthesize declaration.
The #synthesize is a declaration that sets getters and setters for variables. and it is not for assigning vales.
You should do later on in a method that will set the value to your "gold" var.
You are not suppose to set value to the variable in synthesize. They are just for synthesize accessor methods.
The #synthesize generates a getter and setter for the property, but the compiler takes care of generating those.
I think it would be a good idea to read a good tutorial on iOS development, maybe start with the Start developing iOS apps today on the Apple Developer connection website.

How come synthesizing with an underscore is not working?

I am trying to synthesize variables in my iPhone app with
#synthesize samples=_samples;
with samples declared as
#property (strong, nonatomic) NSMutableArray *samples;
However, I get a build error claiming that _samples does not exist. Why?
Are you trying to access the _samples from outside the implementation file? ivars generated through #synthesize are not viewable by anything outside of the implementation where the #synthesize was called. So if you do something like this...
MyView *myView = [[MyView alloc] init];
myView._sample;
...you will see an error. See here for more details: https://stackoverflow.com/a/8511046/251012
.
.
.
.
EDIT: All of the below is wrong. Left, so that the comments make sense
Are you declaring your ivar's and are the names spelled correctly?
When you say something like...
#synthesize foo = _foobar;
...you need to make sure that you set the instance variable in your interface like so...
#interface MyObject : NSObject
{
NSString *_foobar;
}
#property(nonatomic,strong) NSString *foo;
#end
To be clear, when you say foo=_foobar, foo is the base name to auto-generate the getter/setter's, and _foobar is the name of the ivar. If no ivar is declared, #property will auto-generate one of the same name.
Same code is working on my side. Try to restart xcode and rebuild the project.

Objective-C – Expected identifier error

I'm getting an expected identifier error when I try to compile my code.
careerURL is setup like this in .h file:
#property (nonatomic, copy) NSString *careerURL;
And synthesized like this in .m file:
#synthesize careerURL;
I really do not understand what is the issue here. The exact code works in another viewcontroller.
You should either use dot . syntax,
NSString *wtf = self.careerURL;
Or Objective-C message syntax,
NSString *wtf = [self careerURL];
Not both at the same time.
You should write:
NSString *wtf = self.careerURL;
When you are writing [object method] it is expected that you want to call method method from object object. If you want just access some value (that is defined as #property) you can type:
[self nameOfValue];
or
self.nameOfValue;