How to call a method in init method? - objective-c

My program looks like this:
-(id)init
{
if ( (self = [super init]) )
{
//TargetWithActions *targetActions= [[TargetWithActions alloc] init];
[self countDownSpeed123];
}
return self;
}
-(void)countDownSpeed123
{
countDownSpeed = 5.0f;
}
#end
warning: 'TargetWithActions' may not respond to '-countDownSpeed123'
I am getting the warning in this way. Where I am wrong in my program. Please explain ?
Thank You.
If I need to use the countDownSpeed value in another class, how can I retain the value ? How can I use in the other class? I think retain works for pointer types.
EDIT:
Sorry for my bad coding and negligence. I have did mistakes in my program which are very blunt.
Thanks for answering.
First: I did not declare the
function (
-(void)countDownSpeed123; )in
interface.
Second: I did not include the
following in my class where I needed
the (countDownSpeed) value.
TargetWithActions *targetActions= [[TargetWithActions alloc] init];
[targetActions countDownSpeed123];
Now, I got what I need.
Thank You.

In the class where you trying to use
TargetWithActions, and in TargetWithActions.m make sure you
have #import
"TargetWithActions.h".
In TargetWithActions.h make sure
in your class declaration
you declared the method -(void)countDownSpeed123;
Sorry I don't understand what are you trying to do with countDownSpeed123, it does not return anything (void) so I'm not quite sure what you want to retain. If the method returns simple value like float or int you don't have to retain it, it is passed by value - it will be copied.

Sorry for my bad coding and negligence. I have did mistakes in my program which are very blunt. Thanks for answering.
First: I did not declare the function ( -(void)countDownSpeed123; )in interface.
Second: I did not include the following in my class where I needed the (countDownSpeed) value.
TargetWithActions *targetActions= [[TargetWithActions alloc] init];
[targetActions countDownSpeed123];
Now, I got what I need.
Thank You.

Related

Functions and return in OOP

