custom method help

Discussion in 'Mac Programming' started by liptonlover, Aug 18, 2008.

  1. liptonlover macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    I'm working on a framework, mostly for learning but partly because I had something I want to be able to use, but I realized I don't really know how to make methods. I've used various kinds, but I don't really know what anything is or how to make one with multiple values etc..
    First, what's the difference between (id) and (void)? I know that part of the method specifies what kind of object is accepted when the method is called, but I don't see the difference between those two.

    Now for the real question. Here's how I want my currently fictional method:

    - (void)animateIntValueTo:_____ atSpeed:_____ {

    }

    What goes in those blanks? I know in the documentation, it would do something like: (int)anInt or (NSNumber *)aNumber but doing either of those gave me an error, something like 'NSNumber has already been defined' or something like that. Do I need to put variables/objects declared in my header in the blank spaces? If not, how do I get the values the user puts in the slots?
    Thanks in advance,
    Nate
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Basically they are just telling you what types the method accepts.

    Interface

    Code:
    -(void) animateIntValueTo:(int)myInt :atSpeed:(float)myFloat;
    Implementation

    Code:
    -(void) animateIntValueTo:(int)myInt :atSpeed:(float)myFloat
    {
    	
    }
    Edit : Fixed post.
     
  3. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #3
    So I just replace myX with a variable or object?

    Also :atSpeed: is just a typo right? The first colon shouldn't be there?

    Thanks! I really gotta pay you some day...
    Nate
     
  4. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #4
    I think the extra colon is just used to state that the first set of arguments is finished and that the next one is coming up. It compiled fine anyway, not sure myself I only started learning Objective-C properly on Saturday.

    Edit : Just looked through some of the system headers and, yes, you are right, it is incorrect having the extra colon.
     
  5. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #5
    ok. It works fine without it as well.
    How have you been helping me all this time if you only started objective C last saturday???
    Nate
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    Most of your questions have either been C based or something that a quick 20 second Google showed the answer too :). 90% of programming is being able to find relevant documentation quickly and efficiently.
     
  7. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #7
    I'm still getting used to the documentation and using google... usually my google results are useless lol. But I do google and check the documentation before asking these questions.

    This framework is causing more problems than I expected...

    I want to capture the object that called my method into an NSObject instance I created so I can manipulate it in another method... how do I do that?
    The closest thing I can find is to do this...
    [object copy:sender];
    but sender isn't a keyword apparently, and doing (id)sender doesn't work either.
     
  8. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    Pass the object as an argument to the method then copy it within the method and return the copied object?

    - (NSObject *)myCopyObjectMeth:(NSObject *)myObject
    {

    }
     
  9. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #9
    I'm don't know how to access that object at all right now.
     
  10. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #10
    What code have you got at the moment?
     
  11. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #11
    here's my .h and .m... please excuse any messiness.
    Code:
    #import <Cocoa/Cocoa.h>
    
    
    @interface ESAnimation : NSObject {
    	NSView *object;
    	int loopXTimes;
    	NSTimer *timer;
    }
    - (void)animateIntTo:(int)x atSpeed:(float)y;
    - (void)animateFloatTo:(float)x atSpeed:(float)y;
    - (void)animateDoubleTo:(double)x atSpeed:(float)y;
    - (void)animateStringTo:(NSString *)x atSpeed:(float)y;
    - (BOOL)animationIsDone;
    @end
    
    Code:
    #import "ESAnimation.h"
    
    
    @implementation ESAnimation
    - (void)animateIntTo:(int)x atSpeed:(float)y {
    	
    	loopXTimes=x;
    	timer=[[NSTimer scheduledTimerWithTimeInterval:y target:self selector:@selector(incrementInt:) userInfo:nil repeats:YES] retain];
    }
    - (void)animateFloatTo:(float)x atSpeed:(float)y {
    	
    }
    - (void)animateDoubleTo:(double)x atSpeed:(float)y {
    	
    }
    - (void)animateStringTo:(NSString *)x atSpeed:(float)y {
    	
    }
    
    - (void)incrementInt:(NSTimer *)aTimer {
    	int loop=loopXTimes;
    	loopXTimes++;
    	if (loopXTimes==loop) {
    		[timer invalidate];
    		[timer release];
    		timer=nil;
    	}
    }
    - (void)incrementFloat:(NSTimer *)aTimer {
    	
    }
    - (void)incrementDouble:(NSTimer *)aTimer {
    	
    }
    - (void)incrementString:(NSTimer *)aTimer {
    	
    }
    - (BOOL)animationIsDone {
    	BOOL a=1;
    	return a;
    	//Will make BOOL value 1 when animation is finished
    }
    @end
    
    This current code works fine but doesn't exactly do anything. My goal with this class is to be able to animate variables, including strings. When I was messing a teensy bit with CA I found out that if I try animating an int it doesn't do anything. So what I want is to be able to use my methods instead, so that when used that value will increment at the set speed until it reaches the target number, thus animating it. I have a more complicated idea for animating strings but it's the same idea.

    Unfortunately just using a C loop will freeze all input until it's done of course, so I need to use a timer which involves a second method which is proving to be a pain.
     
  12. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #12
    So basically your current methods are just providing the means to animate, other methods will do the drawing?

    If that is the case, you really don't need to know about any of that when designing a framework. You just need to provide an interface (classes and methods) that the programmer uses. You do not need to know how the programmer uses them and the programmer does not need to know how you implement your methods.

    You can worry about that once you have finished your framework and actually need to use it :).
     
  13. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #13
    Code:
    int loop=loopXTimes;
    loopXTimes++;
    if (loopXTimes==loop)
    ...
    
    Just took a quick look at the code, and the above bit has obvious issues. ;)

    loopXTimes is loop+1, so when you compare if they're equal - they will never be, so that if statement will never be true.
     
  14. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #14
    thanks for pointing that out :)

    This is proving to be a bigger problem than I thought. The programmer won't have the chance to update the value each time the timer fires.
    Does anyone have a solution to my problem? I either need to allow the programmer to redraw his objects with the new value, or somehow handle it myself. I'm not sure how to do either, unless someone can show me how to do that selector thing so that I can call a method the programmer wrote each time around, so he can handle redrawing that way. Unfortunately, I don't know how.
     
  15. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #15
    Just to answer a previous question. When you have a method with -(id)methName etc it means it returns an object. When it says void it returns nothing.
     
  16. liptonlover thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #16
    ok thanks.

    I'm trying to figure out how to use a selector or a vague reference I found to fireMethod: but so far with no luck. I want the programmer to be able to specify his own redrawing method that will be called each time the value is updated.
     

Share This Page