I have two classes,In classA I create a variable that I need to use in classB ,
should i use property ?
is there anyone to explain me easier ,how to set StringValue of variable in one class to the textfield of another class?
thanks
Yes, and yes:
http://www.cocoacast.com/?q=node/103
The simple answer is Yes, use properties, that is what they are for: a simple way of exposing the state of an object to other objects.
The longer answer is that Objective-C 2.0 properties are just a wrapper around the concept of Key-Value-Coding and Key-Value-Observing (KVC/KVO).
It is well worth reading the documentation for these as the concept is fundamental to the way that Cocoa works and understanding them early on in your learning process will save you a lot of trouble in the future.
And, since you will be passing object references around I might as well add a link to the Memory Management Programming Guide which will help you correctly apply the proper memory management attributes to your #property declarations.
Related
I am writing a class that has many class variables. So I am declaring the variables as static in my .m file and before #implementation statement, with setters and getters for them as class methods. Is it a good idea to do this for lets say more than 10 class variables? Or is there a better alternative to do this?
Without more information that's a tough call. Technically working - yes.
I know many people don't like singletons, but maybe this is one of the good use cases for it?
Maybe you find that configuring one of those classes, or now objects, really doesn't have to be a singleton at all?
Just because there is only one instance of a given class doesn't mean in cannot be a "normal" class.
Class variables often mystify your state all over your code base and make debugging and code reuse a pain. Let alone multi threading.
Edit: Given your usecase as described in a comment to another answer, I'd go with a singleton, i.e. a 'SoundPlayer' class. '[[SoundPlayer sharedInstance] playCoolSound];' is easy, and you get proper instance variables there, too. And you can always exchange it for another class if needed (think test cases etc.).
Well, I can guarantee that the computer will have no problem with it. They're pretty good with large numbers.
Without knowing more about your situation, it's hard to say. It does sound, though, like you're potentially going about it in a way that isn't optimal. It's good that you followed your intuition and are asking about it. Perhaps you should modify your question (or ask a new one and link it here) to talk about what you really want to accomplish, and then we can help.
I was reading the sources for LibGDX, and I saw that there are too many public fields inside classes. So I was wondering, why? Is there any advantage instead setting up the typical setters/getters for that fields?
I know I should avoid direct accessing to class' fields, but if a guy like the author of LibGDX do it, I'm starting to doubt about "what are the best practices".
You don't have to know the implementation of this class, so they make your code simpler. Also, you are sure some variables you do not want them to be changed adheres to the rules. So they make sure you don't mess up with your code.
Is there a better way to serialize an ObjC object than using /NSKeyedArchive?
I need to distribute the object through a C++ std:ostream-like object to put on another computer.
The object has over 122 members of various types... for which wants me to
[coder encodeObject: (id) forKey: #"blah"];
for all of them...
Does anyone have a nice Perl Script that will at least write it out? I don't even know if the objects it contains implement which means this could turn into a huge ugly mess since I can't change the source of the object - I'll have to inherit & add the #interface to it...
Or am I being dumb? Apple's guide doesn't help me since archiving to XML won't pass nicely though the ostream.
Is there a better way to do this?
-S!
[Edit 1]
So I've looked at NSCoding, and my classes contain generic NSManagedObjects... which don't conform to <NSCoding>. I've tried using the Aspect Oriented Programing found [here][2] - but this leads to an infinite recursion at runtime. (at least that's what I assume a call stack of 104795 calls to [id encodeWithCoder:] are.
Plus, I can't just add <NSCoding> to the class as I'm loading as its members are objects of an imported framework. I can't just add the protocol to it, which is why I thought the Aspect-Oriented stuff would work.
The last (and terrible) Idea that I have is to subclass every class I need, and implement the protocols there. A huge roadblock I can forsee is if the classes include NSArrays of Classes not exposed in the framework. Then I'm sunk.
Is there a better way to do this? I feel like is a design-time decision, and it's too late now to be adding this capability.
help...
PS
this whole anti-spam thing with limiting links suck. How am I supposed to ask intelligent questions!?
[2]:
http:/ /davedelong.com/blog/2009/04/13/aspect-oriented-programming-objective-c#comment-803
Using NSCoding is definitely the right way to go. If you don't want to type all the NSCoding-related boilerplate (and I agree, it's a pain), take a look at Accessorizer which can write it for you.
I've been trying to implement a simple component-based game object architecture using Objective-C, much along the lines of the article 'Evolve Your Hierarchy' by Mick West. To this end, I've successfully used a some ideas as outlined in the article 'Objective-C Message Forwarding' by Mike Ash, that is to say using the -(id)forwardingTargetForSelector: method.
The basic setup is I have a container GameObject class, that contains three instances of component classes as instance variables: GCPositioning, GCRigidBody, and GCRendering. The -(id)forwardingTargetForSelector: method returns whichever component will respond to the relevant selector, determined using the -(BOOL)respondsToSelector: method.
All this, in a way, works like a charm: I can call a method on the GameObject instance of which the implementation is found in one of the components, and it works. Of course, the problem is that the compiler gives 'may not respond to ...' warnings for each call. Now, my question is, how do I avoid this? And specifically regarding the fact that the point is that each instance of GameObject will have a different set of components? Maybe a way to register methods with the container objects, on a object per object basis? Such as, can I create some kind of -(void)registerMethodWithGameObject: method, and how would I do that?
Now, it may or may not be obvious that I'm fairly new to Cocoa and Objective-C, and just horsing around, basically, and this whole thing may be very alien here. Of course, though I would very much like to know of a solution to my specific issue, anyone who would care to explain a more elegant way of doing this would additionally be very welcome.
Much appreciated, -Bastiaan
I don't think that sending the container object all of its components' messages is what Mick West was suggesting--that doesn't help to remove the idea of a "monolithic game entity object".
The eventual goal is to have the components communicate directly with one another, with no container object at all. Until then, the container object acts as glue between old code that expects a single object for each game entity and the new component-to-component system.
That is, you shouldn't need to use message forwarding at all in the final product, so ignoring the warnings, or declaring variables as id for now to quiet them, isn't all that ugly. (The plan as laid out by the article is to eventually remove the very code that is causing your warnings!)
A simple way to have those warnings disappear would be to declare the instance variables of type id
That way the compiler assumes you know what you're doing regarding the type of the object and that the object will respond to whatever messages you send to it, or if it doesn't you don't care.
Override your GameObject's -respondsToSelector: method. Your implementation should in turn send a respondsToSelector: message to each of its instances, and return YES if any one of them returns YES.
You can use type of id - or you could invoke the methods using performSelector methods, or create an NSInvocation if the arguments are complex. This is all just a way of getting around compiler warnings, however. If your objects respond to several methods, then possibly declaring a protocol might help, although the same caveat applies.
Another option if I understand the problem correctly is to implement a protocol. This is link an interface in java and variables can be declared like this:
id anObjectRef
That way the compiler understands that the object referred to by anObjectRef conforms to the protocol.
There are also methods that can tell you if an particular object conforms to a specific protocol before you cast or assign it.
I had a bunch of objects which were responsible for their own construction (get properties from network message, then build). By construction I mean setting frame sizes, colours, that sort of thing, not literal object construction.
The code got really bloated and messy when I started adding conditions to control the building algorithm, so I decided to separate the algorithm to into a "Builder" class, which essentially gets the properties of the object, works out what needs to be done and then applies the changes to the object.
The advantage to having the builder algorithm separate is that I can wrap/decorate it, or override it completely. The object itself doesn't need to worry about how it is built, it just creates a builder and 'decorates' the builder with extra the functionality that it needs to get the job done.
I am quite happy with this approach except for one thing... Because my Builder does not inherit from the object itself (object is large and I want run-time customisation), I have to expose a lot of internal properties of the object.
It's like employing a builder to rebuild your house. He isn't a house himself but he needs access to the internal details, he can't do anything by looking through the windows. I don't want to open my house up to everyone, just the builder.
I know objects are supposed to look after themselves, and in an ideal world my object (house) would build itself, but I am refactoring the build portion of this object only, and I need a way to apply building algorithms dynamically, and I hate opening up my objects with getters and setters just for the sake of the Builder.
I should mention I'm working in Obj-C++ so lack friend classes or internal classes. If the explanation was too abstract I'd be happy to clarify with something a little more concrete. Mostly just looking for ideas or advice about what to do in this kind of situation.
Cheers folks,
Sam
EDIT: is it a good approach to declare a
interface House(StuffTheBuilderNeedsAccessTo)
category inside Builder.h ? That way I suppose I could declare the properties the builder needs and put synthesizers inside House.mm. Nobody would have access to the properties unless they included the Builder header....
That's all I can think of!
I would suggest using Factory pattern to build the object.
You can search for "Factory" on SO and you'll a get a no. of questions related to it.
Also see the Builder pattern.
You might want to consider using a delegate. Add a delegate method (and a protocol for the supported methods) to your class. The objects of the Builder class can be used as delegates.
The delegate can implement methods like calculateFrameSize (which returns a frame size) etc. The returned value of the delegate can be stored as an ivar. This way the implementation details of your class remain hidden. You are just outsourcing part the logic.
There is in fact a design pattern called, suitable enough, Builder which does tries to solve the problem with creating different configurations for a certain class. Check that out. Maybe it can give you some ideas?
But the underlying problem is still there; the builder needs to have access to the properties of the object it is building.
I don't know Obj-C++, so I don't know if this is possible, but this sounds like a problem for Categories. Expose only the necessary methods to your house in the declaration of the house itself, create a category that contains all the private methods you want to keep hidden.
What about the other way around, using multiple inheritance, so your class is also a Builder? That would mean that the bulk of the algorithms could be in the base class, and be extended to fit the neads of you specific House. It is not very beautiful, but it should let you abstract most of the functionality.