Objective-C newbie question re: global instances - objective-c

Is there a way to make one (1) instance of an object and share that instance between different files?
For instance, I have Class "A" (created in fileA.m), and I create an instance of it in a different file (fileB.m). Now, I want to use that same instance created in fileB.m in several other files (fileC.m and fileD.m) so I can share ivar's (such as a database).
How do I do that?

You would want Class A to be a singleton. Here's a great guide to help you out with that.

You need to use a static class method as a constructor which will return a singleton instance of the object you're looking for.

Related

How to get created objects (instances) in VB.net

I just did some researches on google but I didn't find an answer to my question.
Is there a way to get the list of the active objects (instances of classes) at runtime?
In my application I need to have single instance classes that needs to be used by different running forms but if I create an instance in the form A, ho do i get control of the same instance in the form B?
Thank you
Actually, your question has 2 parts:
1. How to create single-instance objects.
2. How to have the same object accessible from different forms.
Fortunately for you, there is a solution to both of these problems in one simple and common design pattern called Singleton.
Classes written in the Singleton pattern can only have a single instance, and as you are about to see, as a side effect, this instance is accessible through the entire application.
The simplest way to use the singleton design pattern is this:
Public Class SingletonClass
Private Shared _instance As SingletonClass
Public Shared Function GetSingletonClass() As SingletonClass
If isNothing(_instance) Then
_instance = New SingletonClass()
End If
Return _instance
End Function
Private Sub New()
'' Create the instance here
End Sub
End Class
As you can see, since the constructor is private, it is not accessible from anywhere outside of class SingletonClass, and since class SingletonClass holds a static reference to it's instance, it means that every time you write SingletonClass.GetSingletonClass() in your application you get the same instance.
This design pattern solves both of your problems in a simple, elegant, and well known architecture.
Update
I've recently read a great article about different ways to implement singleton patterns. It turns out that my above specific implementation is not so good, as it is not thread safe. The code examples in this article are C#, but it should be very easy to change them to VB.Net. If you are using .Net 4 or higher, I would recommend going with the 6th version - using .NET 4's Lazy type.
It is both thread safe and lazy loading, two advantages that the implementation I've written doesn't have.

