The dashes in the title were supposed to be "vs", but I ran out of space.
First a general question: in your expert view, what is good coding practice when sharing code between Mac OS X and iOS projects?
Sometimes I see this on websites:
#if TARGET_OS_IPHONE (or similar)
do something
#else
do something else
#endif
Is this the best approach? To me it seems like every other line would duplicate itself until it's a gigantic mess of #if statements
Also, how or where do you tell xCode when to set TARGET_OS_IPHONE to 0 or 1?
Since the mac has cocoa, iOS has appkit and both have core graphics, custom drawing can also be done with core graphics. That should promote code sharing between a mac and iOS project, or not?
The code below can be found in the documentation or on google and it seems to work just fine. It deals with converting between CGColorRef and NSColor or UIColor. By sticking the conversion into a separate file and keeping the drawing code generic by using CGColorRef, it should minimize code duplication in the drawing routines of both projects, or not?
My question is about the last line:
A corefoundation object is cast to an NSObject (?) and then autoreleased. Is this valid code or good practice?
I thought corefoundation objects could not be autoreleased.
Thanks for sharing your insight!
First a general question: in your expert view, what is good coding practice when sharing code between Mac OS X and iOS projects?
Sometimes I see this on websites:
#if TARGET_OS_IPHONE (or similar)
do something
#else
do something else
#endif
Is this the best approach? To me it seems like every other line would duplicate itself until it's a gigantic mess of #if statements
Also, how or where do you tell xCode when to set TARGET_OS_IPHONE to 0 or 1?
Since the mac has cocoa, iOS has appkit and both have core graphics, custom drawing can also be done with core graphics. That should promote code sharing between a mac and iOS project, or not?
The code below can be found in the documentation or on google and it seems to work just fine. It deals with converting between CGColorRef and NSColor or UIColor. By sticking the conversion into a separate file and keeping the drawing code generic by using CGColorRef, it should minimize code duplication in the drawing routines of both projects, or not?
My question is about the last line:
Code:
return (CGColorRef)[(id)CGColorCreate(colorSpace, components) autorelease];
I thought corefoundation objects could not be autoreleased.
Thanks for sharing your insight!
Code:
@implementation NSUserDefaults (UserDefaultsExtensions)
-(void) setColor:(NSColor *) aColor
forKey:(NSString *) aKey
{
NSData *theData = [NSArchiver archivedDataWithRootObject:aColor];
[self setObject:theData
forKey:aKey];
}
-(NSColor *) colorForKey:(NSString *) aKey
{
NSColor *theColor = nil;
NSData *theData = [self dataForKey:aKey];
if (theData != nil)
theColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:theData];
return theColor;
}
@end
#pragma mark -
#pragma mark -
@implementation NSColor (CGColor)
+(NSColor *) colorWithCGColor:(CGColorRef) aColorRef
{
NSColorSpace *colorSpace = [[NSColorSpace alloc] initWithCGColorSpace:CGColorGetColorSpace(aColorRef)];
NSColor *color = [NSColor colorWithColorSpace:colorSpace
components:CGColorGetComponents(aColorRef)
count:CGColorGetNumberOfComponents(aColorRef)];
[colorSpace release];
return [color autorelease];
}
-(CGColorRef) CGColor
{
const NSInteger numberOfComponents = [self numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[self colorSpace] CGColorSpace];
[self getComponents:(CGFloat *)&components];
return (CGColorRef)[(id)CGColorCreate(colorSpace, components) autorelease];
}
@end