I am currently new to objective c and have came across a problem while making a game I have a custom made object called battleEngine which is an instance variable in my helloWorld scene in cocos2d. That object has an object as an instance variable called plyController which is a PlayerController object. I want battleEngine to have a getter method that returns the plyController object and this code doesn't work:
-(PlayerController*)getPlayerController
{
return plyController;
}
Is there any reason you haven't just declared your player controller object as a property? You could just use the synthesised getter in that case to get the player controller.
Have a look at the documentation on properties.
Also, and I'm afraid I have to say this or they will take my Cocoa-programmer badge away from me, getPlayerController is not a good method name. Methods with get in them are conventionally used to return values in the parameters passed in by reference. The Cocoa Coding Guidelines tells us this, and much more.
Related
I've defined two classes in an m file, the first subclassing UIView and the second UIViewController. The UIViewController is instantiated at some point, and the vc is who instantiates my first class.
the first class implements the touchesEnded method, to simulate a button. when the touchesEnded method is fired in the first class, is it possible to easily call a method defined in the 2nd class, without going into delegates and such?
I tried playing with selectors with no luck
is it possible to easily call a method defined in the 2nd class
Yes, assuming that you are creating an instance of the second class and calling the method on that instance.
Regardless of whether the two classes are subclasses of the same type, or in the same or different files, you need a reference to an instance of that class to call a method on it, or force it to perform a selector.
The proper OO way to do this is with delegates, but you could theoretically do something like pass a reference to view 2 into view 1 when you create the views. If you create them in IB you could create outlets so they reference each other that way.
In short: Yes, it is possible and easy to do, but I can't give you too much in terms of specific code without a more specific example of your situation
I know what a function is, but I am trying to write a few into my project. I have just watched a video series on Objective-C from Lynda.com where I got the idea from.
In the video is it is explained that I can write a function like this:
void declareWebView ()
However if I write it like that into my code, the error comes up and says that my _webView and self (as self.view) are not available.
if I write it like this:
-(void) declareWebView
Then I do not have an issue.
Any ideas on how to get the first one right?
As far as I can tell, I cannot set any parameters with the second way of writing.
The first is as you said called a function. It is part of the C part of Objective-C, and is not connected to objects or classes, so the variable self or any instance variables of an object don't have any meaning. You can pass it variables that are objects though like so:
void my_func(NSString *string, id someObject, int someInt);
void my_func(NSString *string, id someObject, int someInt)
{
NSLog(#"string = %#, someObject = %#, someInt = %d",string,someObject,someInt);
}
The second is a method, and in a method you can access self and, within instance methods, access instance variables. Replacing the "-" in front of the method with a "+" makes it a class method. In a class method you can't access instance variables and self refers to the class itself, not an instance.
Hope this helps!
The first way is written in the language C. The second way is written in Objective-C. Objective-C methods must be declared the second way. You cannot access your object's private members and properties from within a C function directly, but you could use accessor methods on the Objective-C object and call them from a C function.
I am creating a calculator for some cards game. In that game, I am creating a Singleton class to manager the game. It holds the scores, keeps track of where the game is etc...
Now after the app launches, I will ask the user to enter 4 players' names. After that, 4 player objects are instantiated according to their names. I already have an object called "Player", so 4 players will get instantiated with their name, and a score of 0 to start with.
Now I need to store those players in my singleton class. Therefore, I created 4 Player properties in the class. However my question is, under the init method in the Singleton class, in:
if ((self = [super init])) {
// set properties here
}
Where // set properties here is, what do I write? Do I have to do anything with the Players properties over there?
Thank you,
You don't need to do anything except return the shared instance. Usually the singleton's properties are set in whatever class your instantiating it from.
No, you aren't required to do anything with them, although I would probably set them to nil.
Maybe I'm wrong, but doesn't your AppDelegate already serve as a Singleton?
I'm able to make the method for the call [self weaponAttachments:mpk5] but I don't like having to call self. I think [mpk5 weaponAttachments] is more natural and is easier to read.
The problem I'm having is I need to pass in the weapon (mpk5) in order to use it, which I can do with the first method but not with the second one. Does this mean that I need to subclass NSDictionary in order to be able to use a statement like [mpk5 weaponAttachments]? If so, how do I get ahold of the caller "mpk5" so that I can use it inside the method?
EDIT
I apologize for not putting this in the first time but my objective is to have [mpk5 weaponAttachments] return an NSDictionary or NSArray. Right now I have NSDictionary *attachments = [self weaponAttachments:mpk5]; which works but it just doesn't seem like the best approach.
So firstly, your two calls are a little mixed up:
[self weaponAttachments:mpk5] calls the weaponAttachments method, passing in the variable mpk5.
But [mpk5 weaponAttachments] is either asking the mpk5 object to return the weaponAttachments property or is asking the mpk5 object to run a method called weaponAttachments (I'm simplifying here - it's always a method, but if you're using properties you probably won't realise this as Objective-C will create them for you).
These are fundamentally different things.
On to the brunt of your question:
I don't like having to call self
...unfortunately, if you're working in an object-oriented language you're going to have to get used to this. Say I have a class called mySimpleClass, and a method inside that class called doSomething. Writing this:
[mySimpleClass doSomething] would be what we call a static method. Whereas calling [self doSomething] from within an instance of mySimpleClass would be an instance method.
If you're unsure of the difference between static and instance methods you should probably step back and take a look at some of the basic guides out there.
Quick question, my data model is a singleton object and it contains a list of names that I want to archive. My idea is to make the model responsible for loading and saving it's own data. The model's load method will be called by the ViewController's viewDidLoad method and save by the ViewController's applicationWillTerminate. I could do the load/save directly within the ViewController, but this would be messy as the list of names are an instance variable of the model.
gary
You could just load and save in the init and dealloc methods (although it's common to call a save method explicitly). It's a good idea to encapsulate it within the model class. If you're loading from the network you might want to have a separate loadData method or something, rather than doing it from init.
Apple recommends using lazy initialization wherever possible, so I think you're heading down the right path, though you might want to consider making the method name something that looks like a property accessor, e.g. -names rather than -load (especially since there's a class method named +load that means something quite different).