Objective-C - Calling Methods in objects. - objective-c

I'm new to Objective-C, and I seem to be struggling with accessing a method of an object I created. I'm checking out the documentation, but I'm not entirely sure that this is a job for a delegate.
For example I have an object (1) that creates another object (2). I can access a method of the object (2) after I create it, but I can't access it from a method of object (1). I get a error that the object was not defined in this scope.
If anyone can help I greatly appreciate it. I just need a nudge in the right direction so that I can at least get a grasp on how to think about the interaction between the objects.

Turns out I did not define the object in #interface. This caused my methods to not have access to it. So for anyone with a similar issue, make sure to define your i-vars in the interface and not init :)

Related

Do Objective-C objects get their own copies of instance methods?

I'm new to Objective-C and was wondering if anyone could provide any information to clarify this for me. My (possibly wrong) understanding of object instantiation in other languages is that the object will get it's own copies of instance variables as well as instance methods, but I'm noticing that all the literature I've read thus far about Objective-C seems to indicate that the object only gets copies of instance variables, and that even when calling an instance method, program control reverts back to the original method defined inside the class itself. For example, this page from Apple's developer site shows program flow diagrams that suggest this:
https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW1
Also in Kochan's "Programming in Objective-C", 6th ed., pg. 41, referring to an example fraction class and object, the author states that:
"The first message sends the setNumerator: message to myFraction...control is then sent to the setNumerator: method you defined for your Fraction class...Objective-C...knows that it's the method from this class to use because it knows that myFraction is an object from the Fraction class"
On pg. 42, he continues:
"When you allocate a new object...enough space is reserved in memory to store the object's data, which includes space for its instance variables, plus a little more..."
All of this would seem to indicate to me that there is only ever one copy of any method, the original method defined within the class, and when calling an instance method, Objective-C simply passes control to that original copy and temporarily "wires it" to the called object's instance variables. I know I may not be using the right terminology, but is this correct? It seems logical as creating multiple copies of the same methods would be a waste of memory, but this is causing me to rethink my entire understanding of object instantiation. Any input would be greatly appreciated! Thank you.
Your reasoning is correct. The instance methods are shared by all instances of a class. The reason is, as you suspect, that doing it the other way would be a massive waste of memory.
The temporary wiring you speak of is that each method has an additional hidden parameter passed to it: a pointer to the calling object. Since that gives the method access to the calling object, then it can easily access all of the necessary instance variables and all is well. Note that any static variable exists in only a single instance as well and if you are not aware of that, unexpected things can happen. However, regular local variables are not shared and are recreated for each call of a method.
Apple's documention on the topic is very good so have a look for more info.
Just think of a method as a set of instructions. There is no reason to have a copy of the same method for each object. I think you may be mistaken about other languages as well. Methods are associated with the class, not individual objects.
Yes, your thinking is more or less right (although it's simpler than that: behind the scenes in most such languages methods don't need to be "wired" to anything, they just take an extra parameter for self and insert struct lookups before references to instance variables).
What might be confusing you is that not all languages work this way, in their implementations and semantically. Object-oriented languages are (very roughly) divided into two camps: class-based, like Objective-C; and prototype-based, like Javascript. In the second camp of languages, a method or procedure really is an object in its own right and can often be assigned directly to an object's instance variables as well - there are no classes to lookup methods from, only objects and other objects, all with the same first-class status (this is an oversimplification, good languages still allow for sharing and efficiency).

Kindly anyone explain custom delegates in objective-c with a simple coding example? Step By Step [duplicate]

This question already has answers here:
How do I create delegates in Objective-C?
(20 answers)
Closed 8 years ago.
I am a beginner so I need a simple example of custom delegates. How we can create, use and call? Step by step explanation would be appreciated. i know that the question asked many times before but still confused.
Thanks in advance.
A delegate is little more than a property or ivar to another object that may be called to perform specific methods.
Normally, a Protocol is created defining optional and required method declarations for this delegate object and the delegate object implements at least the required ones.
This API contract ensures you can rely on delegating some business logic to the delegate object.
Your object does not need to know how the delegate will make decisions.
It just sends messages to the delegate and can rely on the results if any are returned.
The delegate does not need to know the precise object it is delegating for unless the method includes it as an argument.
The idea is that the delegate can know things the other object doesn't ever need to know about.
Essentially it makes delegates tend to be controller classes but not always.
It enables objects such as views and controls to be generic and reusable.
It also enables event driven programs with ideas like "hey delegate should I do this now?" Or "hey delegate what kind of thing should I display? X, Y or Z?" Or "delegate give me an object that makes sense to you under ABC criteria"
NSMenuDelegate is a great example NSApplicatonDelegate and UIApplicationDelegate are great examples.
NSTableView and UITableView (and other collection views) also give great delegate examples. They also show how this pattern can have other names containing things like "DataSource" for doing more specific things like providing data for the collection.

