understanding class and instance methods variables - objective-c

In my code, i have a class , mainClass , which has an instance method -(void)record .
In the interface of mainClass , i have instance variable,which used by this method.
Now, i know that every time i am creating a new instance of the class with :
mainClass *instance=[mainClass alloc];
its creating a new place in memory to all this class variables , and now if i do
[instance record];
it will create all the variables that are in record but they will be new once.
Now lets say i want to call from an outside class to record , and change/use its variables
not create new once, but use the once already created in the mainClass.
whats the best way of doing this, and what it has to do with a class method ?
Should this method be a class method? if yes , why ?

If you want it accessible, instance and permanent changed you have to make it static, will answer your next question
Objective C Static Class Level variables

Related

What is the difference between classVariableNames: '' and instanceVariableNames: '' when setting up a Singleton in Pharo Smalltalk?

One is located on the instance side:
Object subclass: #MyClass
instanceVariableNames: ''
classVariableNames: ''
category: 'MyApp'
The other accessible on the class side:
MyClass class
instanceVariableNames: ''
When you create a new class (For example a Pen) you create a Class that is an instance of metaclass (this will be Pen class) and you will be able to get Pen instances that are instance of Pen class.
You can have a lot of Pen but you will have only one Pen class.
An instance variable is a variable of one instance. Each instance has it's own variable. Each Pen can have it's own color.
A class variable is a variable of a Class object (Pen class). As you have only one instance of Pen class, this variable will have only one value. If your pen has a class variable #DefaultColor, myPenInstance class defaultColor will return the same for all the Pen instances.
And last, an instance variable of the class side works as an instance variable of the instance side but for a class.
The difference between a class variable and an instance variable in the class side is that the class variable is unique for the class and it's subclasses while an instance variable in the class side will be specific for each of it's subclasses.
If you have a UniqueInstance class variable that stores a Singleton with an accessor in you Pen, Pen uniqueInstance and PenSubclass uniqueInstance will return the unique pen instance.
If you do the same with an instance variable in the class side, Pen uniqueInstance will return the Pen unique instance and PenSubclass uniqueInstance will return the PenSubclass unique instance.
Here goes, I found bits of information here and there.
Managed to find a good explanation here, pasted in a few lines for reference purposes. People should read the entire column.
http://esug.org/data/Articles/Columns/EwingPapers/cvars&cinst_vars.pdf
Classes that use class variables can be made more reusable with a few
coding conventions. These coding conventions make it easier to create
subclasses. Sometimes developers use class variables inappropriately.
Inappropriate use of class variables results in classes that are
difficult to subclass. Often, the better implementation choice for a
particular problem is a class instance variable instead of a class
variable.
What are class variables? Classes can have
• class
variables, and
• class instances variables.
Class variables are
referenced from instance and class methods by referring to the name of
the class variable. Any method, either a class method or an instance
method can reference a class variable.

How to initialise a class variable?

I know that for an instance variable all I have to do is put it inside the initialise method in the instance side and assign it a default value. But how I do this for class variable ? I tried to create an initialise method at class side but it did not give my variable a default value so I had to do this in one of my methods
pythonString ifNil:[pythonString := '']
But I don't like this approach.
I also found this for squeak , http://forum.world.st/Howto-initialize-class-variables-td1667813.html again I don't like this approach either. Is there a proper way of doing this. In Python it was fairly simple case of assignment why is it so cryptic for Pharo ?
First of all I hope that you are talking about instance variable of a class object (not a thing that you define on instance side as "class variable").
initialize is working, but it's being run upon instance creation. And instance (a class object) exists already when you define initialize method.
So when you define your class for the first time, you should run it by yourself e.g. YourClass initialize, bun later each time you load your class into system it should be initialised.

Init a class with one argument using objc_msgSend [duplicate]

