i have trouble with calling method from another class this is my code
this is my method name
-(void)newDetails:(NSString *)name:(NSString *)gender:(NSString *)year:(double) in:(double)out:(NSString *)uid:(NSString *)pass:(NSString *)dob:(double) table:(double)table1{
i import my class and create object like this
#import "Personal.h"
#class Personal;
Personal *personDetails;
calling that mathod like this
[personDetails newDetails:username:categoryMale:financeYear:0:0:userID:password:dob:0:0];
calling that mathod like this
but its not working .
guide me how can call this method
Suppose you have a class A that calls function in class Personal.
In Class A.m do #import "Personal.h"
Make sure that the function is defined in the Personal.h file otherwise it will give warning.
Now In class A when you want to call that function.
you can do.
Personal *per=[Personal alloc]init];
[per newDetails:username:categoryMale:financeYear:0:0:userID:password:dob:0:0];.
Note: If you are coming from Class Personal to Class A and then you want to call a function in Class Personal then no need to create any objects you can simply use delegates.
Initialize the object
personDetails=[[Personal alloc] init] ;
Or if you have any Custom Initializer you can use that.
Related
I know usually, when you want to call a method on another object, you do:
NewObject *object = [NewObject alloc]init];
[object callMethod];
But I created a class that isn't an object itself meaning it doesn't have properties or memory management. It has a couple methods that calculate some stuff.
From any other class, all I have to do is import the header for this class and do:
#import "MyClass.h"
[MyClass callMethod];
Why in this case do I not have to alloc init? It works just fine.
It sounds like you are trying to call a class method. These are methods which have been defined as:
+(void) myStaticMethod;
instead of
-(void) myMethod;
The plus sign indicates that the method does not use any fields, and thereby does not need to instantiate the object.
In your example, "object" is an instance of a class "NewObject" which has been allocated memory and initialized. Where-as your example, "MyClass" is only a class which because it has static members declared as above, does not need to be instantiated.
Class methods provide a nice way to combine a bunch of related functions into one place, rather than having them spread out in the regular namespace, as would usually be done in straight C. You can also have both class methods and instance methods in the same class, using the class ones when needed, and instantiating the class to use the instance ones when needed.
EDIT: Changed terminology to refer to class methods instead of static methods.
because you are calling a class method. You only need to alloc init objects. Classes only need to be included but not alloc inited. So you don't need to init an NSString class, say.
Edit:
Let's just have some nonsense examples:
+ (void)classMethod {
NSLog("Hi!");
}
[SomeClass classMethod]; // prints Hi!
- (void)instanceMethod { // (say it's an instance method of NSString)
NSLog(self);
}
[#"someNSString" instanceMethod]; // prints someNSString. But you need to have a string first, otherwise you cannot use this method.
There is a difference between "instance" methods (normal ones), that have to be called on an object and have access to self, and "class" methods (called static, in many programming languages), that are invoked on the class and thus do not have a self.
Class methods are similar to C++ static methods, in that they can be invoked without creating a concrete instance of the class. The usefulness of this is you can call a class's specialized factory methods to create a new instance; or, you can define a utility library under the scope of a class that may or may not provide concrete instances depending on the task.
Look at NSDate and NSNumber are good examples of this in the Foundation framework.
I've got two view controllers that have some similar function. Some of them are IBActions. Is there a way to put them together(so that it's easier to fix bugs)? I've tried inheritance but it seems that it does not work.
#implementation ClassA
-(IBAction)f
{
//some code here
}
#implementation ClassB
-(IBAction)f
{
//some code here
}
My question is that is there a way that I write function f once? If there is a bug, I could fix it more quickly this way.
in inheritence, you can just declare in parent class, like abstract function, and for each child defination will be separte, now you have to do is this
make a method in parent class that performs the logic only,
make two ibactionn methods both in childs and perform the current child's UI tasks in that methods and use the parent's logic method, that u created above, to get the data.
inform if you get soltion or not.
Make a method that does the job and call it either from the IBAction or wherever you need it.
You can make utility kind of class where you can make class method for this.
And you can call this method where needed.
You Can make another singleton class or utility class as per your requirement. And make class method, for common functionality. So u can use this functionality any where from project.
Its good practice to make utility class or singleton class for some common functionalities.
Like:
#interface ClassXXName(private)
- (void) xxxfunctions
#end
or user category methods?
#interface Foo() creates a class extension (I stand corrected, props to bbum) on interface Foo which is like additional methods added to the interface. Some people also use #interafce Foo(Private) (category) instead of a class extension with (). It's more like "injecting" new methods into a class from outside the class.
Placing this in the .m file just keeps other things from "seeing it" in the .h file, but that's it. Basically people normally use categories or class extensions in .m files to specify private interfaces, but they are also used for things like UIKit uses categories to add row and section public methods to NSIndexPath. (This can be confusing.)
You don't really need to define private methods this way, but if you have a method called bar that calls method foo before foo is defined in the source file you'll get a compiler warning something like "object self may not respond to foo". You can get rid of that by defining foo before you define bar or any other foo-calling code. It's the same with plain C and functions.
Like Ole says this doesn't stop anyone from calling the private methods, it just declares your intention that they be private and causes the compiler to generate the "may not respond to" warnings even if they import the .h file.
can anyone tell me how to create a class method and using that how can we share an object DATA in all classes......?
A class method has no state therefore can't do what you're asking for. You want to create a singleton object and pass that around. Check out Singletons, AppDelegates and top-level data for a discussion of singletons and a handy macro for automatically creating them.
-(returnType)methodName; --> this is your normal object method
+(returnType)methodName; --> this will be your class method
[Classname methodName] --> This is how you will call the class method
I have built a class which has a few methods in it, once of which returns an array, lets call this class A.
I have a second class, class B, which I would like to use to call the method from class A.
But, now how do I call that method from class A and store what is returned in a var in class B? Do I have to initiate the class? I have made sure to include the .h file from class A into class B.
Thanks for helping a newbie.
UPDATE:
Here is how I thought I could do this (DataStore is my class A and pushRideData is my method that returns an array):
DataStore *store = [[DataStore alloc] init];
trailsArray = [store pushRideData];
Assuming you have files A.h, A.m B.h and B.m to define your two classes, then you need to do the following:
Make sure A.h and B.h are #imported into your projects PCH file (this is the easiest/fastest way, but you could also choose to import the files into all the .m files, instead).
If you refer to a class -- say, B *something in A.h -- before that class's header file is imported, then use a forward class declaration to shut up the compiler. I.e. #class B; before the #interface A:NSObject in A.h
If you want to call an instance method of a class, you need to instantiate the class as you describe. Or, if the instance is created somewhere else, you'll need some mechanism to retrieve it. A class method, perhaps, or a global variable or a controller or something like it.
None of this is really that much different than straight C save for a formal notion of Objects (as opposed to malloc'ing a bunch of memory and passing around pointers).
That's a fairly abstract question. Yes, you need an instance to be able to store instance variables in it. You will need to allocate and init the instance, assinging it to an instance or local (pointer) variable in the calling class unless it is one of the several in the Cocoa Touch frameworks which use the singleton pattern, such as the application delegate. Such singletons have special case-specific class methods for obtaining the singleton instance.