LLDB: Show all objects with a pointer to an object in memory

So, at a breakpoint, I have a random object instance. I want to figure out which objects have a pointer to this object. Is there a way to see this in the debugger console? Maybe something that shows me all the objects that have a retain on the object?
Example: I have a NSViewController instance and I want to see all the other objects that hold a pointer this view controller instance. This would be helpful because it would allow me to see the view controller hierarchy that is encapsulating my instance.
Just a crazy thought I had that would really help at times.
In lldb, use command script import lldb.macosx.heap to install some memory-searching functions. The ptr_refs command should be able to do what you want; use ptr_refs --help to learn more.
Not an efficient solution, or applicable in all cases, but you could encapsulate the object you're looking for in an accessor method on one of your classes, and put a breakpoint inside. By stepping through the end of the accessor method, you can eventually see all the call points.
Alternatively, you can remove the definition of the variable, and the compiler will spit out a ton of errors, each will also be a call to this object.
I'd suggest using ARC if you're not already. Ideally your code wouldn't be messy enough that you wouldn't be able to identify references by reading through the code, ARC can help a little bit in that department

I'm having trouble applying a solution found in SO in my iOS app. Can someone show me the big picture?

in the Stack Overflow posting:
How do I create a global UIManagedDocument instance per document-on-disk shared by my whole application using blocks?
Alan asked how to create a global UIManagedDocument to be used throughout his entire app. He provided code slices of his attempt. Kevinpo provided an answer which made perfect sense to Alan.
But I started out with the same problem, and can't make heads or tails out of their collective postings.
Specifically:
Alan's code references an object called managedDocumentDictionary,
but does not explain how to create it so I get an 'undeclared
identifier' compilation error.
Alan starts out stating that he wants to create a helper method to
retrieve a UIManagedDocument, yet throughout both his and Kevin's
code, neither actually show defining a helper method with .h and .m
files.
So, if possible, can anyone make sense of what they are saying and help me understand how it all fits together? Perhaps:
A helper Class definition,
How does one get the ball rolling, i.e., where do I initially create this UIManagedDocument,
Once created, How do I get the document in other TableViewControllers?
A sample of where this should be invoked - in the AppDelegate? or each TableViewController?
Maybe even a sample project?
Thanks to all for any interpretations you can offer.
That post shows how to access a document, based on a name. The dictionary is a mapping from names to UIManagedDocument instances. Thus, he can ask for document #"Foo" and the code will go look up #"Foo" in the dictionary. If it is there, the UIManagedDocument will be returned. If it is not there, then a new one will be created and placed in the dictionary (and the passed-in completion block will be called).
His question was basically, how to pass a completion block to the function, and have that function call the completion block he passed in.

Is "self" the only way to call another method of the same instance from an instance method?

I have been reading Apple's "The Objective-C Programming Language", I noticed that if you have say,
Class A /w methods MA and MB, where MA calls MB with "[self MB]".
If you subclass Class A, lets call it SubClass A, and over-ride MB. Made an instance of SubClass A and call MA, it will use the new MB. Now if you call "[super MA]" instead, it will still use the new MB.
Am I correct?
So I was wondering if "self" is the "right" way to go about this, calling other methods in the same instance, or if it's only used for special situations like covering initializers.
Yes, self is the right way to send a message to the same instance that the method is executing on.
One of the big things to keep in mind about the object model of languages like Objective-C is that, conceptually, you are not "calling methods" — you're sending messages. You're telling the object what to do, not how to do it. You shouldn't normally have to think about what precise method will execute when you send a message — the object is expected to respond appropriately. So if somebody has overridden this "MB" method, presumably the new behavior is how he wants the object to respond when it gets an "MB" message. If somebody has overridden "MB" such that it is no longer usable the way the old method was, then that sounds like a bug.
Yes it is.
Using self inside your class is the right way to go if you want to call methods that are in the same class.
'self' represents the current instance of the class where you are using it.
self is the right way to go. Every method in Objective-C can be thought of as "virtual" (in C++ parlance).
In SubClass A, you don't need to make an instance, you can access any function of subClass A by sing self.
making new object makes a new instance, so it reintialize all the property for that instance.
so you can't do any thing right with this.
self always be right for accessing same class methods and property.
Hope now you can understand why self rather than making other new instance.
And [super MA] must call method of class A's MA method, no case in which MB calls for calling MA.
I have test it, there is no bug all OOPs concept follow in objective c you can call super class method by calling method on super keyword.
So Probably you are doing some thing wrong. just check it.