I am trying to implement a private method which takes an NSMutableDictionary and a Player object as its parameters. As it is a private method, the only place that it exists is in the ".m" file.
It is declared as
-(void) incrementScore: (NSMutableDictionary*) scoreboard forPlayer: ( Player* ) player {
and I call it as follows :
[ self incrementScore:deuceScore forPlayer:p];
However,it won't compile - I get
may not response to message -incrementScore:forplayer
I'm not sure where my error lies - do I need to declare the method in the ".h" file, or elsewhere in the ".m" file, or have I just got the syntax completely wrong?
The compiler needs to find a declaration for your method somewhere before you use it. This be done in three way:
Declare the method in the (public) #interface for the class in its .h file .
Declare the method in a class extension (a semi-private #interface, usually at the top of the .m files).
Define the method somewhere in the #implementation before your first use of it.
This is only a warning not a compile error... (if you changed preferences to treat all warnings like error it'll be a compile error).
Probably the line calling the method is above (in the .m file) the declaration method. Move the method just below #implementation directive, or above the method with the calling line. The warning/error should disapper.
Related
I'm trying to use the userVisibleDateTimeStringForRFC3339DateTimeString method that Apple documents here.
So first I created a separate class called jhsDateFormatter and first modified it from
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString;
to
- (NSMutableString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSMutableString *)rfc3339DateTimeString :(NSString *)rfc3339DateTimeFormatString;
so I could pass in a second parameter, which would be the desired date format string.
I then imported this new class into my view controller.m:
#import "jhsDateFormatter.h"
and called the method this way:
predicateMutableString = [userVisibleDateTimeStringForRFC3339DateTimeString:dateHolderMutableString :#"yyyy'-'MM'-'dd'"];
predicateMutableString is defined in the viewController.h and synthesized in the .m.
I got a build error: use of undeclared identifier 'userVisibleDateTimeSTringForRFC3339DateTimeString
So I commented out my modified version and used the original code and method signature in my class file:
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString;
and called it this way:
I'm not sure why the method call isn't being accepted. I think I've matched up the data types.
Please let me know your ideas of what is awry.
Thanks
You probably want to declare your method like this:
+ (NSMutableString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSMutableString *)rfc3339DateTimeString formatString:(NSString *)rfc3339DateTimeFormatString;
(Note the + at the start, and the fact that the second argument is now named - it was blank in your code, which is valid but weird.)
Then you'd call it like this:
predicateMutableString = [jhsDateFormatter userVisibleDateTimeStringForRFC3339DateTimeString:dateHolderMutableString formatString:#"yyyy'-'MM'-'dd'"];
Where jhsDateFormatter the new class you've made.
In your example code, you're not calling the method on any object, which is why the compiler is complaining.
I am new to iOS. I have defined the following method in a .m file and need to add its name to the .h file.
-(Boolean) addBookFromArrayOne:(Book*)bookOne bookTwo:(Book*)bookTwo mergeByThisField:(NSString*)field sortDescending:(Boolean)
This post Method Syntax in Objective C was very helpful in learning the Objective C method syntax. The answer
in Objective-C, the name of a method is composed of all of the
portions of the declaration that are not arguments and types. This
method's name would therefore be: pickerView:numberOfRowsInComponent:
was particularly helpful.
But it does not show how to generalize to multiple parameters.
I took a guess at the syntax in the header file but am clearly getting it wrong:
#property Boolean addBookFromArrayOne:bookTwo:mergeByThisField:sortDescending;
Can somebody show me how to define this method name in the header file?
Just copy method declaration from implemetation to header
-(Boolean) addBookFromArrayOne:(Book*)bookOne bookTwo:(Book*)bookTwo mergeByThisField:(NSString*)field sortDescending:(Boolean)desc;
Don't use a property to declare a method; to declare a method in your header file just write:
-(Boolean) addBookFromArrayOne:(Book*)bookOne bookTwo:(Book*)bookTwo mergeByThisField:(NSString*)field sortDescending:(Boolean)descending;
In an .h file I have the following line (outside of any #interface block):
static NSMutableDictionary *dictLookup;
In the corresponding .m file I try to initialize that static in the init method of the class:
dictLookup = [NSMutableDictionary dictionary];
dictLookup setValue:#"Hello?" forKey:#"Goodbye"];
However, when I insert breakpoints and do checks, dictLookup never becomes anything other than nil.
Also, I get a bizarre warning "Unused variable dictLookup" at compile time. Bizarre because if I delete the static declaration, then I get an "Undeclared identifier" compiler error at the lines in the init method.
I've since discovered there are better ways of doing what I want. But what was going on here? (1) Why can't I set dictLookup to anything?
Some sources seem to say that in C a static variable can only be used in the file in which it is declared. (2) If so, then why doesn't compiler fail with an error in the .m file? Given (1) that would seem to be the logical thing to design the compiler to do.
And (3) When I designed a new 'test' project from scratch, with a new .h/.m file combo like the one described, I WAS able to set dictLookup and insert keys. Why could accoutn for this difference?
When you put a declaration of a static variable in a .h file, it gets re-defined in every .m file from which the header is included. A brand-new variable will be created in each file, with the same name.
This is not an error in the .m file: the variable is local to that file, and invisible to the linker, so there are no "multiple definitions" error.
That's because your test project used a single .m file.
Some sources seem to say that in C a static variable can only be used in the file in which it is declared.
That is absolutely correct: a static variable is very much like a file-scoped global variable, it should be defined in the .m file. If you want to share a variable, it needs to be a global then. Declare it in the header with the extern keyword, like this
extern NSMutableDictionary *dictLookup;
and then define it in one of the .m files like this:
NSMutableDictionary *dictLookup;
sometimes I get the Warning for example :
Instance method '-PushBottom' not found (return type defaults to 'id')
because the function PushBottom is under this function (where it called) declared.
For fixing this warning I put the function PushBottom about this function (where it called).
Is there any way to get fix this warning without put function PushBottom about the function where it called?
you could declare the push button in the .h file
-(void)PushButton;
or -(IBAction)PushButton:(id)sender;
depending on what the function is.
just declare it in the .h file above the #end and u should be good to go
I would define it in your .m file (the .h file is your public api file and unless you want it to be public, I wouldnt put it in there as suggested in another answer).
Above the #implementation In your .m file write:
#interface yourClassName()
-(void)yourMethod;
#end
I am working my way through some Objective-C code that I did not write and have found a variable declaration style that I am unfamiliar with. Can anyone tell me the scope of the variable 'myVar' in the class implementation below? Note that this appears in the '.m' file and not the interface declaration.
#implementation MyClass
#synthesize ivar1, ivar2;
NSString* myVar; // <- What is the intent?
- (id)init {
...
#end
To me the intention appears to be similar to that of a member variable. What are the advantages of declaring a variable in this way instead of using an ivar in the #interface declaration?
It's just a plain old global variable. There's only one instance of it, and it can be accessed by any code within the same file translation unit (the final file you get after running the preprocessor). Other translation units (that is, other .m files) can also access that global variable, but in order to do so, they need to use an extern statement:
extern NSString *myVar;
extern says "this is the name of a global variable, but it's defined in a different translation unit". The linker resolves all of the extern declarations at link time.
a poorly named global variable...
I'm not too experienced in ObjC but I'd say that is a global.