I am new to Objective-C programming and Xcode. I inherited an old version (3.2.5) of a project, and I recently converted it to the newest version (4.5.2). I converted this project to ARC, and I am having issues with an objective-c object found within a typedef struct:
typedef struct _BitmapFontChar {
int charID;
int x;
int y;
int width;
int height;
int xOffset;
int yOffset;
int xAdvance;
Image * image; // Objective-C object in struct forbidden in ARC
float scale;
} BitmapFontChar;
When I try to use __unsafe __unretained_
it compiles fine in ARC, but the project doesn't work anymore. The *image object is not retained, of course, and my project crashes.
The struct is used as follows:
@interface BitmapFont : NSObject {
GameController * sharedGameController;
Image * image;
BitmapFontChar * charsArray; // typedef struct
int commonHeight;
Color4f fontColor;
}
...
charsArray = calloc(kMaxCharsInFont, sizeof(BitmapFontChar));
How do I convert this code to something that will retain the *image
object, but will also work in ARC?
EDIT: Okay, I used Jano's suggestion using CFTypeRef. I'm still getting the same crash (EXC_BAD_ACCESS) at the same line of code. I think I'm not using the CFBridgingRetain properly. Below is my .h file:
#import "Global.h"
@class Image;
@class GameController;
#define kMaxCharsInFont 223
typedef struct _BitmapFontChar {
int charID;
int x;
int y;
int width;
int height;
int xOffset;
int yOffset;
int xAdvance;
CFTypeRef image;
float scale;
} BitmapFontChar;
enum {
BitmapFontJustification_TopCentered,
BitmapFontJustification_MiddleCentered,
BitmapFontJustification_BottomCentered,
BitmapFontJustification_TopRight,
BitmapFontJustification_MiddleRight,
BitmapFontJustification_BottomRight,
BitmapFontJustification_TopLeft,
BitmapFontJustification_MiddleLeft,
BitmapFontJustification_BottomLeft
};
@interface BitmapFont : NSObject {
GameController *sharedGameController;
Image *image;
BitmapFontChar *charsArray;
int commonHeight;
Color4f fontColor;
}
@property(nonatomic, strong) Image *image;
@property(nonatomic, assign) Color4f fontColor;
- (id)initWithFontImageNamed:(NSString*)aFileName ofType:(NSString*)aFileType
controlFile:(NSString*)aControlFile scale:(Scale2f)aScale filter:(GLenum)aFilter;
- (id)initWithImage:(Image *)aImage controlFile:(NSString *)aControlFile
scale:(Scale2f)aScale filter:(GLenum)aFilter;
-(void)renderStringAt:(CGPoint)aPoint text:(NSString*)aText;
-(void)renderStringJustifiedInFrame:(CGRect)aRect justification:(int)aJustification
text:(NSString*)aText;
-(int)getWidthForString:(NSString*)string;
-(int)getHeightForString:(NSString*)string;
@end
And below is part of my .m file with the method and line of code where the crash happens:
-(void)renderStringAt:(CGPoint)aPoint text:(NSString*)aText {
float xScale = image.scale.x;
float yScale = image.scale.y;
for(int i=0; i<[aText length]; i++) {
unichar charID = [aText characterAtIndex:i] - 32;
int y = aPoint.y + (commonHeight * yScale) - (charsArray[charID].height
+ charsArray[charID].yOffset) * yScale;
int x = aPoint.x + charsArray[charID].xOffset;
CGPoint renderPoint = CGPointMake(x, y);
CFTypeRef vpImage = CFBridgingRetain(image); //???
((__bridge Image *)(charsArray[charID].image)).color = fontColor;//CRASH EXC_BAD_ACCESS
[((__bridge Image *)(charsArray[charID].image)) renderAtPoint:renderPoint];
aPoint.x += charsArray[charID].xAdvance * xScale;
}
}
Thank you again for your suggestions!