This question already has answers here:
Create a subclass of a class using parent's init - from another class
(2 answers)
Closed 8 years ago.
EDIT: Yes, I did it wrong. It's well possibly knowing the init method by using a protocol on class level. This is something I rarely do, so that didn't come to my mind at first (see linked question about my answer to it using a protocol). So yes, this question is broken. As bbum said, there should be absolutely no reason to do this.
Background to my question in [1].
For a design reason (data mapper pattern) I need to initialize classes which I know are subclasses of a certain base class (ManagedEntity). I assert for this once - then later I want to create as many instances, and as fast as possible (I'm programming for iOS). However, since the class where I need to create the concrete instances in doesn't know any of the model classes, the meta class stored and used to create entity instances of is just known to be of type Class.
Long story short: I can't simply use [[[_EntityClass] alloc] initWithBlah:something], since EntityClass is unknown, just known as type Class there, hence the init method initWithBlah is unknown of course - but I know it must exist (it must be by design a subclass of the base class, which is asserted once when the mapper is initialized).
So in order to create instances of the unknown class with the init method that I know it exists, I need to construct a method invocation. This should call the initWith:something selector on the unknown class and create an instance of it.
I think I should use objc_msgSend rather than NSInvocation, because the latter is supposed to be an order of magnitude slower [2]. The init method is supposed to not change, and requires one argument.
So... What would be the equivalent to:
ManagedEntity *newEntity = [[ManagedEntity] alloc] initWithEntityDescription:_entityDescription];
with objc_msgSend?
[1] Create a subclass of a class using parent's init - from another class
[2] http://www.mikeash.com/pyblog/performance-comparisons-of-common-operations-leopard-edition.html
Better:
Class klass = NSClassFromString(className);
id newEntity = [[klass alloc] initWithEntity:entity insertIntoManagedObjectContext:ctx];
There is no reason to use objc_msgSend() directly when you have a fixed selector. You can always call the selector directly using the normal syntax. Worst case, you might have to type-cast the return value of one of the calls.
The only requirement is that the compiler has seen the declaration of initWithEntity:insertIntoManagedObjectContext: sometime prior to compiling the above call site.
Example:
#interface NSObject(BobsYourUncle)
- (void)bob:sender;
#end
...
Class klass = NSClassFromString(#"NSManagedObject");
[[klass alloc] bob:nil];
The above compiles just fine. Not that I'd recommend hanging random definitions off of NSObject. Instead, #import the abstract superclass's declaration (which should contain the selector declaration).
id cls = NSClassFromString(className);
id alloced_cls = objc_msgSend(cls, #selector(alloc));
id newEntity = objc_msgSend(alloced_cls, #selector(initWithEntity:insertIntoManagedObjectContext:), entity, ctx);
return newEntity;

How to access one private instance variable in ios

I have a question want to consult you。The following:
class A is from the ios framework, one instance variable B of the Class A is not public, can i through the getter methods defined in the category C to access instance variable B ?the category C is custom for the class A 。
example, the instance variable _viewDelegate of the class UIView.can I create a category C of the UIView to access instance variable _viewDelegate? if define method -(UIViewController*)viewDelegate in the category C;
ThankYou,First !
If the question is "can I access private variables via Category" then the answer is - depends.
First of all, the variable must be defined in the .h file.
If it is, then if marked as readonly, you can only read it. For example:
#property(nonatomic,readonly) somePropertyOfClassA
Otherwise, you can read/write to the property directly without a getter/setter, for example #property(nonatomic) NSInteger tag
The UIView _viewDelegate is marked as #package which means that the member is accessible only from the framework in which it is defined, which is the ios framework.

Class Method not working in objective c

In my code I have a class called 'ProfileShareViewController', In which I have imported another class I have created called 'OwnProfileData', And I have also created an Instance of that class (class = OwnProfileData) as a property Of 'ProfileShareViewController' and synthesized it (instance called 'OwnProfile').
In another class I have called 'EditProfileViewController', I have imported the 'ProfileShareViewController', and now I am trying to change a property of the OwnProfile object from the ProfileShareViewController within the EditProfileViewController class.
For some reason that doesn't work. I have Tried typing:
[[ProfileShareViewController ownProfile] setName:#"Ido"];
(The property I am trying to set is Name, and as it is synthesized in OwnProfileData, I am using 'setName').
This doesn't work and I get the warning: "No known class method for selector 'ownMethod'.
Any Idea as for why that might happen and how I can fix this?
Thanks for your comments! Any support is highly appreciated!
You need an instance of ProfileShareViewController, because ownProfile is an instance property, not an class method. Read about the differences between classes and instances.
Or did I misunderstood smth?