我是 Objective-C 编程和 Xcode 的新手。我继承了一个项目的旧版本(3.2.5),最近将其转换为最新版本(4.5.2)。我将此项目转换为 ARC,但我在 typedef 结构中找到了一个objective-c 对象时遇到了问题:
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;
当我尝试使用__unsafe __unretained_
它时,它在 ARC 中编译得很好,但该项目不再工作了。当然,*image 对象没有保留,我的项目崩溃了。
结构体的使用如下:
@interface BitmapFont : NSObject {
GameController * sharedGameController;
Image * image;
BitmapFontChar * charsArray; // typedef struct
int commonHeight;
Color4f fontColor;
}
...
charsArray = calloc(kMaxCharsInFont, sizeof(BitmapFontChar));
如何将此代码转换为可以保留*image
对象但也可以在 ARC 中使用的代码?
编辑:好的,我使用 CFTypeRef 使用 Jano 的建议。我仍然在同一行代码中遇到相同的崩溃(EXC_BAD_ACCESS)。我想我没有正确使用 CFBridgingRetain 。下面是我的 .h 文件:
#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
下面是我的 .m 文件的一部分,其中包含发生崩溃的方法和代码行:
-(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;
}
}
再次感谢您的建议!