Objective-C プログラミングと Xcode は初めてです。プロジェクトの古いバージョン (3.2.5) を継承し、最近それを最新バージョン (4.5.2) に変換しました。このプロジェクトを ARC に変換しましたが、typedef 構造体内にある object-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;
}
}
ご提案いただきありがとうございます。