方法调用中的NSBundle参数

时间:2011-03-02 10:00:27

标签: objective-c path nsbundle

我必须称这种方法:

[super initWithPNGFileName: "/Volumes/SAGITTER/Lavoro/Progetti xCode/IVBricker test/ball.png" andGame:game andCGRect:CGRectMake(x,y,16,16)];

所以当我把它放在iPhone上时,路径不对。我知道我必须使用NSBundle,但如果我这样做

[super initWithPNGFileName:[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] andGame:game andCGRect:CGRectMake(x,y,16,16)];

我得到警告:“从不兼容的指针类型传递'initWithPNGFileName:andGame:andCGRect:'的参数1”

我应该如何使用NSBundle?

2 个答案:

答案 0 :(得分:0)

您的第一个调用将c-string传递给initWithPNGFileName:方法,而-pathForResource:返回NSString。从您从bundle获得的路径获取c-string并将该字符串传递给您的方法:

[super initWithPNGFileName:[[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] UTF8String]
                   andGame:game 
                 andCGRect:CGRectMake(x,y,16,16)];

答案 1 :(得分:0)

如果[super initWithPNGFileName: "/Volum...是100%正确的代码,我认为此消息需要C样式字符串 但是[[NSBundle mainBundle] pathForResource:...]将返回一个NSString对象。

尝试将[[[NSBundle mainBundle] pathForResource:@"ball" ofType:@"png" inDirectory:@"/"] cStringUsingEncoding:NSUTF8StringEncoding]传递给方法。