EXC_BAD_ACESS while accessing class variable - iphone

I have assigned class varaible as....
[MyINFO setUsrID:[dict valueForKey:USERID]];
but when i access "usrID" on another view controller it gives EXC_BAD_ACESS error.
Please suggest what changes should be done in MyINFO ?

There's a good Matt Gallagher post about Singletons, along with a clever macro for creating singletons in Objective-C. You can read it here.

Class variables do not work this way in Objective-C. You can create a singleton class and use instance variables of the singleton.

Related

LibGDX how do I use one boolean in all class

I have one class that I put the Boolean in but I need to use it
In a different class so can anyone tell me what to do
To get the boolean to work for the other classes to
https://i.stack.imgur.com/8clF1.png image 2 https://i.stack.imgur.com/wxA3N.png
First of all check this post, we discourage screenshots of code and/or errors so please post code in future instead of screenshot.
From screenshot on is non-static data member of playbutton class. You can't use non-static data member in all class.
You need an object of playbutton, if you want to access any non-static data member.
playbutton x=new playbutton();
x.on=true;
I'll recommend you to follow naming conventions in java for better coding experience.

some peoples are says class is collection of objects and others says class is instance of an object which oop definition is correct?

First of all! Iam new to programming,
In a blog i saw this two definitions . but iam confused .
It says
Class is a instance of an object
and another says
Class is a collection of objecs
Which definition is correct? How?
If both definitions are true? How?
Thank you
Generally speaking, a class is the mold with which you can create an objet. With one class, you create many objects of the same type.
An object is thus an instance of a class (but not the other way around).
Hope it helps
An object is an instance of a class. An object can be instantiated in other classes. A class is definitely not a collection of objects. However, other objects can be created or instantiated inside of a class.
A class is a blueprint to build a specific type of object. Every object is built from a class.
An instance is a specific object built from a specific class. It is assigned to a reference variable that is used to access the instance's properties and methods. The process of making a new instance is called instantiation and is typically done using the new keyword.
A collection of objects is just that, a collection. There are many data structures designed to hold a collection of object, such as arrays, lists, etc.

Private property declaration in your class implementation?

Sorry if it's too obvious for you. I'm still learning objetive-c and proper design patterns.
Could you explain me why it is a good idea to declare a property inside #interface in implementation file of a class as a private property? You just can use a local declaration of your type with a class scope, since nobody outside your class would use any getter or setter for this property.
Thanks.
By using the property semantics, you get memory management behavior handled 'for free' by the compiler. Even if your data is private within your class, having the compiler emit correct release/retain/copy saves time and mistakes down the line.
With the modern ARC compiler, this is probably less of an issue now.
When you declare something as #private, usually an instance variable or a property, it became only accessible in methods of the class that declared it. Trying to access it from a subclass results in an error.
I know you didn't asked for this but there is also #protected, when a property is declared like this, it becomes only accessible in methods of the class that declared it and in the methods of any class that inherits from that class.
The source for this info is the best book I know about Objective-C - Learning Objective-C 2.0 by Robert Clair.

Class Method Example in objective -c

can anyone tell me how to create a class method and using that how can we share an object DATA in all classes......?
A class method has no state therefore can't do what you're asking for. You want to create a singleton object and pass that around. Check out Singletons, AppDelegates and top-level data for a discussion of singletons and a handy macro for automatically creating them.
-(returnType)methodName; --> this is your normal object method
+(returnType)methodName; --> this will be your class method
[Classname methodName] --> This is how you will call the class method

Adding methods to an Objective C class interface is optional?

Coming from a C++ background, one thing that confuses me about Objective C is the fact that you can add a method to a class without actually specifying it in the class interface. So I had a barrage of questions:
Why would someone choose to not add the method in the class interface?
Is it simply because of visibility?
Methods without a declaration in the interface are private?
Is declaring methods in a class interface just optional?
Is it different for overriding a base class' method?
The main difference is that C++ sets up much of its inheritance and types at compile time and Objective C does it mostly at runtime.
The only differences in putting a method in the interface (if all parameters are objects) in objective-C are that the compiler can see it at compile time and check that an object could respond to the method - if it does not then you get a warning but the compilation does succeed and the program will run and loo for the method at runtime. If the method is in the implementation of the class or a category (or some other way) then the run time will find it and call it successfully.
There are NO private methods you can call any method.
I believe that this is the only way to create private methods in Objective-C. The language does not support the ability to declare a private method so by not declaring a method in the header file you are making private from all callers.
Proper data encapsulation requires that you lock down access to members that either expose data or manipulates it. Not all members ought to be exposed.
Yes it is.
Yes, this is true.
Yes, this is true as well.
This I am not sure about - perhaps someone with more Objective-C knowledge could answer this one.
Extending Andrew Hare's answer to answer 5, no, it doesn't: whether declared in an #interface or otherwise, method replacement/refinement works the same.