• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Java and Objective-C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Java and Objective-C


  • Subject: Re: Java and Objective-C
  • From: Denis Bohm <email@hidden>
  • Date: Sat, 7 Jun 2008 13:54:00 -0700


On Jun 7, 2008, at 1:49 PM, Bill Bumgarner wrote:

Thank you -- this is the kind of side by side, purely code oriented, set of comparisons that I think are both largely missing and generally quite useful.

Comments inline.

On Jun 7, 2008, at 1:30 PM, Denis Bohm wrote:
The Objective-C example on that page is:

- (void)setGridVisible:(NSNumber *)flag {
BOOL flagValue = [flag boolValue];
if (gvFlags.showGrid != flagValue) {
NSNumber *currentValue = [NSNumber numberWithBool:gvFlags.showGrid];
gvFlags.showGrid = flagValue;
if (flagValue)
[graphicView resetGUP];
[graphicView cache:[graphicView bounds]];
[undoManager registerUndoWithTarget:self
selector:@selector(setGridVisible:)
object:currentValue];

I would generally write this as:

[[NSUndoManager prepareWithInvocationTarget: self] setGridVisible: currentValue];

In particular, the above form allows one to undo any random state change and not just one that can be represented by a method that takes a single argument as a parameter.

For example:

[[NSUndoManager prepareWithInvocationTarget: self] setFrame: myRect animate: YES spline: splineStruct];

-setFrame:animate:spline: is a method that only exists in my application.

      [undoManager setActionName:GRID_OP];
  }

Something similar in Java could be done as:

void setGridVisible(boolean flagValue) {
if (gvFlags.showGrid != flagValue) {
boolean currentValue = gvFlags.showGrid;
gvFlags.showGrid = flagValue;
if (flagValue) {
graphicView.resetGUP();
graphicView.cache(graphicView.bounds());
undoManager.registerUndoWithTarget(this, "setGridVisible", currentValue);
undoManager.setActionName(GRID_OP);
}


Where the methods in the Java UndoManager could be implemented something like:

public void registerUndoWithTarget(Object target, Method method, Object... args)

public void registerUndoWithTarget(Object target, String methodName, Object... args) {
Method[] methods = target.getClass().getDeclaredMethods();
Method method = getBestMatch(target.getClass(), getTypesOf(args)); //
...
}


protected Method getBestMatch(Class targetClass, Class[] argClasses) {
Method[] methods = targetClass.getDeclaredMethods();
...
}


The Objective-C and Java versions of the user code look pretty similar. What aspects of the Objective-C undo manager am I missing?

How would you support the more generically applicable invocation form in Java?


I.e. this form:

[[NSUndoManager prepareWithInvocationTarget: self] setFrame: myRect animate: YES spline: splineStruct];

That is handled by the Java example above (via the "Object... args"). A method with any number of arguments can be passed to registerUndoWithTarget. So you could do something like:


undoManager.registerUndoWithTarget(this, "setFrame", true, splineStruct);

Denis
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Java and Objective-C
      • From: Bill Bumgarner <email@hidden>
References: 
 >Learning Cocoa with RubyCocoa (was Regular Expressions) (From: Jose Raul Capablanca <email@hidden>)
 >Re: Learning Cocoa with RubyCocoa (was Regular Expressions) (From: "Felipe Monteiro de Carvalho" <email@hidden>)
 >Java and Objective-C (From: Bill Bumgarner <email@hidden>)
 >Re: Java and Objective-C (From: Denis Bohm <email@hidden>)
 >Re: Java and Objective-C (From: Bill Bumgarner <email@hidden>)

  • Prev by Date: Re: Java and Objective-C
  • Next by Date: Re: xCode resources and paths in bundle
  • Previous by thread: Re: Java and Objective-C
  • Next by thread: Re: Java and Objective-C
  • Index(es):
    • Date
    • Thread