Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
As I work my way through the book and the challenges, I often find myself off on some detour to really try and understand what Hillegass means...and this is one of them.
In order to do challenge 18, the one with the ovals and document architecture, I found myself looking at Apple's Sketch...which leads me to this question....which I have not been able to find asked elsewhere.
So, there are 2 parts to the query....which in all likelihood are intimately related.

One of the classes gave me an error. I had added a macro to the class so I could see what method was being called. Soon realized, that the macro, which worked in every other case, did not work **if** the method was **Outside** of an @implementation....end block. Like so....



Code:
#import <AppKit/AppKit.h>

#define METHOD_LOG (NSLog(@"Method: %@ self = %@", \
NSStringFromSelector(_cmd), \
self))

@interface NSObject (SKTPerformExtras)

- (void)performSelector:....blah blah;
- (void)performSelector:(SEL)sel ....blah blah;

@end

NSRect SKTRectFromPoints(NSPoint point1, NSPoint point2);

and


Code:
#import "SKTFoundationExtras.h"

@implementation NSObject (SKTPerformExtras)

- (void)performSelector:(SEL)sel withEachObjectInArray:(NSArray *)array {
	METHOD_LOG;
    ......more stuff
    }
}




@end

NSRect SKTRectFromPoints(NSPoint point1, NSPoint point2) {
	METHOD_LOG;  //<<<error
    return NSMakeRect(((point1.x <= point2.x) ? point1.x : point2.x), ((point1.y <= point2.y) ? point1.y : point2.y), ((point1.x <= point2.x) ? point2.x - point1.x : point1.x - point2.x), ((point1.y <= point2.y) ? point2.y - point1.y : point1.y - point2.y));
}

So, my 2 questions are this.

1) **Why** did the writer of the app choose to place the method **outside** of the implementation block?
2) Probably related to the above, why is the macro here ??out of scope?? if that indeed is the problem.

Thanks as always
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I don't have access to the book, and can't say what the author's intent was exactly, but that looks to me like a helper method that is not an instance or method variable at all, like NSMakeRect. The author probably believes that if you are using the class, you may want access to this helper method. When you #import the class's files, you'll get the related (though not part of the class itself) method, too. You'll call it like any C function:
Code:
NSRect x = SKTRectFromPoints(NSMakePoint(1.0f,1.0f),NSMakePoint(2.0f,2.0f));

I purposely used NSMakePoint, since it is a similar method available in Foundation.

-Lee

EDIT: The reason your macro doesn't work is it depends on all sorts of things that will only be there in an instance method, and this is just a plain old C function.
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
I don't have access to the book, and can't say what the author's intent was exactly, but that looks to me like a helper method that is not an instance or method variable at all, like NSMakeRect. The author probably believes that if you are using the class, you may want access to this helper method. When you #import the class's files, you'll get the related (though not part of the class itself) method, too. You'll call it like any C function:
Code:
NSRect x = SKTRectFromPoints(NSMakePoint(1.0f,1.0f),NSMakePoint(2.0f,2.0f));

I purposely used NSMakePoint, since it is a similar method available in Foundation.

-Lee

EDIT: The reason your macro doesn't work is it depends on all sorts of things that will only be there in an instance method, and this is just a plain old C function.


Lee...OK...so let me ask it another way :)

Why would you **not** make a function an instance method..in a very general sense. From what I have read, this function will not have access to any of the ivars etc etc in the class. Perhaps this is quite common, but I have not seen this used before.

Added later.
Lee....just realized that, as you say, it's just a plain old C function. [Red face]. Found it in the "Foundation Functions" reference, but had not put 2 and 2 together. Thanks for answering the obvious!! :)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Why does this function need access to ivars? It just wraps a little logic around NSMakeRect. It really has nothing to do with the class, but the author must have thought it would be handy for use with the class. NSMakeRect is a function like this. It gets you a struct, and is wholly uncoupled from any class. The only slightly odd thing here is the function being included with a class implementation, but this just seems like a shortcut to getting it included instead of having its own source and header file.

-Lee

edit: started this before your edit.
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
Why does this function need access to ivars? It just wraps a little logic around NSMakeRect. It really has nothing to do with the class, but the author must have thought it would be handy for use with the class. NSMakeRect is a function like this. It gets you a struct, and is wholly uncoupled from any class. The only slightly odd thing here is the function being included with a class implementation, but this just seems like a shortcut to getting it included instead of having its own source and header file.

-Lee

edit: started this before your edit.

Thanks as always.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.