Static method (which isn't class method) in objective C

While reading THIS question and accepted answer for the question, I was unable to get the difference between these two types of methods. Actually got the point by reading the example, but then, I was not able to write my own static method.
I tried googling create static method in objective c static methods
Which returned me links to THIS and THIS question. But, the example here are CLASS methods as per the first link in the question. Which is confusing me.
Can anyone here show me how do I create a static method which is not a class method ?
Any light on this would be appreciated.
The problem you are having is the following - there are no static methods in Obj-C, that's why you cannot create them.
The difference between static and class methods is a difference between language concepts. You can find static methods in languages like Java or C++, you will find class methods in languages like Obj-C and Ruby.
The principal difference is that
Static methods are shared between all instances (this doesn't exist in Obj-C). They are dispatched statically (at compile time) depending on the type of the variable.
Class method is a method on a class. In languages like Obj-C and Ruby a class itself is an instance of another class (metaclass). Using + before a method declaration means the method will be defined on the class. Technically, it's just an instance method, just on a different object.
Don't worry if you don't understand the concept of class method perfectly, it takes time. To simplify, you can consider it as a method shared between instances, but it can be overriden in subclasses.

How are classes instances handled through views?

Im really new to iOS Programming. Although I read lots of books and material I cant really find out an answer to a simple question.
I know C++ language, and I understand how variables are handled through the functions. What I really dont understand in iOS programming is how this thing is done.
For example, in C++ I create a class instance in the main function. When I call another function and I want to share this class instance all you need to do is:
myFunction(&myClassInstance);
When I switch views in iOS programming (and therefore classes) I dont know how to send a class instance that was created before. For example, if I am on the firstView of my program, and I switch to the secondView, how can I send the variables that I was currently using?
If I need to create an instance of a class that is going to be used though ALL the program, where I should create it? In C++ I would create it in the main function, in iOS Programming, where should I create it?
Thanks!
First of all what I understand from your question is
"In C++ when we have to pass instance of some other class we use reference argument. What is option in Objective C?"
So, Objective C is build up on C. So you can pass instance using pointer. like...
-(void)myFunction:(MyClass*)myClassInstance;
Other question is
"If I need some instance in all the program then where should I create?"
You can use AppDelegate class to hold that instance. Because instance of AppDelegate it accessible from all your program.
And you can create instance in "didFinishLaunchingWithOption"

specific questions about scope and property reference in actionscript 3

I've been battling with AS3 for a little while now, and I'm working on a simple application using only actionscript and the FlashDevelop/flex-compiler combo. I've hit a bit of a wall in my fledgling OOP understanding, and I'm wondering whether someone might be able to point me in the right direction. I have genuinely read several books, and spent many hours reading online tutorials etc, but something's just not clicking!
What's baffling me is this: When something is declared 'public', according to what I read, it is therefore available anywhere in the application (and should therfore be used with care!) However, when I try to use public properties and methods in my program, they most definitely are not available anywhere other than from the class/object that instantiated them.
This leads me to conclude that even if objects (of different class) are instantiated from the same (say 'main') class, they are not able to communicate with each other at all, even through public members.
If so, then fair enough, but I've honestly not seen this explained properly anywhere. More to the point, how do different objects communicate with other then? and what does Public actually mean then, if it only works through a direct composition hierarchy? If one has to write applications based only on communication from composer class to it's own objects (and presumably use events for, er, everything else?) - isn't this incredibly restrictive?
I'm sure this is basic OOP stuff, so my apologies in advance!
Any quick tips or links would be massively appreciated.
There are different topics you are covering in your question. Let me clarify:
What does the modifier public mean?
How can instances of the same class communicate to each other?
--
1.
In OOP you organize your code with objects. An object needs to be instantiated to provide its functionality. The place where you instantiate the object can be considered as the "context". In Flash the context might be the first frame, in a pure AS3 movie, it might be the main class, in Flex it could be the main mxml file. In fact, the context is always an object, too. Class modifier of your object public class MyClass tells your context whether it is allowed to instantiate the object or not. If set to internal, the context must live in the same directory as the class of the object. Otherwise it is not allowed to create a new object of the class. Private or protected are not valid class modifiers. Public class ... means that any context may create an object of that class. Next: Not only instantiation is controlled by these modifiers but also the visibility of a type. If set to internal, you cannot use an expression like var obj : InternalType in a context that does not live in the same directory as Internal type.
What about methods and properties? Even if your context is allowed to access a type, certain properties and methods might be restricted internal/protected/private var/method and you perhaps are not able to invoke them.
Why we're having such restrictions? Answer is simple: Differnent developers may develop different parts of the same software. These parts should communicate only over defined interfaces. These interfaces should be as small as possible. The developer therefore declares as much code as possible to be hidden from outside and only the necessary types and properties publicly available.
Don't mix up with modifiers and global properties. The modifier only tells you if a context is allowed to see a type or method. The global variable is available throughout the code. So even if a class is declared to be public, instances of that class do not know each other by default. You can let them know by:
storing the instances in global variables
providing setter such as set obj1(obj1 : OBJ1) : void where each object needs to store the reference in an instance variable
passing the object as method arguments: doSomething(obj1 : OBJ1)
Hope this helps you to more understand OOP. I am happy to answer your follow up questions.
Jens
#Jens answer (disclaimer: I skimmed) appears to be completely correct.
However, I'm not sure it answers your question very directly, so I'll add a bit here.
A public property is a property of that class instance that is available for other objects to use(function: call, variable: access, etc). However, to use them you must have a reference (like a very basic pointer, if that helps?) to that object instance. The object that instantiates (creates, new ...) that object can take that reference by assigning it to a variable of that class type.
// Reference is now stored in 's'
public ExampleClass s = new ExampleClass();
If you'd like to, you do have the option of making a static property, which is available just by knowing the class name. That property will be shared by all instances of that class, and any external class can refer to it (assuming it's public static) by referring to the class name.
A public property is referred to by the reference you stored.
//public property access
s.foo
s.bar(var)
A static property is referred to by the class name.
//static property access
ExampleClass.foo
ExampleClass.bar(var)
Once you've created the instance, and stored the reference, to an object, you can pass it around as you'd like. The below object of type OtherExampleClass would receive the reference to 's' in its constructor, and would have to store it in a local variable of its own to keep the reference.
public OtherExampleClass s2 = new OtherExampleClass(s);

Where in the source code should I create an object?

I want to create an object from a class that I have created. What I have been trying is to create the object (which can be testingclass) from a method of another class, which I will call classexample, but it says that testingclass is undeclared, which I assumed meant that it was out of the scope of the method, and the method couldn't access it. My program doesn't have a main function like in c or anything so I don't really understand where to put code that involves different objects if it can't go in methods.
Do you have the header file of the testingclass and did you import it to the file that create that object? That's all I can guess for your problems with very little information for very strange problem.