I'm currently trying to learn Objective C and by this way, Oriented Object languages.
I'm declaring variables from a class I've written, but, my functions are way too long, and I'd like to cut that code off.
I do not know how the return works with classes and that's my problem.
Grapejuice *juice;
juice = [[Grapejuice alloc] init];
[juice setName:#"Grape juice"];
[juice setOrderNumber:1000];
[juice setPrice:1.79];
This, is part of a main in which I'm doing this to several objects, how can I do that in a separated function, and still got these informations out of this new function to be re-used later (to be printed for example) ?
Not sure if I'm clear but I've just started learning it yesterday, still hesitating on the basics.
Thanks homies.
If I understand you correctly, I believe what you want is a custom "init" method for your Grapejuice class.
In Grapejuice.h, add:
- (instancetype)initWithName:(NSString *)name orderNumber:(NSInteger)number price:(double)price;
In Grapejuice.m, add:
- (instancetype)initWithName:(NSString *)name orderNumber:(NSInteger)number price:(double)price {
self = [super init];
if (self) {
_name = name;
_orderNumber = number;
_price = price;
}
return self;
}
Then to use that code you do:
Grapejuice *juice = [[Grapejuice alloc] initWithName:#"Grape Juice" orderNumber:1000 price:1.79];
Please note that you may need to adjust the data types for the orderNumber and price parameters. I'm just guessing. Adjust them appropriately based on whatever type you specified for the corresponding properties you have on your Grapejuice class.

In Objective-C is there a way to get a list of the methods called by a method?

I have been doing some research online and have found that using the ObjectiveC package in Objective C you can get a list of all the methods on a class using class_copyMethodList(), and I see you can get the implementation (IMP) of a method using instanceMethodForSelector:. The Apple documentation here has been helpful so far but I'm stuck and not sure what I'm really looking to find.
I want a list of the methods/functions called in a given method's implementation so I can build a call tree.
Any suggestions? Thanks in advance!
This solution is kind of hard way, and will cause a line of code in every method You can also make use of sqlite and save the tracked methods..
MethodTracker.h
#interface MethodTracker : NSObject
#property (nonatomic) NSMutableArray *methodTrackArr;
+ (MethodTracker *)sharedVariables;
#end
MethodTracker.m
#implementation MethodTracker
static id _instance = nil;
+ (MethodTracker *)sharedVariables
{
if (!_instance)
_instance = [[super allocWithZone:nil] init];
return _instance;
}
// optional
- (void)addMethod:(NSString *)stringedMethod
{
// or maybe filter by: -containObject to avoid reoccurance
[self.methodTrackArr addObject:stringedMethod];
NSLog("current called methods: %#", methodTrackArr);
}
#end
and using it like:
OtherClass.m
- (void)voidDidLoad
{
[super viewDidLoad];
[[MethodTracker sharedVariables] addMethod:[NSString stringWithUTF8String:__FUNCTION__]];
// or directly
[[MethodTracker sharedVariables].methodTrackArr addObject:[NSString stringWithUTF8String:__FUNCTION__]];
}
- (void)someOtherMethod
{
// and you need to add this in every method you have (-_-)..
[[MethodTracker sharedVariables] addMethod:[NSString stringWithUTF8String:__FUNCTION__]];
}
i suggest you import that MethodTracker.h inside [ProjectName]-Prefix.pch file.
Sorry, for the double answer, i deleted the other one and i have no idea how did that happen..
Hope this have helped you or at least gave you an idea.. Happy coding,
Cheers!
I think in the runtime track method is possible, but function not.
I have been build a tool DaiMethodTracing for trace all methods activity in single class for some of my need. This is based on objective-c method swizzling. So, there is an idea to do this
List all Classes in your application.
swizze all the methods in each class.
filter the method you want to trace.
finally, you may got the method call path.

Syntax to return a on Objective-C class reference from a method?

I did try to google this, but actually found nothing. Coming from a strong Smalltalk background, I thought the following would be fine:
#import "ValveTargetState.h"
- (id) targetStateClass {
return ValveTargetState;
}
- (void) targetIsActive:(BOOL)isActive {
self.targetState = [[[self targetStateClass] alloc] initValve: self isActive: isActive];
[self.targetState push];
}
Basically, I've added a method, so that subclasses can tune what the targetStateClass is used. It was my understanding, that like Smalltalk, classes are objects too in ObjC. But Xcode tells me
Unexpected interface name 'ValveTargetState': expected expression
(for the return ValveTargetState; expression)
I think I'm missing something obvious, but what is it?
Try this:
- (Class)targetStateClass
{
return [ValveTargetState class];
}
Assuming that ValveTargetState is a class that inherits ultimately from NSObject, either
[ValveTargetState class]
or
[ValveTargetState self]
will give you the pointer to the class object for ValveTargetState.
It would be much better to use ValveTargetState directly, but unfortunately the name of a class is not a valid expression in Objective-C.

Can't call a method with another method, what am I missing?

Extremely basic objective-c question here. I want to call a method from within another method and send a variable from the first method to the second method, but I'm not sure how to handle this with #implementation, etc.
Here is what I want:
-(int) isItFav:(int) favNum
{
// some code
}
- (IBAction)myBar:(UISegmentedControl *)sender
{
// some code
int x = 10;
[isItFav x];
}
This causes as error, as isItFav is an undeclared identifier. Can someone please tell me how to fix this up?
If both myBar: and isItFav: are in same class:
int returnedValue = [self isItFav:x];
If in different class, then
int returnedValue = [objectOfClassWhichContainsIsItFavMethod isItFav:x];
This is Objective-C. Please see tutorials and manuals.
[receiverOfCall method:param];
This case:
int result = [self isItFav:x];
Message Call Format is
[sender message:params]

Question about instantiating object

I am new to Objective C and Cocoa. Really don't like the syntax as I'm from Java and C#. I'm trying to do something simple and I get the following error:
I import this in using
#import "Defaults.h"
-(void) awakeFromNib{
Defaults *theDefaults = [[Defaults alloc] init];
}
-(IBAction) addPlanets:(id)sender{
[theDefaults setValue:[planetsButton titleOfSelectedItem] forKey:#"planets"];
NSLog([planetsButton titleOfSelectedItem]);
}
The error I get is
Unknown Receiver theDefaults; Did you mean "Defaults"?
Can someone help me on what this is?
theDefaults doesn't exist in the scope of addPlanets:. You need to make it a global or an instance variable, rather than creating it in awakeFromNib and immediately leaking it.